2012-03-26 43 views
2

我在写一个小的userscript从服务器获取一些内容并将其添加到领带。
该网站有jQuery,我怎么才能使用该网站的jQuery而不是@require(因为Chrome现在不支持这个)?在目标页面上访问jQuery的现有副本

+0

您可以用'unsafeWindow.jQuery'?陷阱[这里](http://wiki.greasespot.net/UnsafeWindow)。 – 2012-03-26 11:24:22

+0

编号'unsafeWindow'是为Chrome *内容脚本*提供的,但操作方式不同。它不允许访问除标准DOM之外的JS对象。 – 2012-03-26 11:27:50

+0

另请参见:[Greasemonkey @require在Chrome中不起作用](http://stackoverflow.com/questions/9791489/greasemonkey-require-does-not-work-in-chrome)和[在页面的作用域中使用JavaScript注入JavaScript Chrome扩展程序](http://stackoverflow.com/a/9517879/938089)。 – 2012-03-26 22:26:22

回答

4

与使用jQuery的代码的现有副本是这样的:

function main() { 
    /*--- Put all of your code here. 
     Use jquery with $ or however the target page uses it. 
    */ 
} 

function addJS_Node (text, s_URL, funcToRun) { 
    var D         = document; 
    var scriptNode       = D.createElement ('script'); 
    scriptNode.type       = "text/javascript"; 
    if (text)  scriptNode.textContent = text; 
    if (s_URL)  scriptNode.src   = s_URL; 
    if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()'; 

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement; 
    targ.appendChild (scriptNode); 
} 

addJS_Node (null, null, main); 
相关问题