Click doesn't work on this Google Translate button?

后端 未结 2 530
傲寒
傲寒 2021-01-14 05:06

I am creating an Tampermonkey userscript that would automatically click the \"star\" button on Google Translate website and save my searches so that I can later view them an

2条回答
  •  無奈伤痛
    2021-01-14 05:44

    Here is my fully intelligent code for storing your searches, based on Brock Adams's answer:

    // ==UserScript==
    // @name     _Auto click the Star button on Google Translate
    // @match    https://translate.google.com/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    function triggerMouseEvent (node, eventType) {
        var clickEvent        = document.createEvent('MouseEvents');
        clickEvent.initEvent (eventType, true, true);
        node.dispatchEvent   (clickEvent);
    }
    
    $(window).on('hashchange', function(e){
        var firstURL = window.location.href;
        console.log(firstURL);
        setTimeout(function(){
        var secondURL = window.location.href;
    
            if (firstURL == secondURL){
                var el = document.getElementById("gt-pb-star").firstChild;
                if (el.classList.contains("trans-pb-button-saved")){
    
                }else{
                triggerMouseEvent (el, "mouseover");
                triggerMouseEvent (el, "mousedown");
                triggerMouseEvent (el, "mouseup");  
                }
            }
        },3000);
    });
    

提交回复
热议问题