0

THE PROCESS:

I created a Google Chrome extension and I placed a button in the options page of this extension. When this button is clicked, it is supposed to execute the code written in options.js file (referring to the function loopTabsAndWriteToConsole), loop through all the open tabs and send a dynamic variable (the iteration number) to the content script. The content script then writes the dynamic variable to the console log of the respective tab, that is being currently accessed by the script.

THE ISSUE:

When the button in the options page is clicked, it does NOT write the expected values to console in each of the tabs that are open.

How can the code be fixed so that when the button is clicked, the script options.js and the function loopTabsAndWriteToConsole are executed, and the dynamic variable/iteration count is written to the console with respect to the tab executed? That is, the dynamic variable/iteration count should be written to the console of the tab that is being accessed by the script.

For example, this is what I expect when I have 5 tabs in the browser and I click the button:

1st Tab Console log: 0
2nd Tab Console log: 1
3rd Tab Console log: 2
4th Tab Console log: 3
5th Tab Console log: 4

In the above code, the values: 0, 1, 2, 3, 4 refer to the iteration number of the for loop (i.e. the tabs that are currently open in my browser window).

Following are my HTML and JS codes:

HTML:

<button id="btnWrite" type="button" class="btn btn-primary btn-block btn-lg">ALL TABS: Write to Console</button>

options.js:

document.getElementById('btnWrite').addEventListener('click', loopTabsAndWriteToConsole);

function loopTabsAndWriteToConsole() {
        chrome.tabs.query({}, function (tabs) {
            for (var i = 0; i < tabs.length; i++) {
                chrome.tabs.executeScript(tabs[i].id, {code: "var config = '" + (i) + "';"}, function () {
                    chrome.tabs.executeScript(tabs[i].id, {file: "scripts/my_script.js"});
                });
            }
        });
}

my_script.js:

function writeToConsole() {
    console.warn(config);
}
writeToConsole();

UPDATE 1:

The following code gets executed but I am unable to pass the dynamic variable/iteration count to log it to console:

chrome.tabs.query( {} ,function (tabs) {
    for (var i = 0; i < tabs.length; i++) {
        chrome.tabs.executeScript(tabs[i].id, {file: "scripts/my_script.js"});
    }
});

SOLUTION:

I figured out a solution for the issue. The following is the revised function that I am using.

function loopTabsAndWriteToConsole() {
    chrome.tabs.query({}, function (tabs) {
        for (var i = 0; i < tabs.length; i++) {
            (function (i) {
                chrome.tabs.executeScript(tabs[i].id, {code: "var config = '" + (i) + "';"}, function () {
                    chrome.tabs.executeScript(tabs[i].id, {file: "scripts/my_script.js"});
                });
            })(i);
        }
    });
}
Devner
  • 6,825
  • 11
  • 63
  • 104
  • have you seen errors in console of chrome? you are sure your program goes into function loopTabsAndWriteToConsole, and tabs.length is not zero? – Frenchy Feb 22 '21 at 12:38
  • @Frenchy I do not see any errors in any of the 5 tabs that I opened. Just blank console! When I try to debug, the function `loopTabsAndWriteToConsole` gets triggered but it does not seem to be entering the code `chrome.tabs.query({}, function (tabs) {`. That's very weird to me. The script is working fine if I remove the `for` loop, but I need the for loop to iterate through all the tabs and then also pass the dynamic variable to it. In other words, I have to replace the `var config` line in for loop with `chrome.tabs.executeScript({file: "scripts/my_script.js"});`, in order for it to work. – Devner Feb 22 '21 at 12:53
  • there is an option in file, chrome.tabs.executeScript(tabs[i].id, {file: "scripts/my_script.js", allFrames : true}); dunno if fix your problem – Frenchy Feb 22 '21 at 14:54
  • @Frenchy Thank you for that. I tried it, but it did not make any difference and I do NOT see anything in the console still. However, I stumbled across a different find. When I click on the "Manage Extensions" and I check my extension, Chrome reports some errors when I click the button. Here is the error: `Error handling response: TypeError: Cannot read property 'id' of undefined`. The error is in the following line: `chrome.tabs.executeScript(tabs[i].id, {file: 'scripts/my_script.js', allFrames: true});`. So IMO, the script is NOT receiving the value of `tabs[i].id`. Any ideas on how to fix? – Devner Feb 22 '21 at 15:20
  • just try i instead tabs[i].id – Frenchy Feb 22 '21 at 15:39
  • Tried `i` as you suggested. Got the following error: `Unchecked runtime.lastError: No tab with id: 8.` Seems like its looping through all tabs and pointing to the last entry! – Devner Feb 22 '21 at 15:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229046/discussion-between-frenchy-and-devner). – Frenchy Feb 22 '21 at 15:47
  • @Frenchy I was able to solve the issue. I posted the Solution in the description. Please feel free to check it out, if you get a chance. Thanks a ton for all your help. I appreciate it! – Devner Mar 08 '21 at 04:15

0 Answers0