[英]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. 我试图点击按钮刷新Chrome扩展程序中的当前页面。
(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();"
如果您知道一种消除此代码的方法,并且只使用一个非常棒的onclick,但我尝试了
onclick="location.reload();"
but it doesn't work because it isn't fetching the actual tab but just the popup page. 但它不起作用,因为它不是获取实际的选项卡而只是弹出页面。
background.html background.html
chrome.browserAction.onClicked.addListener(function (activeTab) {
var newURL = toggleDevMode(activeTab.url);
chrome.tabs.update({
url: refresh
});
});
refresh.js 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 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. 要刷新页面,请使用带有选项卡URL的chrome.tabs.update 。
refresh.js: 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});
});
};
Assume you want to refresh the tab the user is using in the current Chrome window from the extension background runtime, you can use the following example in background.html/background.js. 假设您要从扩展后台运行时刷新用户在当前Chrome窗口中使用的选项卡,您可以在background.html / background.js中使用以下示例。
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.reload(tabs[0].id);
});
More about chrome.tabs.reload . 有关chrome.tabs.reload的更多信息。
My Chrome extension correctly reloads the active tab using chrome.tabs.reload() with all defaults. 我的Chrome扩展程序使用chrome.tabs.reload()正确地重新加载活动标签,其中包含所有默认值。 Very simple.
很简单。 The popup itself stays open.
弹出窗口本身保持打开状态。
In popup.js... 在popup.js中......
function reloadMainTab() {
chrome.tabs.reload();
}
document.getElementById('reloadBtn').addEventListener('click', reloadMainTab);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.