javascript 如何从 chrome 扩展 popup.html 中重新加载当前选项卡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32570100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:29:04  来源:igfitidea点击:

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

javascriptjqueryhtmlgoogle-chrome-extensionpage-refresh

提问by wanna know

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();"but it doesn't work because it isn't fetching the actual tab but just the popup page.

我想不通。这是我尝试刷新页面的代码。如果您知道一种消除此代码的方法并且只使用 onclick 也很棒,但我尝试过onclick="location.reload();"但它不起作用,因为它没有获取实际选项卡,而只是获取弹出页面。

background.html

背景.html

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

refresh.js

刷新.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

弹出窗口.html

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

Please help thanks.

请帮忙谢谢。

回答by wOxxOm

To refresh the page use chrome.tabs.updatewith the tab's url.

要刷新页面,请使用chrome.tabs.update和选项卡的 url。

refresh.js:

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

回答by Shih-Wen Su

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

回答by Pomo

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

我的 Chrome 扩展使用具有所有默认值的 chrome.tabs.reload() 正确重新加载活动选项卡。很简单的。弹出窗口本身保持打开状态。

In popup.js...

在 popup.js 中...

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