-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: update to chrome extension manifest version 2 -> 3 #8
Conversation
* Update spacing * add new property host_permissions https://developer.chrome.com/docs/extensions/develop/migrate/manifest
* Error: Invalid value for 'content_security_policy'"? * Solution: https://stackoverflow.com/questions/67130826/why-am-i-getting-failed-to-load-extension-invalid-value-for-content-security
Note: https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers Instead, move the event listener registration to the top level of your script. This ensures that Chrome will be able to immediately find and invoke your action's click handler, even if your extension hasn't finished executing its startup logic. |
Note: There are several problems. chrome://extensions UI is misleading you by showing old errors without indicating they're old before you changed and fixed your background script. You'll have to account for this poor design and click the "Clear all" button every time you reload the extension. The background script (the service worker) doesn't have ManifestV3 extensions can't use inline code in Note: Using jQuery in service worker manifest V3 Correct. As you noted, document is not exposed in the service worker's global scope so that variable is undefined. As I see it you have two basic options.
Personally I'd lean towards removing the jQuery dependency, but depending on how heavily it's being used that may be a lot of work. If you go the shim route, try looking for resources that describe how to use jQuery inside non-document JavaScript contexts, like this blog post about using jQuery inside a WebWorker. Alternatively, you could try using a DOM library to shim the missing document object. I'd probably start by experimenting with linkedom, but note that that library doesn't come in a nicely packaged, single-file format.
===
Removing the jQuery dependency might be too big a task. The offscreen document isn't visible so running jQuery there also doesn't make a lot of sense unless you want to use it to scrape a web site inside an iframe. As for an example see the folders with "offscreen" in https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples |
Note: Manifest V3 service worker registration failed Even better, you can get the same result without changing your background script at all! To do this, create a wrapper script that imports your actual background script & surfaces the errors. // manifest.json // background-wrapper.js // background.js (also for the record I think the platform should just do this for you) |
note: chrome extension how can service worker communicate with popup javascript You're sending a message to the tab but there's nothing that listens to it. You need a content script with onMessage listener. Note that there's no essential difference between MV2 and MV3 so you can learn MV2 and then read the migration article without paying any attention to the mostly irrelevant promotion of service workers as they're essentially the same as the non-persistent background scripts minus DOM and a few other features. See also this. My comment above assumed you want to send a message directly to the content script because this is what @wOxxOm A step in the right direction! No more errors when I try and send a message from the popup, but no console logs are being reported? However I still have the issue with the "Service worker registration failed" warning? It seems strange that Google's example has that warning out of the box?! |
… code into popup.js
Note: chrome.runtime API Its common for an extension's content scripts to need data managed by another part of the extension, like the service worker. Much like two browser windows opened to the same web page, In this example, the content script needs some data from the extension's service worker to initialize its UI. To get this data, it passes the developer-defined get-user-data message to the service worker, and it responds with a copy of the user's information. |
Note: You are trying to use chrome.tabs.create in a content script. This API is only available for background scripts. Instead, use window.open to create new tabs from within a content script.
For the options page, this does not work since it uses the chrome-extension protocol, which Firefox does not support using window.open. This is the way I open my options page in a background script:
If you need to call it in a content script, use messaging: https://developer.chrome.com/apps/messaging Example: background.js
content_script.js |
* added messaging in popup.js for service_worker to receive for chrome APIs that only service_worker can use
…ng value * everything working for key setup, hardToDeactivate clicks * did not test youtube blocking yet
Note: other notes used https://stackoverflow.com/questions/49192636/how-can-i-open-my-options-html-currently-i-get-cannot-read-property-create-of/49192949#49192949 |
Note:
|
Note: Question: Can you send notifications through contentScript? https://stackoverflow.com/questions/3536621/desktop-notifications-from-content-scripts |
Changes
Testing
Problem
Issue: #7
Notes
Search for
Note
in this PR