Chrome扩展API用于刷新页面 [英] chrome extension API for refreshing the page

查看:442
本文介绍了Chrome扩展API用于刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有API以编程方式刷新浏览器动作按钮内的当前选项卡?我配置了后台页面,它通过以下方式附加了一个监听器:

  chrome.browserAction.onClicked.addListener(function(tab){。 ...}); 

所以回调函数会检索对它被点击的标签的引用,但是我没有在任何地方都可以刷新/重新载入该标签。

向当前标签中注入调用 window.location.reload()的javascript。例如:

  chrome.tabs.getSelected(null,function(tab){
var code ='window。 location.reload();';
chrome.tabs.executeScript(tab.id,{code:code});
});

参考这里


Is there an API to programmatically refresh the current tab from inside a browser action button? I have background page configured, which attaches a listener via:

chrome.browserAction.onClicked.addListener(function(tab) { ... });

So the callback function retrieves a reference to the tab that it was clicked from, but I don't see an API anywhere to refresh/reload that tab.

解决方案

I recommend using chrome.tabs.executeScript to inject javascript that calls window.location.reload() into the current tab. Something like:

chrome.tabs.getSelected(null, function(tab) {
  var code = 'window.location.reload();';
  chrome.tabs.executeScript(tab.id, {code: code});
});

Reference here

这篇关于Chrome扩展API用于刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆