扇贝背单词,增加单词快捷文本复制按钮 需要安装tampermonkey

// ==UserScript==
// @name         新增复制按钮
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://web.shanbay.com/wordsweb/
// @icon         https://www.google.com/s2/favicons?domain=shanbay.com
// @grant        none
// @require      https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';

// 复制的方法
function copyText(text, callback){ // text: 要复制的内容, callback: 回调
    var tag = document.createElement('input');
    tag.setAttribute('id', 'cp_hgz_input');
    tag.value = text;
    document.getElementsByTagName('body')[0].appendChild(tag);
    document.getElementById('cp_hgz_input').select();
    document.execCommand('copy');
    document.getElementById('cp_hgz_input').remove();
    if(callback) {callback(text)}
}

    function InsertCopyBtn(){
            $(".flex-between div").each(function () {
              if ($(this).hasClass("VocabPronounce_vcoabPronounce__2D0UH") && !$(this).hasClass("copy") ){
                $(this).addClass("copy")
       
                $(this).after('<div ><button type="button" class="copyBtn">Copy</button></div>')
                $(this).after('<div style="display:none"><input id="copyInput" /></div>')

                var word1="";
                 $(".VocabPronounce_word__17Tma").each(function () {
                    word1=$(this).text()
                  })

                console.log("word:"+word1)

                $("copyInput").attr("value",word1);
                //增加响应时间
                $(".copyBtn").each(function () {
                     console.log("增加响应事件");
                       $(this).click(function(){
                           //选择复制内容
                           //const input = document.querySelector('#copyInput');
                           //input.select();
                           // (document.execCommand('copy')) {
                           //    document.execCommand('copy');
                           //    console.log('复制成功');
                           //}
                           //
                           copyText(word1, function (){console.log('复制成功')})
                       });
                  })
            }
        });
    }
    function scanCopyContainer(){
            if ($(".flex-between").length>0){
           
                InsertCopyBtn()
            }
           setTimeout(scanCopyContainer, 2000);
    }
    $(document).ready(function(){
      //隐藏文本
        setTimeout(scanCopyContainer, 300);
 
    })
    // Your code here...
})();

原文地址:https://www.cnblogs.com/jifeng/p/15118372.html