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.
Related
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.
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 am creating a chrome extension that adds an action with a global hotkey to JIRA. I can hard code the url for my own instance of JIRA into my extension, but I would like this url to be user configurable as other users will have different urls for their own JIRA instances.
I would like to know if there is a better way of doing this than just giving my extension permission for all urls and checking in my background script to compare the current url to the one the user has chosen as a setting. Ideally my background/inject script would only run on the users chosen url. I looked at the permissions api but can't figure it out.
My current manifest.json looks as follows, I have hard coded permission to my jira url, is there a way to make this permission user configurable?
{
"permissions": [
"contentSettings",
"storage",
"commands",
"http://myjiraurl/*"
],
"background": {
"matches": [
"http://myjiraurl/*"
],
"scripts": ["src/background/background.js"]
},
"content_scripts": [
{
"matches": [
"http://myjiraurl/*"
],
"js": [
"js/jquery.min.js",
"src/inject/inject.js"
]
}
]
}
Whatever you do, in order to inject into arbitrary hosts, you have to have http://*/ and https://*/ in your permissions, preferably optional permissions. Your proposal - comparing the current url with the one in the settings - sounds very reasonable: Just compare the current url with the one in the setting and make a chrome.permissions.request if it matches, silently stop running your code if it fails.
I can think of one work-around to avoid that, but honestly it's not giving you much:
Use http://*/ and https://*/ as optional permissions (you should really really make them optional anyway).
Make the chrome.permissions.request call at the time the user is modifying the options.
In your background script, make a chrome.permissions.contains call for the current url and silently stop running your code if there's no permission.
The result of the above is that instead of comparing the current url to the one in the setting, it will compare the current url with whatever the extension has previously got permission for. I guess it simplifies the background script a bit (you don't have to get the url stored in the settings), but makes the options page code a bit more complicated.
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 */
});
I am working on my first extension and am trying to create a simple extension to inject a draggable div on a page. That works nicely, but I want to preserve the location of the div on the background page (I'm also trying out local storage, but want to understand why this isn't working).
I do not need a button so have not created a popup.html file, which, I believe, is entirely optional. It certainly has worked so far just injecting javascript files.
However, I now get the following error thrown when executing chrome.extension.getBackgroundPage():
Uncaught Error: chrome.extension.getBackgroundPage can only be used in extension processes. See the content scripts documentation for more details.
The content scripts documentation did not seem to identify anything wrong with my approach: http://code.google.com/chrome/extensions/content_scripts.html
Here is a redacted manifest I am using:
{
"name": "My helper",
"version": "1.0",
"description": "Tastes great",
"background_page": "background.html",
"content_scripts": [
{
"matches":["https://page.of.interest/*"],
"run_at":"document_idle",
"js":[ "jquery.js", "jquery-ui-1.8.17.custom.min.js", "my_content_script.js"],
"css": [ "my_content_script.css" ]
}
],
"permissions": [
"background"
]
}
So I am running this statement inside "my_content_script.js". Is this NOT considered part of the extension process? Can I only run this on a popup.html (or other possibly?) file?
If this is the case, then maybe it is easier to just use localstorage rather than trying to communicate through the dom with the extension process.
Hope I've been clear despite my ignorance about some of these concepts.
I don't think the docs explicitly say you can't use chrome.extension.getBackgroundPage() from a content_script but because the content_script has permissions more oriented with the page it is being run on it isn't allowed access. The docs do mention a few methods you can use however.
Unlike the other chrome.* APIs, parts of chrome.extension can be used by content scripts:
You will have to use message passing to communicate between the background_page and the content_script.
You have to use the chrome.cookies.get() in background.html, and then do the communication between your content script and background.html for exchanging cookie data.