How to track the transition to a new page (not the tab) using the Chrome API?

I try this, but probably wrong:

chrome.windows.onCreated.addListener(function(win){ chrome.windows.get(win.id,{populate:true},function(tabwin){ setTimeout(function(){ //chrome.tabs.executeScript(tabwin.tabs[0].id,{code:"alert(JSON.stringify(window));",runAt:'document_idle'}); console.log('onCreated'); },500); }); }); 

Addition:


An error flies when we try to send messages: extensions :: schemaUtils: 113 Uncaught Error: Invocation of form tabs.sendMessage (integer) doesn’t match definition tabs.sendMessage (integer tabId, any message, optional object options, optional function responseCallback)

  chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if(changeInfo.url != null) { console.log('изменилось '+changeInfo.url); console.log('tabId '+tabId); setTemer(tabId); } }); function setTemer(tabs_id){ setInterval(function(){ chrome.tabs.sendMessage(tabs_id), {greeting: "hello"}, function (response) { //console.log("ID - "+tabs); }; }, 6000); } 
  • at least try to remove the bracket chrome.tabs.sendMessage (tabs_id), {greeting: "hello"}, .... - UserName
  • @UserName I just want to send messages. If I remove sendMessage, I don’t even know how I can send it, how. - gilo1212
  • updated answer. Code checked - UserName
  • Forgot to clarify. The code in which I added a worker in the response provided that you have a receiver in the content_scripts written without errors - UserName

1 answer 1

Alternatively, use the chrome.tabs.api* onUpdated .

It works when the tab is updated. Following links anchors are also caught.

 chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if(changeInfo.url != null) { alert(changeInfo.url); } }); 

tabId - As the name implies, this is the id of the tab in which the event worked.

changeInfo - the object containing the changes of the updated tab.

  • url - Property of the changeInfo object. Contains a value if the url has changed.

tab - The tab itself, in which the event worked.

tab | onUpdated

UPD

  function setTemer(tabs_id){ setInterval(function(){ chrome.tabs.sendMessage(tabs_id), {greeting: "hello"}, function (response) { //console.log("ID - "+tabs); }; }, 6000); } 

It does not work, if only because you have an extra bracket right after the tabs_id .

And there is no bracket after the closing curly callback'a.

Because of this, you get the error presented in the supplement.

 function setTemer(tabs_id) { setInterval(function () { chrome.tabs.sendMessage(tabs_id, {greeting: "hello"}, function (response) { //console.log("ID - "+tabs); }); }, 6000); } 

The rest of the code did not touch. Worked fine. From the background script, the messages reached the content_scripts .

  • Thank. And how to set the id for each tab? The extension should work with those tabs on which I am not at the moment. - gilo1212
  • @ gilo1212, what does it mean to set the id for each tab? When the event is triggered, you already get the tabId of the updated owner. In the example above, in callback, the first parameter. It will work on all tabs, even if they are not active. Ie for example, from the active tab, click on the links and open them in separate tabs, the event will work for each of the new tabs. - UserName
  • Thanks, but it looks like it's not exactly what I'm looking for. I need a transition to a new tab, to receive the information that we sent from the popup menu. If you do not mind, please pay attention to this question: ru.stackoverflow.com/questions/530652/… - gilo1212
  • By the way, for some reason, the code does not work, I added it to background.js - gilo1212
  • @ gilo1212, the code should work. I copied it from the editor, that is, I checked it. Check in the manifest.json permission section. The tabs permission must be set. "On your second question. Did you want to track the focus change from the popup and continue sending messages at intervals? That is a continuation question, right? - UserName