Search code examples
jquerygoogle-chromegreasemonkeyuserscripts

Access an existing copy of jQuery on the target page


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)?


Solution

  • 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);