Pabi Pabi - 1 year ago 65
Javascript Question

Scipt not Executing in Selected Tab to Send and Receive Messages

I am having trouble making a script run on a page.

Here is what my background.js looks like:

url: '*'
}, function(tabs) {
if (tabs.length == 1) {
var tabURL = tabs[0].url;
chrome.tabs.executeScript(tabs[0].id, {file: "getTradingData.js"}, function(){
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
} else {
alert("Please have one and only one tradingview chart page opened.");

chrome.tabs.executeScript(tabs[0].id, {file: "getTradingData.js"}
should be executing the script getTradingData.js but I don't think it is. I have an alert in the script that is not running, but does run as content script when I refresh the page.

Here is what ny getTradingData.js looks like:
alert("getTradingData.js is Running");

var myPort = chrome.extension.connect();

function(request, sender, sendResponse) {
alert("got message");
if (request.subject == "getPrice")
msg: price

Finally here is my manifest.json:

"manifest_version": 2,

"name": "Automated TradingView Strategy",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",

"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
"background": {
"scripts": ["jquery-2.2.3.min.js", "background.js"]
"content_scripts": [
"matches": ["*", "*"],
"js": ["jquery-2.2.3.min.js", "getTradingData.js"]
"permissions": [

I need to be able to send data between "*" and the extension background.js. However, the script is not loading when I refresh the extension so it can't receive nor send messages.

Let me know what I am doing wrong.

Answer Source

To run the getTradingData.js using executeScript you must add the address of the site in permissions.

"permissions": ["*://**", "tabs"]

From background you send {greeting: "hello"}, but you do not accept these data in getTradingData.js. Therefore you do not get response.


You do not send response.farewell. They will not be displayed.

if (request.subject == "getPrice") {

    sendResponse({msg: price});

Same. You get alerts only.

Programmatic injection

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download