execute background scripts only once in my chrome extension - google-chrome-extension

I am developing a chrome extension. My scripts are called everytime I reload a page or open a new tab. How I do make it to load the extension only once, i.e., when chrome starts and reset all values to default on browser close.
I did go through most of the links available here in stackoverflow but was unable to focus it down to my situation.
manifest.json
"background":{
"scripts":["common.js","example.js"],
"persistent":true
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["myscript.js"]
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"permissions": ["http://*/*",
"https://*/*",
"contextMenus",
"tabs"]
And in one of my background javascripts, I am triggering an event:
example.js
var DOMContentLoaded_event = document.createEvent("Event");
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true);
window.document.dispatchEvent(DOMContentLoaded_event);
The above code is to trigger DONContent so that, the user need not click on the extension image everytime he boots the Chrome browser.
The trigger event is getting called each time my page loads, whether it is a reload of the
same page or open another tab, the event is getting called. I know I am missing something major here. I did setting things in localStorage. Did NOT work(I mean, the event gets called on refresh of a webpage). I did try the "persistent": true option but in vain.
Can I know what I am missing?
Nikhil

As per your comments, you are trying to register a listener for when Chrome starts and loads your extension. You can achieve this with the chrome.runtime.onStartup event:
Fired when a profile that has this extension installed first starts up. This event is not fired when an incognito profile is started, even if this extension is operating in 'split' incognito mode.
It is as simple as:
chrome.runtime.onStartup.addListener(function () {
/* Do some initialization */
});

Related

how can i get the local file in chrome extension v3 [duplicate]

I have a Chrome extension that can (if you allow access to file URLs) grab your local pdf file that you have open in chrome and send it on to our API for processing. This is done by fetching the pdf with XMLHttpRequest to file:///Users/user/whatever/testfile.pdf from the background script.
When migrating to manifest v3 for a Chrome extension the background script becomes a service worker. In a service worker only fetch is available, not XMLHttpRequest. Problem is, fetch only supports http and https, not file:// urls. So how can I make the same feature of having the Chrome extension fetching/getting the local file?
EDIT: Things I also tried:
Making the XMLHttpRequest from injected iframe as suggested by answer.
This gives error net:ERR_UNKNOWN_URL_SCHEME when making the request
Making the XMLHttpRequest from injected content script.
This gives error Access to XMLHttpRequest at 'file:///.../testfile1.docx.pdf' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
From what I can understand from a lot of research access to file:// is in general blocked and Chrome extension background scripts used to be an exception to this. Seems to me it was never allowed from content scripts or action popups.
My manifest.json for reference:
{
"manifest_version": 3,
"name": "..",
"version": "0.1",
"icons": {
"16": "assets/icon-16x16.png",
"48": "assets/icon-48x48.png",
"128": "assets/icon-128x128.png"
},
"action": {
"default_title": ".."
},
"background": {
"service_worker": "background.js"
},
"permissions": [
"webRequest",
"activeTab",
"scripting",
"storage",
"unlimitedStorage",
"identity",
"pageCapture"
],
"host_permissions": [
"<all_urls>"
],
"web_accessible_resources": [{
"resources": ["iframe.html"],
"matches": [],
"extension_ids": []
}]
}
The content script is injected programmatically (using webextension-polyfill for promise support)
browser.action.onClicked.addListener(async (tab: Tab) => {
await browser.scripting.executeScript({files: [ "inject.js" ], target: {tabId: tab.id}});
});
Chrome 98 and older can't do it in the background service worker for the reasons you mentioned.
There was also a bug that prevented doing it in a normal visible chrome-extension:// page or iframe. It's fixed in Chrome 91.
Solution
Use fetch in Chrome 99 and newer.
In older versions use the following workarounds.
Workaround 1: File System API, Chrome 86+
A ManifestV3 extension can use the new File System API to read the contents of the file, for example inside the iframe exposed via web_accessible_resources.
Workaround 2. Extension frame, Chrome 91+
Use a content script that runs in the tab with that pdf:
matches in manifest.json should contain <all_urls> or file://*/* and file access should be enabled by the user in
chrome://extensions UI for your extension. Alternatively you can use
activeTab permission and programmatic injection when the user
clicks your extension's icon or invokes it through the context menu.
The content script adds an invisible iframe that points to iframe.html file exposed in web_accessible_resources
The iframe.html loads iframe.js which uses XMLHttpRequest as usual. Since the iframe has chrome-extension:// URL its environment
is the same as your old background script so you can do everything
you did before right there.
Workaround 3. Extension window/tab, Chrome 91+
An alternative solution is to use any other visible page of your
extension like the action popup or options page or any other
chrome-extension:// page belonging to your extension as they can
access the file:// URL via XMLHttpRequest same as before.
Notes
File access should be enabled in chrome://extensions page for this extension.

Extension: Use runtime.sendMessage/port.postMessage with only activeTab permission?

I'm reading about message passing between extension and webpage, and I have a question about permissions.
My use case is: I need to communicate with all webpages, but only the active one. On the webpage, when the user clicks on a button "[Connect with my Extension]", it sends a message to the extension. What I'm doing now, is I'm injecting a content_script inside all the webpages:
// manifest.json snippet
"permissions": ["storage"],
"content_scripts": [{
"js": ["content.js"],
"matches": ["http://*/*", "https://*/*"],
"run_at": "document_start"
}],
and content.js does chrome.runtime.sendMessage/chrome.runtime.onMessage with the background. It works, but when I publish my extension, Chrome says:
Because of the following issue, your extension may require an in-depth review:
Broad host permissions
Instead of requesting broad host permissions, consider using the
activeTab permission, or specify the sites that your extension needs
access to. Both options are more secure than allowing full access to
an indeterminate number of sites, and they may help minimize review
times.
The activeTab permission allows access to a tab in response to an
explicit user gesture.
{ ... "permissions": ["activeTab"] }
My question is: is there a way to achieve what I want by using activeTab only, as Chrome suggests?
My initial understanding is that NO. activeTab is only activated on some specific user interactions, whereas I would need to activate it on button click inside the webpage. So my only hope is to battle with Chrome's "in-depth reviews". Is that right?
Thanks.

How to make write a chrome extension which is always enabled for any urls?

I'm writing a chrome extension, to do some modification to the page content, but I have to click on it to make it working on current page.
What I want is: if I click on the extension icon (to enable it), it will always enabled, no matter what new pages/tabs are open, and will work on them?
How to write code to configure it?
Using content script you can achieve this. Content scripts are files that run in the context of web pages so you can modify webpage content.
Add below code in manifest.json
{
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["your-content-script.js"]
}
],
"permissions": [
"tabs", "http://*/*","https://*/*"
]
}
To learn more, read this https://developer.chrome.com/extensions/overview.html#arch

I can't activate a custom chrome content script extension outside of developer mode

I am having problem activating my own custom made script. It's working in the developer mode, when I load it as a directory. But when I pack it using the method provided by chrome it does not work.
It creates a .pem and .crx file, and when I drag and drop the extension inside the chrome://extensions page it shows. When pressing f5 it is deactivated every time, and when I check "also allow in incognito mode", that checkbox disappears immediately and doesn't come back.
Below my manifest file contents:
{
"manifest_version": 2,
"name": "Test script",
"version": "0.1",
"incognito": "split",
"content_scripts": [
{
"matches": [
"http://mail.google.com/*",
"https://mail.google.com/*",
"https://accounts.google.com/*"
],
"js": [
"jquery-2.1.4.min.js",
"content.js"
]
}
]
}
Any clue what's happening?
Chrome blocks extension installs on Windows unless they come from Chrome Web Store.
Install Chrome from a developer channel or use another OS.

Background script only for specific domain in Chrome extension

I would like to know how to run the background script of a Chrome extension for only specific/specified domain/s please?
For example, if an extension is meant to run only on pages of Google.com, so there is no reason to keep the background script running on any other domains.
In my manifest file I have set "matches" but I can still see the background script running on every domain and tab.
...
"background": {
"matches": [
"*://*.google.com/*"
],
"scripts": ["scripts/background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": [
"*://*.google.com/*"
],
"css": ["styles/default.css"],
"js": ["scripts/jquery.js", "scripts/default.js"]
}
],
"permissions": [
"storage",
"declarativeContent",
"tabs"
],
...
Edit
To add more info that will help understand my goal:
I have a content script and a popup, the popup is being used as a remote control with options to choose from and play with, that will eventually effects the page.
When changing an option in the popup, it sends it's value to the background/event script, there it being temporary saved in a variable, and then being sent to the content script, where it actually being executing and show up on the page to the user.
And I want that only when the user leaves the specific domain, the background/event script will save the settings to storage, so by that there will be only a single storage saving task and NOT each time the user is changing a setting in the popup.
After the settings got saved to storage and the user left, I want nothing to run it the background anymore please.
You are misunderstanding what the background page is for, and what "persistent": false means.
The background page does not run "for" any domain; it just runs. A single copy per extension. Have a look at the Architecture Overview.
However, if you are concerned that it consumes resources, you add "persistent": false to the manifest. This makes it an Event page, that is unloaded when it's not doing work.
If your event page is woken up only by content scripts, then you have achieved your goal: it won't be running when it's not needed.
It's entirely up to you to properly construct the background page so it's idle when you don't need it. Since you haven't told what it's supposed to be doing - well..
Do read the Event page documentation, there are important restrictions you need to understand.

Resources