0

I have this issue. I saw this question and tried its solution but it does not seem to work. The error disappears but the code does not do what it's supposed to do.

So, basically, I have a background script that uses XMLHTTPSrequest to inject a content script in all the pages that are http or https.

background.js:

chrome.browserAction.onClicked.addListener(function (event) {
    show_floater = !show_floater;
    // inject content script in tabs
    let xhr = new XMLHttpRequest();
    xhr.open("GET", "https://127.0.0.1/js/test1.js", true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            chrome.tabs.query({ currentWindow: true }, (tabs) => {
                tabs.forEach(function(e) {
                    if (/http:/.test(e.url) || /https:/.test(e.url)) {
                        chrome.tabs.executeScript(e.tabId, { code: xhr.responseText }, () => {
                            connect(show_floater);
                            console.log(e.url);
                        });
                    }
                    else
                        console.log('e: ' + e.url);
                });
            });
        }
    }
    xhr.send();
});

The content script then does it's magic on the page and sends a message back to the bg when a user action occurs.

content.js

 chrome.runtime.sendMessage({}, function (response) {
     console.log('sent');  
     let msgData = { text: "am I connected?" };
     chrome.runtime.sendMessage(JSON.stringify(msgData));
 });

and here is how bg handles the messages:

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    console.log('I AM HERE');
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        if (/http:/.test(e.url) || /https:/.test(e.url)) {
            const port = chrome.tabs.connect(tabs[0].id);
            msg = JSON.parse(msg);
            if (msg.text == "am I connected?") {
                //do stuff
            }
        }
    });
    // return true;
});

the answer I was looking at said to add a 'return true' at the end. I tried and the error goes away but the console.log does not appear... HALP!

Stefano Pozzi
  • 529
  • 2
  • 10
  • 26

2 Answers2

0

No need for all that over-complicated stuff.
Simply send a message and respond:

content.js

chrome.runtime.sendMessage({ text: 'am I connected?' }, response => {
  console.log(response);
});

background.js

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  if (msg.text == 'am I connected?') {
    console.log(msg, 'sender URL:', sender.url);
    //do stuff
    sendResponse({ foo: 'bar' });
  }
});

Note that this onMessage listener should be added just once, in the global scope of background.js - not inside the loop that you use to process tabs.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • the loop is to add the content script to each page that has either HTTP or HTTPS in the url (so that I do not add it to 'chrome://' pages). But as far as i can see I do send the messages as you say it. Except that I have no sendResponse. Is that the important part? – Stefano Pozzi Feb 09 '21 at 12:54
-1

Problem here in your chrome extensions.

go here chrome://extensions and disabled all extensions and try! it will work.

but try to enable extensions one by one to know what the extension make this error

Abdulrahman
  • 775
  • 6
  • 19