I am writing a small userscript to get some content from a server and add it to a tie.
The site has jQuery, how can I just use the site's jQuery instead of @require
(because Chrome doesn't support this now)?
Asked
Active
Viewed 450 times
2

Brock Adams
- 90,639
- 22
- 233
- 295

Li Song
- 659
- 1
- 6
- 12
-
Can you use `unsafeWindow.jQuery`? Pitfalls [here](http://wiki.greasespot.net/UnsafeWindow). – Paul Grime Mar 26 '12 at 11:24
-
No. `unsafeWindow` is provided for Chrome *content scripts* but does not work the same way. It does not allow access to JS objects except for the standard DOM. – Brock Adams Mar 26 '12 at 11:27
-
See also: [Greasemonkey @require does not work in Chrome](http://stackoverflow.com/questions/9791489/greasemonkey-require-does-not-work-in-chrome) and [Injecting JavaScript in the page's scope using a Chrome extension](http://stackoverflow.com/a/9517879/938089). – Rob W Mar 26 '12 at 22:26
1 Answers
4
Use an existing copy of jQuery with code like this:
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);

Brock Adams
- 90,639
- 22
- 233
- 295