Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // It doesn't matter if the scripts exist or not
- // Browser won't try to load them either way
- var scripts = [
- '//testdomain.test/script1.js',
- '//testdomain.test/script2.js',
- '//testdomain.test/script3.js'
- ];
- function createIFrame(win, onCreated) {
- var iframe = win.document.createElement('iframe');
- iframe.onload = function () {
- onCreated(iframe);
- };
- win.document.body.appendChild(iframe);
- }
- function loadScript(win, url) {
- var script = win.document.createElement('script');
- script.src = url;
- script.onload = function() {
- console.log("Script " + url + " is loaded.");
- };
- win.document.getElementsByTagName('head')[0].appendChild(script);
- }
- createIFrame(window, function(iframe1) {
- loadScript(iframe1.contentWindow, scripts[0]);
- createIFrame(iframe1.contentWindow, function (iframe2) {
- loadScript(iframe2.contentWindow, scripts[1]);
- createIFrame(iframe2.contentWindow, function (iframe3) {
- loadScript(iframe3.contentWindow, scripts[2]);
- });
- });
- });
- var scripts = [
- 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js',
- 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.js',
- 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js'
- ];
- function createIFrame(win, onCreated) {
- var iframe = win.document.createElement('iframe');
- iframe.onload = function () {
- onCreated(iframe);
- };
- win.document.body.appendChild(iframe);
- }
- function loadScript(win, url) {
- var script = win.document.createElement('script');
- script.src = url;
- script.onload = function() {
- console.log("Script " + url + " is loaded.");
- };
- win.document.getElementsByTagName('head')[0].appendChild(script);
- }
- window.onload = function(){
- createIFrame(window, function(iframe1) {
- loadScript(iframe1.contentWindow, scripts[0]);
- createIFrame(iframe1.contentWindow, function (iframe2) {
- loadScript(iframe2.contentWindow, scripts[1]);
- createIFrame(iframe2.contentWindow, function (iframe3) {
- loadScript(iframe3.contentWindow, scripts[2]);
- });
- });
- });
- };
- /* This is valid to omit the http:/https: protocol.
- In that case, browser should automatically append
- protocol used by the parent page */
- var scripts = [
- '//testdomain.test/script1.js',
- '//testdomain.test/script2.js',
- '//testdomain.test/script3.js'
- ];
- function appendSchema(win, url) {
- if (url.startsWith('//')) {
- var protocol = 'https:';
- try {
- var wPrev = undefined;
- var wCur = win;
- while (wPrev != wCur) {
- console.log(wCur.location.protocol);
- if (wCur.location.protocol.startsWith("http")) {
- protocol = wCur.location.protocol;
- break;
- }
- wPrev = wCur;
- wCur = wCur.parent;
- }
- } catch (e) {
- /* We cannot get protocol of a cross-site iframe.
- * So in case we are inside cross-site iframe, and
- * there are no http/https iframes before it,
- * we will just use https: */
- }
- return protocol + url;
- }
- return url;
- }
- new URL(scriptURL, window.location.href).toString();
- function url(win: Window, path: string): string {
- // We search up the window hierarchy for the first window which uses
- // a protocol that starts with "http".
- while (true) {
- if (win.location.protocol.startsWith("http")) {
- // Interpret the path relative to that window's href. So the path
- // will acquire the protocol used by the window. And the less we
- // specify in `path`, the more it gets from the window. For
- // instance, if path is "/foo.js", then the host name will also be
- // acquired from the window's location.
- return new URL(path, win.location.href).toString();
- }
- // We searched all the way to the top and found nothing useful.
- if (win === win.parent) {
- break;
- }
- win = win.parent;
- }
- // I've got a big problem on my hands if there's nothing that works.
- throw new Error("cannot normalize the URL");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement