Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. // It doesn't matter if the scripts exist or not
  2. // Browser won't try to load them either way
  3. var scripts = [
  4. '//testdomain.test/script1.js',
  5. '//testdomain.test/script2.js',
  6. '//testdomain.test/script3.js'
  7. ];
  8.  
  9. function createIFrame(win, onCreated) {
  10. var iframe = win.document.createElement('iframe');
  11. iframe.onload = function () {
  12. onCreated(iframe);
  13. };
  14. win.document.body.appendChild(iframe);
  15. }
  16.  
  17. function loadScript(win, url) {
  18. var script = win.document.createElement('script');
  19. script.src = url;
  20. script.onload = function() {
  21. console.log("Script " + url + " is loaded.");
  22. };
  23. win.document.getElementsByTagName('head')[0].appendChild(script);
  24. }
  25.  
  26. createIFrame(window, function(iframe1) {
  27. loadScript(iframe1.contentWindow, scripts[0]);
  28. createIFrame(iframe1.contentWindow, function (iframe2) {
  29. loadScript(iframe2.contentWindow, scripts[1]);
  30. createIFrame(iframe2.contentWindow, function (iframe3) {
  31. loadScript(iframe3.contentWindow, scripts[2]);
  32. });
  33. });
  34. });
  35.  
  36. var scripts = [
  37. 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js',
  38. 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.js',
  39. 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js'
  40. ];
  41.  
  42. function createIFrame(win, onCreated) {
  43. var iframe = win.document.createElement('iframe');
  44. iframe.onload = function () {
  45. onCreated(iframe);
  46. };
  47. win.document.body.appendChild(iframe);
  48. }
  49.  
  50. function loadScript(win, url) {
  51. var script = win.document.createElement('script');
  52. script.src = url;
  53. script.onload = function() {
  54. console.log("Script " + url + " is loaded.");
  55. };
  56. win.document.getElementsByTagName('head')[0].appendChild(script);
  57. }
  58. window.onload = function(){
  59. createIFrame(window, function(iframe1) {
  60. loadScript(iframe1.contentWindow, scripts[0]);
  61. createIFrame(iframe1.contentWindow, function (iframe2) {
  62. loadScript(iframe2.contentWindow, scripts[1]);
  63. createIFrame(iframe2.contentWindow, function (iframe3) {
  64. loadScript(iframe3.contentWindow, scripts[2]);
  65. });
  66. });
  67. });
  68. };
  69.  
  70. /* This is valid to omit the http:/https: protocol.
  71. In that case, browser should automatically append
  72. protocol used by the parent page */
  73. var scripts = [
  74. '//testdomain.test/script1.js',
  75. '//testdomain.test/script2.js',
  76. '//testdomain.test/script3.js'
  77. ];
  78.  
  79. function appendSchema(win, url) {
  80. if (url.startsWith('//')) {
  81. var protocol = 'https:';
  82. try {
  83. var wPrev = undefined;
  84. var wCur = win;
  85. while (wPrev != wCur) {
  86. console.log(wCur.location.protocol);
  87. if (wCur.location.protocol.startsWith("http")) {
  88. protocol = wCur.location.protocol;
  89. break;
  90. }
  91. wPrev = wCur;
  92. wCur = wCur.parent;
  93. }
  94. } catch (e) {
  95. /* We cannot get protocol of a cross-site iframe.
  96. * So in case we are inside cross-site iframe, and
  97. * there are no http/https iframes before it,
  98. * we will just use https: */
  99. }
  100. return protocol + url;
  101. }
  102. return url;
  103. }
  104.  
  105. new URL(scriptURL, window.location.href).toString();
  106.  
  107. function url(win: Window, path: string): string {
  108. // We search up the window hierarchy for the first window which uses
  109. // a protocol that starts with "http".
  110. while (true) {
  111. if (win.location.protocol.startsWith("http")) {
  112. // Interpret the path relative to that window's href. So the path
  113. // will acquire the protocol used by the window. And the less we
  114. // specify in `path`, the more it gets from the window. For
  115. // instance, if path is "/foo.js", then the host name will also be
  116. // acquired from the window's location.
  117. return new URL(path, win.location.href).toString();
  118. }
  119.  
  120. // We searched all the way to the top and found nothing useful.
  121. if (win === win.parent) {
  122. break;
  123. }
  124.  
  125. win = win.parent;
  126. }
  127.  
  128. // I've got a big problem on my hands if there's nothing that works.
  129. throw new Error("cannot normalize the URL");
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement