{"_id":"@ajayyy/webext-content-scripts","name":"@ajayyy/webext-content-scripts","dist-tags":{"latest":"2.5.4-safari-fix"},"versions":{"2.5.4-safari-fix":{"name":"@ajayyy/webext-content-scripts","version":"2.5.4-safari-fix","description":"Utility functions to inject content scripts in WebExtensions, for Manifest v2 and v3","keywords":["contentscript","injection","manifest","chrome","firefox","browser","extension","executefunction","executescript","insertcss","mv2","mv3"],"repository":{"type":"git","url":"git+https://github.com/fregante/webext-content-scripts.git"},"funding":"https://github.com/sponsors/fregante","license":"MIT","author":{"name":"Federico Brigante","email":"me@fregante.com","url":"https://fregante.com"},"type":"module","exports":"./index.js","main":"./index.js","types":"./index.d.ts","scripts":{"build":"tsc","prepare":"tsc --sourceMap false","test":"xo && tsc --noEmit","watch":"tsc --watch"},"xo":{"rules":{"unicorn/prefer-ternary":"off","n/file-extension-in-import":"off","@typescript-eslint/no-implicit-any-catch":"off","@typescript-eslint/consistent-type-definitions":"off"}},"dependencies":{"webext-patterns":"^1.3.0","webext-polyfill-kinda":"^1.0.2"},"devDependencies":{"@sindresorhus/tsconfig":"^3.0.1","@types/chrome":"^0.0.241","@types/jest":"^29.5.3","jest-chrome":"^0.8.0","typescript":"^5.1.6","vitest":"^0.33.0","xo":"^0.54.2"},"engines":{"node":">=16"},"_id":"@ajayyy/webext-content-scripts@2.5.4-safari-fix","gitHead":"04aadc046047eb14af6b811e0f393e39c2e991a9","bugs":{"url":"https://github.com/fregante/webext-content-scripts/issues"},"homepage":"https://github.com/fregante/webext-content-scripts#readme","_nodeVersion":"20.5.0","_npmVersion":"9.8.1","dist":{"integrity":"sha512-gn1PF4+dfld6whvmjjnYGvB26z9ja0SNIcgHVQl42Bh/lz89hviH8HWqi1p7XXN/p0BsOwJAx8jzqkmb2IAQvQ==","shasum":"df397cd4ce444edc570da5ad3ade1aa2d489c8ac","tarball":"https://registry.npmjs.org/@ajayyy/webext-content-scripts/-/webext-content-scripts-2.5.4-safari-fix.tgz","fileCount":6,"unpackedSize":18060,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDT0WvW87VUM1DpC8ArtYD1mxowO8MK66Sib9qP6he2GgIgPUg6D9WIz/6ba7s2yCSLcISlCQbKAWJq9B2vuG52yCM="}]},"_npmUser":{"name":"ajayyy","email":"dev@ajay.app"},"directories":{},"maintainers":[{"name":"ajayyy","email":"dev@ajay.app"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/webext-content-scripts_2.5.4-safari-fix_1693009025590_0.1836949757296178"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-26T00:17:05.461Z","2.5.4-safari-fix":"2023-08-26T00:17:05.759Z","modified":"2023-08-26T00:17:06.103Z"},"maintainers":[{"name":"ajayyy","email":"dev@ajay.app"}],"description":"Utility functions to inject content scripts in WebExtensions, for Manifest v2 and v3","homepage":"https://github.com/fregante/webext-content-scripts#readme","keywords":["contentscript","injection","manifest","chrome","firefox","browser","extension","executefunction","executescript","insertcss","mv2","mv3"],"repository":{"type":"git","url":"git+https://github.com/fregante/webext-content-scripts.git"},"author":{"name":"Federico Brigante","email":"me@fregante.com","url":"https://fregante.com"},"bugs":{"url":"https://github.com/fregante/webext-content-scripts/issues"},"license":"MIT","readme":"# webext-content-scripts [![npm version](https://img.shields.io/npm/v/webext-content-scripts.svg)](https://www.npmjs.com/package/webext-content-scripts)\n\n> Utility functions to inject content scripts in WebExtensions, for Manifest v2 and v3.\n\n- Browsers: Chrome, Firefox, and Safari\n- Manifest: v2 and v3\n- Permissions: In manifest v3, you'll need the `scripting` permission\n- Context: They can be called from any context that has access to the `chrome.tabs` or `chrome.scripting` APIs\n\n**Sponsored by [PixieBrix](https://www.pixiebrix.com)** :tada:\n\n## Install\n\nYou can download the [standalone bundle](https://bundle.fregante.com/?pkg=webext-content-scripts&name=window) and include it in your `manifest.json`. Or use npm:\n\n```sh\nnpm install webext-content-scripts\n```\n\n```js\n// This module is only offered as a ES Module\nimport {\n\texecuteScript,\n\tinsertCSS,\n\tinjectContentScript,\n\texecuteFunction,\n} from 'webext-content-scripts';\n```\n\n## Usage\n\n### `executeScript`\n\nLike `chrome.tabs.executeScript` but:\n\n- it works on Manifest v3\n- it can execute multiple scripts at once\n\n```js\nexecuteScript({\n\ttabId: 1,\n\tframeId: 20,\n\tfiles: ['react.js', 'main.js'],\n});\n```\n\n```js\nexecuteScript({\n\ttabId: 1,\n\tframeId: 20,\n\tfiles: [\n\t\t{file: 'react.js'},\n\t\t{code: 'console.log(42)'}, // This will fail on Manifest v3\n\t],\n});\n```\n\n### `insertCSS`\n\nLike `chrome.tabs.insertCSS` but:\n\n- it works on Manifest v3\n- it can insert multiple styles at once\n\n```js\ninsertCSS({\n\ttabId: 1,\n\tframeId: 20,\n\tfiles: ['bootstrap.css', 'style.css'],\n});\n```\n\n```js\ninsertCSS({\n\ttabId: 1,\n\tframeId: 20,\n\tfiles: [\n\t\t{file: 'bootstrap.css'},\n\t\t{code: 'hmtl { color: red }'}\n\t],\n});\n```\n\n### `injectContentScript(targets, scripts)`\n\nIt combines `executeScript` and `injectCSS` in a single call. You can pass the entire `content_script` object from the manifest too, without change (even with `snake_case_keys`). It accepts either an object or an array of objects.\n\n### targets\n\nThis can be a tab ID, an array of tab IDs, a specific tab/frame combination, an array of such combinations:\n\n```js\ninjectContentScript(1, scripts);\ninjectContentScript([1, 2], scripts)\ninjectContentScript({tabId: 1, frameId: 0}, scripts);\ninjectContentScript([{tabId: 1, frameId: 0}, {tabId: 23, frameId: 98765}], scripts);\n\n// You can also use the exported `getTabsByUrl` utility to inject by URL as well\ninjectContentScript(await getTabsByUrl(['https://example.com/*']), scripts);\n```\n\n### Examples\n\n```js\nconst tabId = 42;\nawait injectContentScript(tabId, {\n\trunAt: 'document_idle',\n\tallFrames: true, // Default when passing frame-less tab IDs\n\tmatchAboutBlank: true,\n\tjs: [\n\t\t'contentscript.js'\n\t],\n\tcss: [\n\t\t'style.css'\n\t],\n})\n```\n\n```js\nawait injectContentScript({\n\ttabId: 42,\n\tframeId: 56\n}, [\n\t{\n\t\tjs: [\n\t\t\t'jquery.js',\n\t\t\t'contentscript.js'\n\t\t],\n\t\tcss: [\n\t\t\t'bootstrap.css',\n\t\t\t'style.css'\n\t\t],\n\t},\n\t{\n\trunAt: 'document_start',\n\t\tcss: [\n\t\t\t'more-styles.css'\n\t\t],\n\t}\n])\n```\n\n```js\nconst tabId = 42;\nconst scripts = browser.runtime.getManifest().content_scripts;\n// `matches`, `exclude_matches`, etc are ignored, so you can inject them on any host that you have permission to\nawait injectContentScript(tabId, scripts);\n```\n\n### `executeFunction(tabId, function, ...arguments)`\n\n### `executeFunction({tabId, frameId}, function, ...arguments)`\n\nLike `chrome.tabs.executeScript`, except that it accepts a raw function to be executed in the chosen tab.\n\n```js\nconst tabId = 10;\n\nconst tabUrl = await executeFunction(tabId, () => {\n\talert('This code is run as a content script');\n\treturn location.href;\n});\n\nconsole.log(tabUrl);\n```\n\nNote: The function must be self-contained because it will be serialized.\n\n```js\nconst tabId = 10;\nconst catsAndDogs = 'cute';\n\nawait executeFunction(tabId, () => {\n\tconsole.log(catsAndDogs); // ERROR: catsAndDogs will be undeclared and will throw an error\n});\n```\n\nyou must pass it as arguments:\n\n```js\nconst tabId = 10;\nconst catsAndDogs = 'cute';\n\nawait executeFunction(tabId, (localCatsAndDogs) => {\n\tconsole.log(localCatsAndDogs); // It logs \"cute\"\n}, catsAndDogs); // Argument\n```\n\n### `isScriptableUrl(url)`\n\nBrowsers block access to some URLs for security reasons. This function will check whether a passed URL is blocked. Permissions and the manifest are not checked, this function is completely static. It will also returns `false` for any URL that doesn't start with `http`.\n\nMore info may be found on:\n\n- https://stackoverflow.com/q/11613371/288906\n- https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts\n\n```js\nconst url = 'https://addons.mozilla.org/en-US/firefox/addon/ghosttext/';\nif (isScriptableUrl(url)) {\n\tconsole.log('I can inject content script to this page if permitted');\n} else {\n\tconsole.log('Content scripts are never allowed on this page');\n}\n```\n\n## Related\n\n- [webext-tools](https://github.com/fregante/webext-tools) - Utility functions for Web Extensions.\n- [webext-domain-permission-toggle](https://github.com/fregante/webext-domain-permission-toggle) - Browser-action context menu to request permission for the current tab.\n- [webext-additional-permissions](https://github.com/fregante/webext-additional-permissions) - Get any optional permissions that users have granted you.\n- [webext-dynamic-content-scripts](https://github.com/fregante/webext-dynamic-content-scripts) - Automatically registers your content_scripts on domains added via permission.request\n- [More…](https://github.com/fregante/webext-fun)\n\n## License\n\nMIT © [Federico Brigante](https://fregante.com)\n","readmeFilename":"readme.md"}