|
| 1 | +// https://github.com/bfred-it/webext-dynamic-content-scripts |
| 2 | +(function (global, factory) { |
| 3 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
| 4 | + typeof define === 'function' && define.amd ? define(['exports'], factory) : |
| 5 | + (factory((global.DCS = global.DCS || {}))); |
| 6 | +}(this, (function (exports) { 'use strict'; |
| 7 | + |
| 8 | +function interopDefault(ex) { |
| 9 | + return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex; |
| 10 | +} |
| 11 | + |
| 12 | +function createCommonjsModule(fn, module) { |
| 13 | + return module = { exports: {} }, fn(module, module.exports), module.exports; |
| 14 | +} |
| 15 | + |
| 16 | +var webextContentScriptPing = createCommonjsModule(function (module) { |
| 17 | +// https://github.com/bfred-it/webext-content-script-ping |
| 18 | + |
| 19 | +/** |
| 20 | + * Ping responder |
| 21 | + */ |
| 22 | +document.__webextContentScriptLoaded = true; |
| 23 | + |
| 24 | +/** |
| 25 | + * Pinger |
| 26 | + */ |
| 27 | +function pingContentScript(tab) { |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + chrome.tabs.executeScript(tab.id || tab, { |
| 30 | + code: 'document.__webextContentScriptLoaded', |
| 31 | + runAt: 'document_start' |
| 32 | + }, hasScriptAlready => { |
| 33 | + if (chrome.runtime.lastError) { |
| 34 | + reject(chrome.runtime.lastError); |
| 35 | + } else { |
| 36 | + resolve(Boolean(hasScriptAlready[0])); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +if (typeof module === 'object') { |
| 43 | + module.exports = pingContentScript; |
| 44 | +} |
| 45 | +}); |
| 46 | + |
| 47 | +var pingContentScript = interopDefault(webextContentScriptPing); |
| 48 | + |
| 49 | +async function p(fn, ...args) { |
| 50 | + return new Promise((resolve, reject) => fn(...args, r => { |
| 51 | + if (chrome.runtime.lastError) { |
| 52 | + reject(chrome.runtime.lastError); |
| 53 | + } else { |
| 54 | + resolve(r); |
| 55 | + } |
| 56 | + })); |
| 57 | +} |
| 58 | + |
| 59 | +async function addToTab(tab, contentScripts) { |
| 60 | + if (typeof tab !== 'object' && typeof tab !== 'number') { |
| 61 | + throw new TypeError('Specify a Tab or tabId'); |
| 62 | + } |
| 63 | + |
| 64 | + if (!contentScripts) { |
| 65 | + // Get all scripts from manifest.json |
| 66 | + contentScripts = chrome.runtime.getManifest().content_scripts; |
| 67 | + } else if (!Array.isArray(contentScripts)) { |
| 68 | + // Single script object, make it an array |
| 69 | + contentScripts = [contentScripts]; |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + const tabId = tab.id || tab; |
| 74 | + if (!await pingContentScript(tabId)) { |
| 75 | + const injections = []; |
| 76 | + for (const group of contentScripts) { |
| 77 | + const allFrames = group.all_frames; |
| 78 | + const runAt = group.run_at; |
| 79 | + for (const file of group.css) { |
| 80 | + injections.push(p(chrome.tabs.insertCSS, tabId, {file, allFrames, runAt})); |
| 81 | + } |
| 82 | + for (const file of group.js) { |
| 83 | + injections.push(p(chrome.tabs.executeScript, tabId, {file, allFrames, runAt})); |
| 84 | + } |
| 85 | + } |
| 86 | + return Promise.all(injections); |
| 87 | + } |
| 88 | + } catch (err) { |
| 89 | + // Probably the domain isn't permitted. |
| 90 | + // It's easier to catch this than do 2 queries |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function addToFutureTabs(scripts) { |
| 95 | + chrome.tabs.onUpdated.addListener((tabId, {status}) => { |
| 96 | + if (status === 'loading') { |
| 97 | + addToTab(tabId, scripts); |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +var index = { |
| 103 | + addToTab, |
| 104 | + addToFutureTabs |
| 105 | +}; |
| 106 | + |
| 107 | +exports.addToTab = addToTab; |
| 108 | +exports.addToFutureTabs = addToFutureTabs; |
| 109 | +exports['default'] = index; |
| 110 | + |
| 111 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 112 | + |
| 113 | +}))); |
0 commit comments