用于更改youtube页面的Greasemonkey脚本无法正常工作

时间:2013-09-16 09:42:44

标签: javascript onload

我写了一个Greasemonkey脚本来在YouTube页面上插入一个脚本,在加载脚本后,替换内容的文本(“下载”到日语单词(在这个问题中,我将使用“TEST”代替。) )。

但是,脚本不能按照我想要的方式工作。

我想将此插入html;

  

< script type =“text / javascript”src =“http://userscripts.org/scripts/source/25105.user.js”onload =“var elements = document.getElementsByClassName('yt-uix-button -content'); for(var i = 0; i< elements.length; i ++)elements [i] .innerHTML.replace('Download','TEST');“>< / script>

但实际插入的是这个(onload =“”缺失。);

  

&lt; script type =“text / javascript”src =“http://userscripts.org/scripts/source/25105.user.js"></script&gt;

有什么问题?

// Start script
window.addEventListener("load",function(){
if ((document.URL.match("www.youtube.com/watch?"))||(document.URL.match("c.youtube.com/videoplayback?"))) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://userscripts.org/scripts/source/25105.user.js';
script.onload = "var elements=document.getElementsByClassName('yt-uix-button-content');for(var i=0;i<elements.length;i++)elements[i].innerHTML.replace('Download','TEST');"; //Not working
head.appendChild(script);
};},!1);
// End

1 个答案:

答案 0 :(得分:0)

尝试类似:

window.addEventListener("load", function(){
    if ((document.URL.match("www.youtube.com/watch?"))||(document.URL.match("c.youtube.com/videoplayback?"))) {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'http://userscripts.org/scripts/source/25105.user.js';

        if( script.readyState ) {
            script.onreadystatechange = function() {
                if( script.readyState == 'loaded' || scriptTag.readyState == 'complete' ) {
                    script.onreadystatechange = null;
                    scriptHandler();
                }
            };
        }
        else {
            script.onload = function() {
                scriptHandler();
            };
        }
        head.appendChild(script);
    }
},false);


function scriptHandler() {
    var scriptContent = document.createElement('script');
    var s = document.getElementsByTagName('head')[0];
    scriptContent.type = 'text/javascript';
    scriptContent.text = "var elements=document.getElementsByClassName('yt-uix-button-content');for(var i=0;i<elements.length;i++)elements[i].innerHTML.replace('Download','TEST');";
    s.appendChild(scriptContent);
}