Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to click on a button to refresh the current page from within a chrome extension.

(NOTE: The reason is because I loaded a script and I needed the page to refresh because presently I just have a disclaimer"you need to refresh the page for it to work", but I would like to remove this disclaimer and just have it done automatically).

I can't figure it out. Here is my code I am trying to refresh the page. If you know a way to eliminate this code and just use an onclick that would be great too, but I tried onclick="location.reload();" but it doesn't work because it isn't fetching the actual tab but just the popup page.

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.

like image 313
wanna know Avatar asked Sep 14 '15 17:09

wanna know


People also ask

How do I refresh the current tab in Chrome?

Reload All tabs using keyboard shortcut (alt + shift + r), context menu, browser action button, or startup.

How do I refresh all tabs in one click?

another easier way is this: for example you have 10 tabs open and you want to select all of them at once, move your mouse pointer to the right most tab, hold CTRL + SHIFT and then click on it, you will see all of the tabs, from left to right, are selected.


1 Answers

To refresh the page use chrome.tabs.update with the tab's 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});
    });
};
like image 149
wOxxOm Avatar answered Sep 22 '22 18:09

wOxxOm