I have a background.js with following snippet:
chrome.browserAction.onClicked.addListener(toggleStatus);
Now inside toggleStatus I'd like to send a message to my content script, that something has happened. Is that possible?
Or am I completely missing the point, is this the right way to go?
Behind this is that I'd like to activate/deactive my extension, without the need of reloading the page.
After some more trial & error, I finally found a solution which satisfied me enough.
First add an event listener to the content script:
var extensionPort = chrome.extension.connect();
extensionPort.onMessage(function(msg) {
// handle the received message
});
To send messages to every connected port of the extension add this to the background.js:
chrome.extension.onConnect.addListener(function(port) {
port.postMessage({ someMessage: 'take me to the foobar' });
});
Related
I am currently making a chrome extension using the following Chrome tutorial:
https://developer.chrome.com/docs/extensions/mv3/getstarted/
I ran into some issues with my own code initially so now I'm just using their code as a basis for my own thing.
In popup.js, the tutorial has a function that changes the background color on click.
function setPageBackgroundColor() {
chrome.storage.sync.get("color", ({ color }) => {
document.body.style.backgroundColor = color;
});
I replaced the line in the code instead to what I wanted it to do.
document.getElementById(xyz).miscfunction(arg1, arg2);
The getting started extension works as intended (obviously). When I run my line of code in the console in Chrome, it works exactly as intended. However, when I run the line using the extension, it gives me the following error:
Uncaught TypeError: document.getElementById(...).miscfunction is not a function
I have tested and found out that getElementById is indeed getting the element I want because when I try to run this on a tab that does not have said element, it gives me an error saying that the element is null. It does not do so for the tab that actually does have the element.
What is preventing my extension from being able to access the miscfunction? When I was last trying to debug this months ago, I recall having some issues with injection and the content security policy but I do not remember what I did to get to that point. Any help would be appreciated.
EDIT:
I have since updated the popup to call a script js file instead of calling a function, and then put the function in a separate file.
To access the document element of the page you are viewing, you need to paste the required code in the content script.
You first need to add content script and it's scope in manifest.json
"content_scripts" : [{
"matches" : ["https://<website-you-are-working-on>"]
"js" : ["script.js"] //file containing the code you need to execute
}]
The pop up script now needs to send message to content script to do what you want to do.
popup.js
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
const activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {
"action" : "change-background"
"payload": {
//required info
}
})});
Now listen for the message in the content script and do the required task.
script.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if(message.action == "change-background"){
//your code here
}
})
I want to send a variable called "website_hostname" from the content-script to the background script. It contains the hostname of the current website you're on.
Content Script:
var website_hostname = window.location.href;
//Code to send website_hostname
Background Script:
// Get website_hostname
I believe you can do that by using chrome onMessage.
in content,
//content-script.js
function notifyExtension() {
chrome.runtime.sendMessage({"url": window.loaction.href});
}
//background.js
chrome.runtime.onMessage.addListener(notify);
function notify(message) {
variable = message.url
});
}
I got this from https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage
Similar to what suggested to you in the previous answer
But to do this it will be better use long lived connections port
Long-lived connections https://developer.chrome.com/extensions/messaging
var port = chrome.runtime.connect() Send a message to background page port.postMessage()
Listener
for response port.onMessage.addListener()
I am developing a Google Chrome extension, and I need to detect redirects so I can perform a certain action (the action is irrelevant, this question simply pertains to the redirect). Thus far I have this code in my background.js:
chrome.webRequest.onBeforeRedirect.addListener(function (url, tabId) {
console.log("This is a redirect");
chrome.tabs.sendMessage(tabId, {"message": url}, function(response){});
}, {urls: ["<all_urls>"]});
However, neither the console.log or sendMessage method is getting called. I tried going to wikipedia.com, google.net, and several other sites that I know redirect the user. Why isn't the extension picking this up?
(And yes, I have put "webRequest" in my permissions under my manifest.json file.)
Thanks in advance, please let me know if you need any other code.
EDIT: Thanks to #ze it's working now, but now it's working a little too well. In other words, it's starting to fire now multiple times per page, and when I begin to type the url of a redirect site into the chrome search bar, it also gives me the redirect message. How do I only get it to fire once, when I actually push enter to navigate to the redirect site (and not while i'm still typing)? Here's the new code:
chrome.webRequest.onBeforeRedirect.addListener(function (details) {
if(details.frameId === 0){
alert("This is a redirect.");
// chrome.tabs.sendMessage(details.tabId, {"message": details.url}, function(response){});
}
}, {urls: ["<all_urls>"]});
From what I can see your callback for onBeforeRedirect has two arguments while here says that the callback should have one argument: details. Then you access url and tabId using details.url (or details.redirectUrl if you need the new url) and details.tabId.
Also, frames inside a tab may redirect as well so the event might fire more than once. If the code has to run only when the main frame redirects you should add a check like:
if (details.frameId == 0){
//Your code here
}
Also, I am not sure why you have {urls: ["<all_urls>"]} in the arguments of the addListener function. The only argument is the callback.
So, I would write something like:
chrome.webRequest.onBeforeRedirect.addListener(function (details) {
if(details.frameId == 0){
console.log("This is a redirect");
chrome.tabs.sendMessage(details.tabId, {"message": details.url}, function(response){});
}
});
Let me know if this worked out.
I'm trying to extend some of the handling of messaging between my background process and content scripts. In the normal course of things my background process sends a message via postMessage() and the content script replies via another channel with a response. However I would like to now extend the background process to fall-back to something else if the content script can't find a valid thing on the page. It's when looking at this I discovered a problem when sending a message to blank or system pages. As the tabs don't have content scripts loaded there is nothing to receive the posted message. This generates warnings in the console logs but otherwise no ill effects. However:
// Called when the user clicks on the browser action.
//
// When clicked we send a message to the current active tab's
// content script. It will then use heuristics to decide which text
// area to spawn an edit request for.
chrome.browserAction.onClicked.addListener(function(tab) {
var find_msg = {
msg: "find_edit"
};
try {
// sometimes there is no tab to talk to
var tab_port = chrome.tabs.connect(tab.id);
tab_port.postMessage(find_msg);
updateUserFeedback("sent request to content script", "green");
} catch (err) {
if (settings.get("enable_foreground")) {
handleForegroundMessage(msg);
} else {
updateUserFeedback("no text area listener on this page", "red");
}
}
});
Doesn't work. I would expect the connect or the postMessage to throw an error I can trap, however the console log is filled with error messages including:
Port: Could not establish connection. Receiving end does not exist.
But I do not end up in the catch statement.
In the end I couldn't do it with connect, I had to use the one shot sendMessage() which has a call-back function when the response comes in. That can then be interrogated for success and the state of lastError. The code now looks like this:
// Called when the user clicks on the browser action.
//
// When clicked we send a message to the current active tab's
// content script. It will then use heuristics to decide which text
// area to spawn an edit request for.
chrome.browserAction.onClicked.addListener(function(tab) {
var find_msg = {
msg: "find_edit"
};
// sometimes there is no content script to talk to which we need to detect
console.log("sending find_edit message");
chrome.tabs.sendMessage(tab.id, find_msg, function(response) {
console.log("sendMessage: "+response);
if (chrome.runtime.lastError && settings.get("enable_foreground")) {
handleForegroundMessage();
} else {
updateUserFeedback("sent request to content script", "green");
}
});
});
(I have already read this and it didn't work, and I've done a lot of searching and experimentation to no avail.)
I am writing a Chrome extension (BigConsole) with the goal of building a better Console tab for the Chrome developer tools. This means I would like to execute user-input code in the context of the page with access to the DOM and other variables on the page. To do this, the communication is structured as follows:
devtools creates a panel where the user writes code
When the user wants to execute code from the panel, the panel sends a message to a background script with the code
The background script receives the message/code from panel and passes it on to the content script which is injected into the page
The content script receives the message/code from the background script and injects a script element into the page which then runs the code
The result of the script on the page is then posted back to the content script with window.postMessage
The content script listens for the message/result from the page and passes it on to the background script
The background script receives the message/result from the content script and passes it on to the panel
The panel receives the message/result from the background script and inserts it into the log of results
Whew.
Right now, when the user tries to run the code, nothing happens. I put a bunch of console.log()s into the code but nothing appears in the console. My main question is, what have I done wrong here with the message passing that results in nothing happening? Alternatively, I would love to be told that I am making this way too complicated and there is a better way of doing things. Simplified code below...
panel.js:
window.onload = function() {
var port = chrome.runtime.connect({name: "Eval in context"});
// Add the eval'd response to the console when the background page sends it back
port.onMessage.addListener(function (msg) {
addToConsole(msg, false);
});
document.getElementById('run').addEventListener('click', function() {
var s = document.getElementById('console').value;
try {
// Ask the background page to ask the content script to inject a script
// into the DOM that can finally eval `s` in the right context.
port.postMessage(s);
// Outputting `s` to the log in the panel works here,
// but console.log() does nothing, and I can't observe any
// results of port.postMessage
}
catch(e) {}
});
};
background.js:
chrome.runtime.onConnect.addListener(function (port) {
// Listen for message from the panel and pass it on to the content
port.onMessage.addListener(function (message) {
// Request a tab for sending needed information
chrome.tabs.query({'active': true,'currentWindow': true}, function (tabs) {
// Send message to content script
if (tab) {
chrome.tabs.sendMessage(tabs[0].id, message);
}
});
});
// Post back to Devtools from content
chrome.runtime.onMessage.addListener(function (message, sender) {
port.postMessage(message);
});
});
content.js:
// Listen for the content to eval from the panel via the background page
chrome.runtime.onMessage.addListener(function (message, sender) {
executeScriptInPageContext(message);
});
function executeScriptInPageContext(m) { alert(m); }
As pointed out by Alex, here's a typo in your code which prevents it from working.
Drop your current code and use chrome.devtools.inspectedWindow.eval to directly run the code and parse the results. This simplifies your complicated logic to:
devtools creates a panel where the user writes code
devtools runs code
devtools handles result
PS. There is a way to manipulate the existing console, but I recommend against using it, unless it's for personal use. Two different ways to do this are shown in this answer.