How do I auto load script when a page loads?
I have this extension source:
{ "browser_action" : { "default_icon" : "icon.png"},
"description" : "Alert on Google Load",
"icons" : { "128" : "icon.png" },
"name" : "Auto alert",
"version" : "1.0",
"content_scripts": [
{
"matches": ["http://www.google*"],
"js": ["myscript.js"],
"run_at": ["document_end"]
}
],
}
Now myscript.js includes:
alert("hi")
But when i load google.com, nothing happens.
thanks alot.
The match pattern you are using (http://www.google*) is invalid. You can use the wildcard character (*) as scheme, host or path parts of URL. You can not use it as a part of domain.
If you want to match all Google sites, you should use this pattern:
*://*.google.com/*
You can read about match patterns, including good and bad examples, in the documentation at: http://developer.chrome.com/extensions/match_patterns.html
Related
I'm just publishing a Google Chrome extension and I run into the error message: "Because of the following issue, your extension may require an in-depth review:
- Broad host permissions"
And whilst I know why this is (see snippet of manifest code) I wonder what the best way to fix it is?
I know this is to do with the wildcards I'm using and I have now published the extension with specific websites to test it works as intended (it does). But for the extension to be truly effective I want it to work more universally.
"content_scripts" : [{
"matches" : [ "http://*/*","https://*/*" ],
"exclude_globs" : [ "*.jpg","*.jpeg","*.gif","*.png","*.bmp","*.webp",
"*.css","*.swf","*.js","*.zip","*.rar","*.tar","*.tar.gz","*.tgz","*.tar.bz2","*.tbz2",
"*.doc","*.docx","*.ppt","*.pptx","*.xls","*.xlsx","*.pdf","*.xml","*.txt",
"*.mpg","*.mpeg","*.qt","*.mp4","*.m4v","*.m4a","*.mp3","*.ogv","*.ogm","*.ogg","*.oga","*.webm","*.wav",
"*.","*." ],
"css" : [ "css/inject.css" ],
"js" : [ "js/jquery.js" , "js/inject.js" ],
"run_at" : "document_start"
}],
"web_accessible_resources" : [
"html/iframe/*",
"src/options/options.html"
],
"permissions" : [
"tabs"
"activeTab",
"webRequest",
"http://*/*",
"https://*/*",
"storage"
]
}
I'm trying to publish my chrome extension but it's appears and i don't use permissions for active tab
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"] } If your extension only needs to
run on certain sites, simply specify those sites in the extension
manifest: { ... "permissions": ["https://example.com/*"] }
My Manifest litterally don't have activeTab permissions
{
"name" : "AZSolusindo VPN",
"description" : "VPN Solusindo",
"permissions" : [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking",
"http://azsolusindo.info/*",
"http://127.0.0.1/*"
],
"background" : {
"scripts" : [
"background.js"
]
},
"browser_action" : {
"default_icon": {
"16" : "images/vpnoff.png",
"32" : "images/vpn32.png",
"48" : "images/vpn48.png",
"128" : "images/vpn128.png"
},
"default_title": "AZ VPN",
"default_popup": "popup.html"
},
"commands": {
"_execute_browser_action": {
"suggested_key": {
"default": "Alt+Shift+O",
"mac": "Alt+Shift+O",
"chromeos": "Alt+Shift+O",
"linux": "Alt+Shift+O"
},
"description": "Open Pop Up."
}
},
"content_scripts": [
{
"matches": [
"http://azsolusindo.info/vpn/index.php/template",
"http://127.0.0.1/AzVPN/public/template"
],
"js": ["JS/content.js"],
"run_at": "document_end"
}
],
"version" : "0.9.5.1",
"manifest_version" : 2
}
Is there anything i need to change ?
What’s getting flagged is your <all_urls> permissions entry. The message is saying that requesting such broad permissions is going to require a more in-depth review for your extension.
If you only need host permissions in a few places, then declare the specific hosts in your permissions array. As the message suggests, you can also use the activeTab permission if all you need is tab-related information and lifecycle events (such as tab id, URL, etc.).
If you do in fact potentially need full permissions on any given webpage, then there’s not much you can do here - your extension will need to go through the extended review.
I thought this would be a regularly asked question, but I can not find the answer for it.
Is there a way for a chrome extension to have it's browser action popup available only on content_scripts matching pages?
Eg: I make an extension for a site. I want the popup to only be available when browsing the site (on the current tab). How do I do?
For me the browser action is always enabled by default, whatever page I'm browsing, and page action never enabled.
Thanks for your help
EDIT: Here is part of the manifest, I changed with pageAction with No success. I want my icon to activate only on tabs browsing LDLC.
{
"content_scripts": [
{
"js": [ "jquery.min.js", "scrooge.js" ],
"matches": [ "*://www.ldlc.com/fiche/*", "*://ldlc.com/fiche/*" ]
}],
"manifest_version": 2,
"permissions": [ "storage", "activeTab","*://ldlc.com/*", "*://scroogealpha.esy.es/*" ],
"page_action": {
"default_title": "Worth buying?", // optional; shown in tooltip
"default_popup": "popup.html" // optional
},
"web_accessible_resources": [
],
"background":{
"scripts" : ["eventPage.js"],
"persistent": false
}
}
Use a page action and in your background script listen for tab changes. When a tab url changes, check if you want to show the page action.
In your background script do something like this:
let matches = ["://www.ldlc.com/fiche/", "://ldlc.com/fiche/" ]
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
for (let i in matches) {
if (tab.url.includes(matches[i])) {
chrome.pageAction.show(tabId)
break
}
}
})
Also add the tabs permission in your manifest.
I am looking for simple solution of my problem.
Pressing ctrl+alt+lmb should fire my script and I can achieve that using content_scripts, but content_scripts does not have access to chrome.*.
Any suggestions?
manifest.json
{
"manifest_version": 2,
"version": "1.0",
"name" : "Incognito Shortcut",
"content_scripts" : [
{
"matches" : ["*://*/*"],
"js" : ["core.js"],
"run_at" : "document_end",
"all_frames" : true
}
],
"permissions" : [
"tabs"
]
}
core.js
document.addEventListener("click", function(e) {
if(e.altKey && e.ctrlKey) {
// do stuff
e.preventDefault();
}
}, false);
Standard approach in such situation, is that you'll have to pass messages from content script to the background page (which has access to the most of chrome api's). E.g. content script will send message to the background-page and background page will call some appropriate chrome api
If you will read articles Content-Scripts and Message Passing from official documentation, you should be able to implement it easily.
I've tried browsing through similar questions posted here, but none seems to work
Manifest.json
{
"manifest_version": 2,
"name" : "A simple Found Text Demo",
"description" : "Bla",
"version" : "1.0",
"background" : {
"pages" : "background.html"
},
"page_action" : {
"default_icon" : "icon.png"
},
"content_scripts" : [{
"matches" : ["*://*/*"],
"js" : ["contentscript.js"]
}]
}
Background.html
<html>
<script>
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
alert(request);
//chrome.pageAction.show(sender.tab.id);
sendResponse('Found!');
}
)
</script>
</html>
Contentscript.js
chrome.extension.sendMessage({"name" : "hola"}, function(res){
console.log(res); })
However I repeatedly get the same error :
Port error: Could not establish connection. Receiving end does not exist.
Any ideas?
Since things changed over to manifest 2, you are actually no longer allowed to use in-line scripts (such as what you have in your background.html in the <script> tags above. See here). I'm not sure of your use case, but in most cases simple cases (read: the stuff I've done :) ), you don't actually need to populate background.html with anything. Instead, you can directly pass in a background.js file that will contain the same script you have above. Therefore you can try changing your manifest.json to this:
{
"manifest_version": 2,
"name" : "A simple Found Text Demo",
"description" : "Bla",
"version" : "1.0",
"background" : {
"scripts" : ["background.js"]
},
"page_action" : {
"default_icon" : "icon.png"
},
"content_scripts" : [{
"matches" : ["*://*/*"],
"js" : ["contentscript.js"],
"run_at": "document_end"
}]
}
Note we did two things here - changed pages to scripts inside of background and pointed it to ["background.js"], and then added "run_at": "document_end" to the end of the content_scripts section. This is something that can definitely cause issues if left out (issues similar to what you are seeing now) - you are now telling the content script to run after the page has loaded. If it runs immediately, you run the risk of the background page not having loaded, which means it isn't yet ready to receive messages and gives you the connection error. Below is background.js, which is identical to the script you had in between your <script> tags before:
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
alert(request);
//chrome.pageAction.show(sender.tab.id);
sendResponse('Found!');
}
)