2

How to identify a tab is reloading, I mean actual page reload? I see chrome.tabs.onUpdated event, but for this event status is 'loading' even in case of AJAX calls from a webpage. How can I detect a page is getting reloaded ?

lucky
  • 414
  • 1
  • 7
  • 19

2 Answers2

0

You are right, looks like not possible to recognize AJAX and page reload calls. As workaround you could listen for onunload event for tab webpage. You probably need to check if tabid and url were not changed after that.

But do you really need to know if page reloaded?

Andrey
  • 722
  • 2
  • 8
  • 17
  • 1
    Yes I do need to know if a page reloaded. I thought about the option you gave, but problem is with async calls page URL gets updated without reloading a page. – lucky Apr 29 '14 at 17:23
  • Maybe your algorithm does not need to know about reload and can handle "complete" state only But I just checked for Google SERP page and noticed that AJAX calls are not logged by OnUpdated event – Andrey Apr 30 '14 at 09:10
  • 1
    chrome.tabs.onUpdated event is executed on ajax calls, btw I found an alternative to handle it in my code, so I have a way out :). But it would have been great if chrome extension provide a param differentiating between pageload and ajax. – lucky Apr 30 '14 at 18:37
0

It's an old question, but here is my solution that could be in help (without AIAX).

Since the method 'chrome.tabs.get()' return a promise, you can use the 'callback function' to check the current 'tab.status'.

Setting a boolean variable 'waitingForComplete = true', you will run your code, only when the tab will return at status = 'complete'.

background.js

var waitingForComplete = false;

chrome.tabs.onUpdated.addListener((tabId. changeInfo,tab) => {
    if(changeInfo.status == 'complete'){
        if(waitingForComplete){
            waitingForComplete = false;
            // runYourCode...
        }
    }
};

function checkTabStatusComplete(tabId){
    chrome.tabs.get(tabId. function(tab){
        if(tab.status == 'complete'){
            // runYourCode...
        } else {
            waitingForComplete = true;
        }
    }
};
Marcello
  • 438
  • 5
  • 21