简体   繁体   English

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

[英]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.

相关问题 Chrome扩展程序-激活popup.html中当前选项卡的脚本 - Chrome Extension - Activating a script within the popup.html for the current tab 从 chrome 扩展中的 popup.html 获取当前选项卡的 HTML 源代码 - Getting HTML source code of current tab from popup.html in chrome extension 如何使用 chrome 扩展将来自 popup.html 的输入值传递到当前页面 - How to pass input value from popup.html to current page with a chrome extension 如何在Chrome扩展程序中通过鼠标调整POPUP.html页面的大小 - How to resizing POPUP.html page from mouse in chrome extension Chrome扩展程序-如何将变量从JS传递到popup.html? - Chrome Extension - How to pass variables from JS to popup.html? Chrome扩展程序:popup.html关闭并在新标签中打开响应? - Chrome Extension: popup.html closes and opens response in a new tab? 在content.js(Chrome扩展程序)中发生操作时,如何检查popup.html页面? - How to check popup.html page when action occurs within content.js (Chrome Extension)? 如何使用Chrome扩展程序popup.html从新的弹出窗口中定位DOM元素 - How to target DOM elements from new popup window, using chrome extension popup.html 如何查询外部域,将其存储在chrome扩展名中,然后调用它以显示在popup.html中? - How to query external domain, store it within chrome extension, and call it to display in popup.html? 从“popup.html”访问当前选项卡DOM对象? - Accessing Current Tab DOM Object from "popup.html"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM