zoukankan      html  css  js  c++  java
  • 扇贝背单词,增加单词快捷文本复制按钮 需要安装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...
    })();

  • 相关阅读:
    python之路-HTML初识
    python之路-CentOS6.5 安装Python 的依赖包
    python之路-离线pip下载Python包
    python之路-Memcache
    python之路-SQLAlchemy
    python之路-Redis
    python之路-Mysql
    偶然发现了获取有ID的dom的一种方法
    js 工厂模式和构造函数的区别
    ES6:export和import
  • 原文地址:https://www.cnblogs.com/jifeng/p/15118372.html
Copyright © 2011-2022 走看看