0

I have constructed a Chrome extension which creates an additional local page (about:blank-based) in a separate tab with a data table relevant to the main page from which the extension script is run. One feature of this table is that it contains links which when clicked should change the focus back to the main page tab, simultaneously with changing the URL of the main page tab. I have used the chrome.tabs.highlight function from the Chrome tabs API here in conjunction with a background.js script to change the focus.

This has worked fine since I developed this function this spring. Today I notice that although the main page tab changes its location when the link is clicked, the focus is no longer shifted. Have the inner workings of the chrome tabs API been changed in any way recently? Current Chrome version is 76.0.3809.132 for Mac and my OS is 10.13.6.

Using alert commands (see code) I can verify that info about tab index is passed ok between background.js and content.js.

"tabs" is also included in the permissions in the manifest.json file.

Here is a summary of the relevant code. Note that the attribute "goto" is used instead of "href"; this is for tecnical reasons which I believe are irrelevant here.

//background.js:

var replyObject = {};
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    switch(request.command){
      case "changeTab": 
         var toTab = request.data; 
         chrome.tabs.highlight({tabs:toTab});

         //alert("totab: " + toTab);

        break;
      case "getTabIndex":  
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          var currTab = tabs[0].index;
          if (currTab) { 
             replyObject = {type:"tabIdIs",data:currTab};

             //alert("currTab: " + currTab);

          }
        });
       break;
    };
    sendResponse(replyObject);
});



//===================

//content.js:

//getting tab index info:
 chrome.runtime.sendMessage({command:"getTabIndex"},function(reply){
   if(reply.data != undefined){
      mainTab = reply.data;
     }
 });

//changing tab:
  $(sumBody).on("click","a.qlink",function(){ 
     var sendObj = {command:"changeTab",data:mainTab};
     window.location.href = $(this).attr("goto");
     chrome.runtime.sendMessage(sendObj);//visa Inspera-fliken
  });
danbae
  • 563
  • 2
  • 8
  • 22
  • 1) sendResponse will always send the wrong/empty replyObject because query() is asynchronous and runs **after** the outer code completes, [more info](/q/23667086/), to fix it you'll need `return true` at the end of the onMessage listener and move sendResponse inside the callback of query() 2) I would move `window.location.href` line after sendMessage() just in case – wOxxOm Sep 03 '19 at 15:43
  • @wOxxOm: (1) for some reason, the replyObject _does_ get conveyed ok as witnessed by inserting an `alert(JSON.stringify(reply));` line in the first sendMessage function ("getting tab index info:") which yields the message `{"type":"tabIdIs","data":1}`. Adding `return true` to the listener changes nothing. I also tried moving the `sendResponse`, either to within the conditional `if (currTab) {` or just outside. It did not rectify the problem in either case. (2) Moving the `window.location.href` line did not change anything either. – danbae Sep 03 '19 at 16:18
  • Asynchronous JS doesn't work like you describe - as per the specification - so I guess there's some other factor at play here. Anyway I can't help without inspecting the actual extension myself. Could be even a bug in Chrome, which you can verify by running a portable older version of Chrome or Chromium. – wOxxOm Sep 03 '19 at 16:23
  • Correction to my previous comment and replacing one more which is now deleted: after cleaning up some other parts of the original code, following wOxxOm's recommendations now gives the desired result. Still mysterious why it worked earlier though. Maybe a Chromium bug has been _removed_. – danbae Sep 03 '19 at 19:29

0 Answers0