scripts.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global extension, browser, fetch, TextDecoder */
  24. extension.lib.core.bg.scripts = (() => {
  25. let contentScript, frameScript;
  26. const contentScriptFiles = [
  27. "lib/single-file/dist/single-file.js",
  28. "extension/index.js",
  29. "extension/lib/single-file/index.js",
  30. "extension/lib/single-file/browser-polyfill/chrome-browser-polyfill.js",
  31. "extension/lib/single-file/fetch/content/content-fetch.js",
  32. ];
  33. const frameScriptFiles = [
  34. "lib/single-file/dist/single-file-frames.js",
  35. "extension/index.js",
  36. "extension/lib/single-file/index.js",
  37. "extension/lib/single-file/browser-polyfill/chrome-browser-polyfill.js",
  38. "extension/lib/single-file/fetch/content/content-fetch.js"
  39. ];
  40. const basePath = "../../../";
  41. return {
  42. inject
  43. };
  44. async function inject(tabId, options) {
  45. await initScripts(options);
  46. let scriptsInjected;
  47. if (!options.removeFrames) {
  48. try {
  49. await browser.tabs.executeScript(tabId, { code: frameScript, allFrames: true, matchAboutBlank: true, runAt: "document_start" });
  50. } catch (error) {
  51. // ignored
  52. }
  53. }
  54. try {
  55. await browser.tabs.executeScript(tabId, { code: contentScript, allFrames: false, runAt: "document_idle" });
  56. scriptsInjected = true;
  57. } catch (error) {
  58. // ignored
  59. }
  60. if (scriptsInjected) {
  61. if (options.frameId) {
  62. await browser.tabs.executeScript(tabId, { code: "document.documentElement.dataset.requestedFrameId = true", frameId: options.frameId, matchAboutBlank: true, runAt: "document_start" });
  63. }
  64. }
  65. return scriptsInjected;
  66. }
  67. async function initScripts(options) {
  68. const extensionScriptFiles = options.extensionScriptFiles || [];
  69. if (!contentScript && !frameScript) {
  70. [contentScript, frameScript] = await Promise.all([
  71. getScript(contentScriptFiles.concat(extensionScriptFiles)),
  72. getScript(frameScriptFiles)
  73. ]);
  74. }
  75. }
  76. async function getScript(scriptFiles) {
  77. const scriptsPromises = scriptFiles.map(async scriptFile => {
  78. if (typeof scriptFile == "function") {
  79. return "(" + scriptFile.toString() + ")();";
  80. } else {
  81. const scriptResource = await fetch(browser.runtime.getURL(basePath + scriptFile));
  82. return new TextDecoder().decode(await scriptResource.arrayBuffer());
  83. }
  84. });
  85. let content = "";
  86. for (const scriptPromise of scriptsPromises) {
  87. content += await scriptPromise;
  88. }
  89. return content;
  90. }
  91. })();