Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery with Tampermonkey

So my console is giving me this message -

    The page at https://en.wikibooks.org/wiki/Vehicle_Identification_Numbers_(VIN_codes)/GM/VIN_Codes ran insecure content from http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js.
jquerified: true
ERROR: Execution of script 'GMVinCodeScraper' failed! $ is not defined
ReferenceError: $ is not defined
    at getGMCodes (eval at <anonymous> (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:57:21))))
    at Function.eval (eval at <anonymous> (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:57:21))))
    at N (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:57:21)))
    at l (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:57:21)))
    at chromeEmu.extension.onRequest.addListener.Z (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:57:21)))
    at W (eval at eventHandler (eval at <anonymous> (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/content.js:57:21)))

In my userscript, I am running a function that automates inclusion of jquery on the page and it seems that jquery has been loaded because I can see a script tag with the jquery url as a src attribute.

For reference the function is here -

function jquerify(jquerified){
if(!(window.JQuery && window.Jquery.fn.jquery == '1')){
    var s = document.createElement('script');
    s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
    s.setAttribute('type','text/javascript');
    document.getElementsByTagName('head')[0].appendChild(s);
    jquerified = true;
    console.log("jquerified: " + jquerified);
    return jquerified; 
}
}
jquerify(false);

So, not sure why $ would not be defined. Any idea why?

UPDATE-

using @require solves the problem.

like image 925
praks5432 Avatar asked Dec 26 '22 19:12

praks5432


2 Answers

You can get around this by using @require url

// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js
like image 72
praks5432 Avatar answered Dec 29 '22 09:12

praks5432


Add this to your script header.

// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js

You can then use $().

like image 28
Louis Avatar answered Dec 29 '22 10:12

Louis