0

我写了一个 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="" 缺失。);

<script type="text/javascript" src=" http://userscripts.org/scripts/source/25105.user.js"></script >

问题是什么?

// 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
4

1 回答 1

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);
}
于 2013-09-16T10:25:39.673 回答