chrome extension API for refreshing the page

前端 未结 6 1273
我寻月下人不归
我寻月下人不归 2020-12-14 16:54

Is there an API to programmatically refresh the current tab from inside a browser action button? I have background page configured, which attaches a listener via:

         


        
相关标签:
6条回答
  • 2020-12-14 17:12

    More specifically:

    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.reload(tab.id);
    });
    
    0 讨论(0)
  • 2020-12-14 17:16

    The API for chrome.tabs.getSelected(), which the accepted answer uses, has been deprecated. You should instead get the current tab and reload it using something like the following:

    chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {
        var code = 'window.location.reload();';
        chrome.tabs.executeScript(arrayOfTabs[0].id, {code: code});
    });
    

    Or perhaps:

    chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {
        chrome.tabs.reload(arrayOfTabs[0].id);
    });
    

    I had no real luck with the second version, though other answers seem to suggest it should work. The API seems to suggest that, too.

    0 讨论(0)
  • 2020-12-14 17:17

    You can also use this:

    chrome.tabs.reload(function(){});
    

    reload function params: integer tabId, object reloadProperties, function callback

    Reference: http://developer.chrome.com/extensions/tabs.html#method-reload

    0 讨论(0)
  • 2020-12-14 17:29

    I recommend using chrome.tabs.executeScript to inject javascript that calls window.location.reload() into the current tab. Something like:

    chrome.tabs.getSelected(null, function(tab) {
      var code = 'window.location.reload();';
      chrome.tabs.executeScript(tab.id, {code: code});
    });
    

    Reference here

    0 讨论(0)
  • 2020-12-14 17:30

    I think what you're looking for is:

    chrome.tabs.reload(integer tabId, object reloadProperties, function callback)
    

    Check out tabs API() documentation for more information.

    0 讨论(0)
  • 2020-12-14 17:32

    if you want to reload all the tabs which have loaded completely and are active in their window

    chrome.tabs.query({status:'complete'}, (tabs)=>{
    tabs.forEach((tab)=>{
        if(tab.url){
            chrome.tabs.update(tab.id,{url: tab.url});
         }
        });
    });
    

    you can change the parameter object to fetch only active tabs as {status:'complete', active: true} refer to query api of chrome extensions

    Reason for not using chrome.tabs.reload :

    If the tab properties especially the tab.url have not changed, tab does not reload. If you want to force reload every time, it is better to update the tab URL with its own tab.url which sends the event of the change in property and tab automatically reloads.

    0 讨论(0)
提交回复
热议问题