Skip to content

Instantly share code, notes, and snippets.

@hthuong09
Created October 15, 2015 06:57
Show Gist options
  • Save hthuong09/7ec01e1e724ab59d4b92 to your computer and use it in GitHub Desktop.
Save hthuong09/7ec01e1e724ab59d4b92 to your computer and use it in GitHub Desktop.
inject jquery to any webpage
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
// the guts of this userscript
function main() {
// Note, jQ replaces $ to avoid conflicts.
alert("There are " + jQ('a').length + " links on this page.");
}
// load jQuery and execute the main function
addJQuery(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment