chrome extension - manifest version 2 - google-chrome-extension

I have a chrome extension that has a reference to the jquery file.
this is my popup html (only the head tag):
<head>
<title>My Extention</title>
<script type="text/javascript" src="http://www.MySite.com/Resources/JS/JQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="MyExtensionScript.js"></script>
</head>
so in "MyExtensionScript.js" i thought i could use jquery but apparently the $ function is not defined.
This is my manifest.json file:
{
"name": "My Test Extension",
"version": "1.0",
"manifest_version": 2,
"description": "Test version of My Extension",
"browser_action": {
"default_icon": "test.ico",
"default_popup": "Test.html"
},
"permissions": [
"cookies",
"tabs",
"<all_urls>"
]
}
in version 1 of the manifest it worked, but now it doesn't. I tried to use the "web_accessible_resources" and add to them "http://www.MySite.com/Resources/JS/JQuery/jquery-1.7.2.min.js" but that didn't work also. any ideas?
also, i have a script injected to the current page and returning me a message (in my case some html source of the current page), will this behavior be affected by the transition to manifest version 2?
Thanks all :)
EDIT: I have a web application that enables cross domain scripting (using JSONP). In my extension i had a script calling a web service in my site with $.getJSON. now it doesn't work. i'm pretty sure that it has to do with the new manifest version but how can i enable again the cross domain scripting?

You need to use a jQuery file stored locally in your extension, rather than referenced from your site.
This is due to Chrome's strict Content Security Policy that only allows local scripts to be executed, with no inline code.
Web Accessible Resources are files inside your extension that may be accessed from the web, rather that resources your extension can access that are on the web. For example, if you wanted to change the background image of a page using an image stored in the extension, you have to add that image to the list in web_accessible_resouces in your manifest.
The change of manifest version should not affect your content scripts, unless they do something that is no longer allowed. You can see what else has changed from the Chrome manifestVersion docs.

I just include jquery in my content scripts. just make sure to load it before your script.
{
"manifest_version": 2,
"default_title": "Babble",
"version": "1.2",
"description": "Chat in your language with friends in their language",
"default_locale": "en",
"default_icons": {
"16": "img/icon16.png",
"48": "img/icon48.png",
"128": "img/icon128.png"
},
"content_scripts":[
{
"matches": ["http://mail.google.com/*", "https://mail.google.com/*"],
"css" : ["css/style.css"],
"js" : ["js/jquery.js" , "js/translate.js" , "js/jquery.cookie.js"]
}
]
}

Related

Chrome extension injection blocked by sandbox CSP

I am currently working on a Chrome extension which has a script that needs to access global properties of the page. To do so, I came up with an approach very similar to Method 1 in this Stack Overflow answer, the only difference being that instead of inserting a new script element directly in the document, the extension creates an ìframe with a src attribute pointing to a HTML document that already contains such a script tag. This approach is convenient in my use case as this newly created iframe and HTML document are also used for other purposes, but this is not relevant to the minimal reproducible example showcased here.
Here's the code responsible for creating the iframe, executed via a content script content.js:
const iframe = document.createElement("iframe");
iframe.setAttribute("src", chrome.runtime.getURL("inject.html"));
document.body.insertBefore(iframe, document.body.firstChild);
Here is a simplified version of inject.html:
<!DOCTYPE html>
<html>
<body>
<script src="inject.js"></script>
</body>
</html>
Here is inject.js (which in reality contains code accessing the global properties of the page):
console.log("Loaded!");
Finally, manifest.json sets both inject.js and inject.html as web accessible resources and injects content.js in all pages:
{
"name": "Example",
"version": "0.0.1",
"author": "Pyves",
"content_scripts": [
{
"all_frames": true,
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
],
"web_accessible_resources": [
"inject.html",
"inject.js"
],
"manifest_version": 2
}
The extension is in particular targeting GitHub raw pages (for instance https://raw.githubusercontent.com/PyvesB/JavAssembly/master/README.md), which happen to have a sandbox Content Security Policy. So far this setup has been working like a charm, however since updating my Chromium-based browsers from version 63 to 64 earlier this month, the extension is no longer fully functional. inject.js is blocked, with the following error message in the browser's console:
Blocked script execution in 'chrome-extension://xxxx/inject.html' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
Am I missing something here? How can I once again inject extension scripts in sandboxed frames in Chromium 64+?

open all the links on a web page with a single click on Chrome Extensions

I want open all the links on a web page with a single click on Chrome Extensions. I try for that وbut I did not get any results. please help me ...
my manifist.json file :
{
"name": "Open Links in New Tabs",
"version": "1.0",
"description": "open all link in page on new tabs with one click",
"background_page" : "background.html",
"browser_action": {
"default_icon": "icon.png"
},
"permissions":
["tabs"],
"manifest_version": 2
}
background.html file :
<!doctype html>
<html>
<head>
<script src="background.js"></script>
</head>
<body>
</body>
</html>
background.js file :
for(i = 0; i < document.links.length; i++)
{ chrome.tabs.create({active: true, url: document.links[i].href});}
Please explain any problems. thanks
When you access the document object in background.js, you're looking at background page, not the currently active tab.
To access the DOM contents of the open page, you will need a content script. It can then send a message to the background script (which has access to chrome.tabs) with the list of links to open.
Please take a moment to read this excellent overview of extension architecture. Also, read on messaging to pass the list.
Actually, if you're going to be injecting the script programmatically via chrome.tabs.executeScript, you can ditch messaging and just use the callback of executeScript.
As a side note, since you don't have any HTML in the background page, you can replace it with an auto-generated script page like this:
manifest.json
"background": {
scripts": ["background.js"]
},
in place of "background_page"
Lastly, consider minimal permissions that you need.
If your extension is supposed to trigger on click on the extension button (wrapped in a chrome.browserAction.onClicked listener), you can inject a script "for free" with activeTab permission, no need for tabs/host pemission.
Same goes to chrome.tabs.create, you don't need the mighty and scary "tabs" permission.
There are quite a few.
A background script doesn't have access to the page DOM of a tab. The only DOM it can access directly is the DOM of the background.html. You should use a content script and message passing.
background_page is an option of Manifest Version 1, you are using Manifest Version 2, for which the option is called background. Usually you provide the background.js directly as a script (see https://developer.chrome.com/extensions/background_pages). You should return after you implemented those suggestions

Chrome Extensions: You do not have permission to use blocking webRequest listeners

I am writing an extension and have this call in my background page:
chrome.webRequest.onBeforeRequest.addListener(function(details) {console.log(details)}, {urls: ["<all_urls>"]}, ["blocking"]);
However whenever I run it I get this error in the dev tools for the background page:
Error during webRequestInternal.addEventListener: You do not have permission to use blocking webRequest listeners. Be sure to declare the webRequestBlocking permission in your manifest.
Even though my permissions in my manifest look like so:
"permissions": [
"cookies",
"http://*/*",
"https://*/*",
"tabs",
"history",
"webRequest",
"webRequestBlocking"
]
What the heck is going on here? Here's the web request docs http://developer.chrome.com/stable/extensions/webRequest.html.
Works good for me with following code, why do you want to do it in a background page in specific?
Screen Shot
Manifest.json
{
"name":"My First App",
"description":"This is First App",
"version":"1",
"manifest_version":2,
"permissions": [
"cookies",
"http://*/*",
"https://*/*",
"tabs",
"history",
"webRequest",
"webRequestBlocking"
],
"icons":{"16":"icon.jpg"},
"background":{
"scripts": ["background.js"]
},
"browser_action":{
"default_popup":"popup.html",
"default_icon":"icon.jpg"
}
}
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
popup.js
chrome.webRequest.onBeforeRequest.addListener(function(details) {
console.log(details);
}, {urls: ["<all_urls>"]}, ["blocking"]);
Background.js
function doNothing(){
}
I had a similar problem when I was writing my first Chrome extension:
My solution was to remove the extension, fix the manifest.json and add the extension again. Just deactivating it and re-activating it won't do the trick.
I loaded the extension into Chrome and started to debug. When I found an error in the code, I deactivated the extension, fixed the bug an activated the extension again. This worked fine to fix code errors. But when I tried to use the webrequest module in a blocking fashion I always got "Error during webRequestInternal.addEventListener: You do not have permission to use blocking webRequest listeners. Be sure to declare the webRequestBlocking permission in your manifest.". No matter how I changed the manifest.json.
Going to manifest version 2 instead of 3 fixed this issue for me.

chrome extension auto-update from a local server

I have created a chrome extension for the company I work for, and want to post updates to a local server within the office network.
I have read http://code.google.com/chrome/extensions/autoupdate.html, but to no avail.
manifest.json
{
"name": "My Helper",
"version": "1.0",
"description": "desc",
"omnibox": { "keyword" : "bla" },
"update_url": "\\\\serverName\\Dir\\updates.xml",
"background_page" : "background.html",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions":
["tabs"]
}
updates.xml
<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='abpjmfcmljpfhjpalecklgnndibgnjmp'>
<updatecheck codebase='\\serverName\Dir\my_helper.crx' version='1.0' />
</app>
</gupdate>
Now, after I make some changes, I do the following:
Update the version in manifest.json to 1.1
Update the version in updates.xml to 1.1
Pack the extension, specifying the directory which holds the source files and the pem file.
Post the new crx file and the new updates.xml file to \\serverName\Dir\
Go to another computer on the network, and click update extensions now in the Chrome's extensions window.
-> Nothing happens.
Question is - how to make an extension auto-update from a shared server on the local network?
P.S. I am pretty sure this is not a permissions issue. I have made sure I can access \\serverName\Dir\ from all clients on this network.
AFAIK Network URL's are not supported. Host a simple HTTP server (there are many out there), and link directly to that instead.
yep you could put the update on a live but private server with some ridiculous sub-domain or domain name and even further add a random dir name or two if your worried about it going public

Google chrome extension issue: popup.html interferes with script execution in background.html

I am learning how to extend Google chrome and I have run into the following problem:
I have the following manifest file:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"background_page": "background.html",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
]
}
My background.html file just injects some simple JavaScript into the page:
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {code:"alert(\"hi from background CODE\");"});
});
</script>
My popup.html file is just simple HTML:
<body>
Sup Playa
</body>
The dialog box from background.html never gets displayed. popup.html functions as expected.
However when I comment out popup.html from the manifest file, the script in background.html works.
What am I doing wrong? Why aren't both the dialog box and the popup showing up?
As it says in the docs:
onClicked event will not fire if the browser action has a popup.
So if you assign any html file to your popup "popup": "popup.html" (rather than just a button without body), onClicked event isn't fired.
You can just put your code right into popup.html (it has same privileges as a background page) if you want something to be executed each time it is opened:
chrome.tabs.executeScript(null, {code:"alert(\"hi from background CODE\");"});

Resources