How to refactor global variables from MV2 to using chrome.storage in MV3 service worker?

The number of variables in the state doesn’t change the approach: read the state on the start of the script save the state on change For small data (1MB total) use chrome.storage.session, which is in-memory i.e. it doesn’t write to disk, otherwise use chrome.storage.local. Both can only store JSON-compatible types i.e. string, number, boolean, null, … Read more

executeScript is undefined or not a function in a ManifestV3 extension

The executeScript method in ManifestV3 has changed and is now in chrome.scripting API: https://developer.chrome.com/docs/extensions/reference/scripting/ Add this line in manifest.json: “permissions”: [“scripting”] background.js chrome.scripting.executeScript({ target: {tabId: id, allFrames: true}, files: [‘content_scripts/cscript.js’], });

How to store sentitive data in Chrome extension Manifest V3

Use chrome.storage.session, which is created for this exact purpose: to store variables in memory without persisting to the disk. The API is the same as any other chrome.storage API, so the data must be JSON-compatible: string, number, boolean, null, array/object of these types. The maximum capacity of the storage is currently 1MB. async function foo() … Read more

Getting selected text in a Chrome extension

Remove content.js from index.html. Content scripts are for web pages, not for extension pages such as the popup. Create index.js and load it in index.html: <script src=”index.js”></script> </body> index.js: document.getElementById(“save-btn”).onclick = async () => { const [tab] = await chrome.tabs.query({active: true, currentWindow: true}); let result; try { [{result}] = await chrome.scripting.executeScript({ target: {tabId: tab.id}, function: … Read more

Chrome Extension: How do I inject a script that the user provided?

Executing arbitrary user code (userscripts) isn’t yet implemented in ManifestV3 and is still forbidden by the policies of Chrome’s Web store for extensions. The personal workaround (e.g. in an unpacked extension) is to run such code in the page context i.e. not as a content script: async function execInPage(code) { const [tab] = await chrome.tabs.query({currentWindow: … Read more

How do I import scripts into a service worker using Chrome extension manifest version 3?

First off, important warnings: Warning: Chrome 92 or older doesn’t show errors occurred in the service worker – it was a bug, fixed in newer Chrome, which now shows the errors in chrome://extensions page. These old versions of Chrome can’t register the background script if an unhandled exception occurs during its compilation (a syntax error … Read more

chrome extension mv3 – Modularize service worker js file

First off, important warnings: Warning: Chrome 92 or older doesn’t show errors occurred in the service worker – it was a bug, fixed in newer Chrome, which now shows the errors in chrome://extensions page. These old versions of Chrome can’t register the background script if an unhandled exception occurs during its compilation (a syntax error … Read more