Last active
January 29, 2018 00:04
-
-
Save jboulhous/07b178d06e67281b5d927403063215ba to your computer and use it in GitHub Desktop.
inject jquery underscore backbone into page window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Version 0.0.1, 2018 | |
Copyright (C) 2018 Jamal Boulhous (]d). | |
Everyone is permitted to copy and distribute verbatim copies | |
of this license document, but changing it is not allowed. | |
Ok, the purpose of this license is simple | |
and you just | |
DO WHAT THE FUCK YOU WANT TO. | |
*/ | |
function ify (simbol = "$", name = "jQuery", url = "ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js", dokument = document, win = window) { | |
var scripts = dokument.getElementsByTagName("script"); | |
var script = null; | |
var filename = new RegExp(`^${name}.*\.js$`, "i"); | |
var found = false; | |
for (var i = 0; i < scripts.length; i++) { | |
script = scripts[i]; | |
// is this realy cool. | |
if (filename.test(script.src)) { | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
try { | |
found = win[simbol] && win[name] && (win[simbol] === win[name]); | |
} catch (err) { | |
console.log(`ify error : ${err}`); | |
} | |
} | |
if (!found) { | |
// inject ;-) | |
script = dokument.createElement("script"); | |
script.type = "text/javascript"; | |
var protocol = /^https:/i.test(dokument.location) ? "https" : "http"; | |
script.src = `${protocol}://${url}`; | |
dokument.getElementsByTagName("body")[0].appendChild(script); | |
} | |
return found; | |
} | |
function scriptify(simbol = "$", name = "jQuery", url = "cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js", dokument = document, win = window) { | |
var found = ify(simbol, name, url, dokument, win); | |
console.log(found, `${simbol} ${name} ${url}`) | |
} | |
function known_libs(name) { | |
if (!KNOWN_LIBS[name]) return console.log('lib not found'); | |
scriptify.apply(null, KNOWN_LIBS[name]); | |
} | |
KNOWN_LIBS = { | |
"jquery" : [], | |
"underscore" : ["_", "underscore", "cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"], | |
"backbone" : ["Backbone", "Backbone", "cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone.js"], | |
"backbone_localsotrage": ["backbone_localstorage", "backbone_localstorage", "cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.16/backbone.localStorage-min.js"], | |
}; | |
async function do_after (seconds, fn, arg) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
fn(arg); | |
resolve(); | |
}, seconds * 1000); | |
}); | |
} | |
await do_after(0, known_libs, "jquery"); | |
await do_after(2, known_libs, "underscore"); | |
await do_after(4, known_libs, "backbone"); | |
await do_after(6, known_libs, "backbone_localstorage"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment