基于URL注入Chrome浏览器扩展内容脚本 [英] Inject chrome browser extension content script based on URL

查看:297
本文介绍了基于URL注入Chrome浏览器扩展内容脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让我的后台页面观看网址,并在某些网址上调用chrome.tabs.executeScript。我应该调用哪些API来以这种方式观看URL? google.com/chrome/extensions/tabs.html#event-onUpdatedrel =nofollow noreferrer> chrome.tabs.onUpdated.addListener 可以使用检测标签加载。这不会检测框架内的导航,但:

  chrome.tabs.onUpdated.addListener(function(tabId,info,tab){ 
if(info.status ==='complete'&& /some_reg_ex_pattern/.test(tab.url)){
// ...
}
} );

为此,您最好使用内容脚本, all_frames 设置为true 。在内容脚本中,您可以使用此答案中所述的方法注入代码。然后使用该页面的位置对象来过滤网址。


I'd like to have my background page watch the URL and call chrome.tabs.executeScript on certain URLs. What API should I call to watch the URL in such a manner?

解决方案

chrome.tabs.onUpdated.addListener can be used to detect tab loads. This will not detect navigation within frames though:

chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
   if (info.status === 'complete' && /some_reg_ex_pattern/.test(tab.url)) {
       // ...
   }
});

For this purpose, you'd better use a content script, with all_frames set to true. Within the content script, you can inject code using the methods as described in this answer. Then use the page's location object to filter URLs.

这篇关于基于URL注入Chrome浏览器扩展内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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