0

这个脚本(用于 Greasemonkey)不能正常工作。
我想插入一个脚本,并在加载脚本后,将文本“下载”替换为日语单词(我将使用“TEST”。)。

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

问题是什么?

4

1 回答 1

0

您必须确保元素已经加载。这意味着,在加载正文内容后,您可以查询正文中的元素。这导致以下解决方案。在 BODY 标签上,您必须添加以下内容

<body onload="myOnloadFunc()">

那么您必须按如下方式创建 myOnloadFunc:

<script type="text/javascript>
function myOnloadFunc () {

  // in here you should place the original code you have created.

  if ((document.URL.match("www.youtube.com/watch?"))|| (document.URL.match("c.youtube.com/videoplayback?"))) {
     var head = document.getElementsByTagName('head')[0]; //Working
     var script = document.createElement('script'); //Working
     script.type = 'text/javascript'; //Working
     script.src = 'http://userscripts.org/scripts/source/25105.user.js'; //Working
     script.onload = 'document.getElementsByClassName("yt-uix-button-content")[0].innerHTML.replace("Download","TEST")'; //Not working.
     head.appendChild(script); //Working
}
</script>
于 2013-09-16T08:38:04.803 回答