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.
Reload All tabs using keyboard shortcut (alt + shift + r), context menu, browser action button, or startup.
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.
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});
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With