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.
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});
});
};