How to reload current tab from chrome extension popup.html?

I am trying to click a button to refresh the current page using the chrome extension.

(NOTE: The reason is that I downloaded the script and I need a page to update, because for now I just have a disclaimer of โ€œyou need to refresh the page for it to work,โ€ but I would like to remove this disclaimer and just do it automatically).

I can not understand. Here is my code I'm trying to update. If you know a way to eliminate this code and just use onclick, that's great too, but I tried onclick="location.reload();" but it doesnโ€™t work because it does not retrieve the actual tab, but only a popup.

background.html

 chrome.browserAction.onClicked.addListener(function (activeTab) { var newURL = toggleDevMode(activeTab.url); chrome.tabs.update({ url: refresh }); }); 

refresh.js

 document.getElementById("mydivtoclicky").onclick = function() { chrome.tabs.getSelected(null, function(tab) { tabId = tab.id; // send a request to the background page to store a new tabId chrome.runtime.sendMessage({type:"new tabid", tabid:tabId}); }); }; chrome.runtime.sendMessage({type:"refresh"}); 

popup.html

 <head> <script type="text/javascript" src="../refresh.js"></script> </head> <body> <div id="mydivtoclicky">REFRESH PAGE</div> </body> 

Please help thanks.

+5
source share
3 answers

To refresh the page, use chrome.tabs.update with the tab url.

refresh.js:

 document.getElementById("mydivtoclicky").onclick = function() { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.update(tabs[0].id, {url: tabs[0].url}); }); }; 
+4
source

Suppose you want to update the tab that the user is using in the current Chrome window from the background background runtime, you can use the following example in background.html / background.js.

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

Learn more about chrome.tabs.reload .

+5
source

My Chrome extension correctly reloads the active tab with chrome.tabs.reload () with all the defaults. Very simple. The popup window remains open.

In popup.js ...

 function reloadMainTab() { chrome.tabs.reload(); } document.getElementById('reloadBtn').addEventListener('click', reloadMainTab); 
+3
source

All Articles