Passing variable from background to popup on Chrome extension - google-chrome

I want to make a simple extension, which is passing a variable from background to popup. The problem is, I get 'undefined' response.
Manifest:
{
"name": "Get var",
"description": "get var",
"version": "2.0",
"permissions": [
"activeTab"
],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_title": "Get that variable",
"default_popup": "popup.html"
},
"manifest_version": 2
}
background.js
var myURL = 'aaa';
popup.html
<!doctype html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
popup.js
document.addEventListener('DOMContentLoaded', function () {
var bg = chrome.extension.getBackgroundPage();
var myURL = bg.myURL;
alert(myURL)
});

Your code (exactly as you posted it in your question) is correct and should work fine. (In fact when I created a sample extension using the code you provided, it did work as expected.)
You either accidentaly changed something when copying-pasting the actual code or there is something else messing things up (something you left out of your post).
BTW, I would suggest looking into event-pages (i.e. non-persistent background-pages) which are more resource-friendly. Keep in mind, that moving to an event-page would require some changes in order for the code to work:
document.addEventListener('DOMContentLoaded', function () {
chrome.runtime.getBackgroundPage(function (bg) {
var myUrl = bg.myUrl;
alert(myUrl);
});
});
Note: I do not in any way imply that the reason you receive undefined is that you use a persistent background-page. The code you posted should work fine with a persistent background-page - it is just better practice to use an event page (whenever possible).

I ran across a similar issue; my problem was that I didn't reload the extension after changing the manifest.
I would guess that the OP's issue was the same.

Finally I found the problem, I read background pages need background permission to work (chrome extension permissions documentation here). So I changed manifest like this:
{
"name": "Get var",
"description": "get var",
"version": "2.0",
"permissions": [
"activeTab,background"
],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_title": "Get that variable",
"default_popup": "popup.html"
},
"manifest_version": 2
}

Related

Chrome - Message passing - From popup click to context script on specific tab

Can you tell me why the following code is not working. Here is my code :
Popup.js (not a backgorund script) :
chrome.tabs.create({url: url}, function(tab) {
chrome.tabs.executeScript(tab.id, {file: 'connect.js', allFrames:true}, function() {
chrome.tabs.sendMessage(tab.id, 'whatever value; String, object, whatever');
});
});
content script :
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
console.log(message);
// Handle message.
// In this example, message === 'whatever value; String, object, whatever'
});
And my manifest :
{
"name": "AN App",
"version": "1.0",
"description": "To connect",
"permissions": [
"storage",
"activeTab",
"tabs",
"https://*/*"],
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["https://*/*"],
"js": ["connect.js"]
}],
/*
"background": {
"scripts": ["background.js"]
},*/
"manifest_version": 2
}
I don't understand, the console debug in the tab do not display anything...
I also try from the popup to the background and then from the background to the tab but nothing happen neither (I'm quite new at chrome extension so I hope u can help me)
Thanks,
Regards
Martin
I found the solution. When I call chrome.tabs.create from the JS inside the popup it closes the code running in the popup and the message is never sent.
So instead of calling the chrome.tabs.create inside the JS linked to the popup, I just send a message to the background script, the background script call chrome.tabs.create (like this it runs in background and the code do not stop from executing).
And then the message function works correctly like chrome doc says.
Martin

Chrome Extension - Append HTML & Run jQuery Function on Extension Click

So I'm in the midst of creating my first Chrome Extension (Trying)
I feel like I'm close... But I genuinely don't know what to google to get the answers I need. So I'm sorry if this is a silly question.
Essentially what I'm trying to do is on click of extension - Append HTML & CSS to body and run a jQuery function. But from the looks of it, I need to call in jQuery in the manifest? Which I think I've done and it's still not working.
My Code:
manifest.json
{
"name": "Title",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_title": "Hover Title",
"default_icon": "icon.png"
},
"content_scripts": [ {
"js": [ "jquery-1.7.2.min.js", "background.js" ],
"matches": [ "http://*/*", "https://*/*"]
}],
"manifest_version": 2
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
(function ($) {
$('body').append("Hello");
alert("Hello");
console.log("Hello");
}(jQuery));
});
Any insight into where I'm going wrong would be massively helpful!
Thank you!!
Chrome extension architecture is simple but it doesn't mean one can write code without studying it.
There are two methods of injecting content scripts:
Unconditionally on all specified urls, in which case "content_script" key is used in the manifest and the content scripts communicate with the background page via runtime.sendMessage.
Only when some event occurs like e.g. the user clicks our toolbar icon, in which case we only need the permission to access the active tab.
So in the given case we'll attach the icon click handler and inject the code afterwards:
manifest.json:
{
"name": "Title",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_title": "Icon Title",
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": ["activeTab"],
"manifest_version": 2
}
background.js (this is an event page because we didn't use "persistent": true in the manifest, so be advised that the [global] variables will be lost after a few seconds of inactivity; instead you should use chrome.storage API or HTML5 localStorage/sessionStorage/and so on):
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({file: "jquery-1.7.2.min.js"}, function(result) {
chrome.tabs.executeScript({file: "content.js"}, function(result) {
});
});
});
content.js (the code runs in a sandbox so there's no need to hide global variables using IIFE)
$('body').append("Hello");
alert("Hello");
console.log("Hello");

Chrome extension: Cannot run code in chrome.tabs.executeScript in context of the page

I created an extension for Google Chrome with this background script background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {code:"document.body.style.background='red !important';"}); // doesn't work
chrome.tabs.executeScript(tab.id, {code:"alert('hello');"}); // runs alert
});
I want to run document.body.style.background='red !important'; in the context of the web page.
How can I do it?
manifest.json:
{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"description": "Test",
"browser_action": { "default_icon": "icon.png" },
"background": { "scripts": ["background.js"] },
"permissions": ["tabs", "*://*/*"]
}
Plain and straight. Add https://*/* to permissions.
background actually expects everything, if you want to change the color use backgroundColor and need not give !important as you are injecting after everything is loaded.
So the below change may work. Please try that.
chrome.tabs.executeScript(tab.id, {code:"document.body.style.backgroundColor='red';"});

Chrome Addon not showing in Context Menu [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Port error while changing chrome extension from manifest v1 to v2
I am trying to develop an addon for a website of mine. My users will need to be able to right-click any hyperlink on any webpage and then click a link in the Chrome context menu that will take them to my website to perform an action.
My addon is done, but everytime I try to test it, the link does not appear in the Chrome context menu when a hyperlink is reght-clicked.
Here is my files:
manifest.jason
{
"manifest_version": 2,
"background_page": "background.html",
"description": "Decrypt Short URLs.",
"icons": {
"128": "icon-128.png",
"16": "icon-16.png",
"48": "icon-48.png"
},
"minimum_chrome_version": "8.0.0.0",
"name": "xxxx.xxx",
"permissions": [ "http://*/*", "https://*/*", "tabs", "contextMenus" ],
"version": "1.0"
}
background.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function handleClick() {
return function(info, tab) {
var url = 'http://xxx.xxx/api.php?url=' + info.linkUrl + '&source=chromeextension'
// Create a new tab to the results page
chrome.tabs.create({ url: url, selected:true });
};
};
chrome.contextMenus.create({
"title" : "Decrypt this Link",
"type" : "normal",
"contexts" : ["link"],
"onclick" : handleClick()
});
</script>
</body>
I will appreciate any help.
I guess the problem is in your manifest file. You use manifest version 2, but your background page is declared as in manifest version 1.
You should try to change this part of manifest.json:
"background_page": "background.html",
on this one:
"background": {
"scripts": ["background.js"]
},
and put all of your background code to background.js
More info you can read here: background_pages
P.S. Sorry, Mr. Rob W already points on this problem in the comments to main question.

Background page function not triggered in google chrome:

I am using the following code to access the background page function in google chrome
popup.html
function sendRequest(ea,eb)
{
console.log("Inside");
chrome.extension.sendRequest({ea:ea,eb:eb},
function(response)
{
alert(response.farewell);
});
}
background.html
<html>
<body>
<script>
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
})
</html>
</body>
</script>
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["http://*/"],
"js": ["popup.js"]
}
],
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "mine.html"
},
"permissions": [
"tabs","http://*/","background"
],
"web_accessible_resources": ["loading.html","bu.png"]
}
However it does not print the alert. Can anyone tell me what i am doing wrong here?
Your HTML for background.html is extremely malformed and should be fixed;
<html>
<body>
<script>
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
})
</script>
</body>
</html>
Tags should be closed in the reverse order of them being open to maintain a correct hierarchy. Since you had not done so, the <script> element was malformed and contained invalid syntax </html></body> so would not be executed correctly.
Since you're using version 2 of the manifest you may want to consider abstracting the contents of this script element (ignoring all HTML) to its own file (e.g. background.js) and change your manifest to the following;
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"minimum_chrome_version": "18",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*/"],
"js": ["popup.js"]
}
],
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "mine.html"
},
"permissions": [
"tabs","http://*/","background"
],
"web_accessible_resources": ["loading.html","bu.png"]
}
Notice that the background property now contains an array of strings representing JavaScript files to be loaded (in the order specified) in to a dynamically generated background page.
I've also set the minimum_chrome_version property to 18 as manifest version 2 should only be used when targeting this version of Chrome and above.
Developers should now only really need to use background pages instead of scripts when they need to support older versions of Chrome.
Edit
It just clicked that you're attempting to execute embedded JavaScript within your background page. Manifest version 2 introduces Content Security Policies which prohibit the execution of inline (e.g. onclick="showDialog();" and href="javascript:void(0);") and embedded JavaScript. This is why your background.html won't work and why background.js will. You will also want to ensure your popup.html doesn't contain any embedded JavaScript. The best workaround (and generally best practice anyway) is to abstract all JavaScript into its own file (e.g. popup.js) which is referenced by the HTML file. For example;
<script src="/popup.js"></script>