Jquery 使用Chrome用户脚本将脚本附加到正文

Jquery 使用Chrome用户脚本将脚本附加到正文,jquery,google-chrome,Jquery,Google Chrome,我想在Chrome中使用Greasemonkey风格的用户脚本向给定页面添加脚本 我使用它将jQuery添加到我的用户脚本中 如果我尝试使用jQuery的append方法向页面正文添加,即Test,一切正常 然而,如果我尝试附加一个JS脚本,什么都不会发生 如何解决此类问题 这是我的.user.js文件: // ==UserScript== // @match http://*/* // ==/UserScript== // a function that loads jQuery and c

我想在Chrome中使用Greasemonkey风格的用户脚本向给定页面添加脚本

我使用它将jQuery添加到我的用户脚本中

如果我尝试使用jQuery的append方法向页面正文添加,即
Test
,一切正常

然而,如果我尝试附加一个JS脚本,什么都不会发生

如何解决此类问题

这是我的.user.js文件:

// ==UserScript==
// @match http://*/*
// ==/UserScript==

// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {

$(document).ready(function() {
    $('body').append('<script type="text/javascript" src="http://timkl.com/wip/bibobRedesign/js/jquery.contenthack.js"></script>');
});

}

// load jQuery and execute the main function
addJQuery(main);
/==UserScript==
//@match-http://*/*
//==/UserScript==
//加载jQuery并在jQuery完成加载时调用回调函数的函数
函数addJQuery(回调){
var script=document.createElement(“脚本”);
script.setAttribute(“src”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load',function()){
var script=document.createElement(“脚本”);
script.textContent=“(“+callback.toString()+”);”;
document.body.appendChild(脚本);
},假);
document.body.appendChild(脚本);
}
//这个用户脚本的核心
函数main(){
$(文档).ready(函数(){
$('body')。追加('');
});
}
//加载jQuery并执行main函数
addJQuery(main);

jQuery处理脚本元素的方式不同于其他DOM元素。这方面还有其他细节

要附加脚本,最安全的方法是使用与插入jQuery库相同的方法

function main() {
    var script = document.createElement("script");
    script.setAttribute("src", "http://timkl.com/wip/bibobRedesign/js/jquery.contenthack.js");
    document.body.appendChild(script);
    $(document).ready(function () {
        //$('body').append();
    });
}
未测试选项
如果要加载很多脚本,请查看脚本加载程序(例如)

您可能可以执行以下操作:

function addLABjs(callback) {
    var script = document.createElement("script");
    script.setAttribute("src", "http://somepath.to/LAB.js");
    script.addEventListener('load', function () {
        var script = document.createElement("script");
        script.textContent = "(" + callback.toString() + ")();";
        document.body.appendChild(script);
    }, false);
    document.body.appendChild(script);
}

// the guts of this userscript


function main() {
    $LAB.script("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js").wait()
        .script("http://timkl.com/wip/bibobRedesign/js/jquery.contenthack.js")
        .script("YourOtherScript.js").wait();

    $(document).ready(function () {
    });
}

addLABjs(main);