Chrome 扩展:让它在每个页面加载时运行 [英] Chrome Extension: Make it run every page load

查看:55
本文介绍了Chrome 扩展:让它在每个页面加载时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个在页面加载后执行一些脚本的 chrome 扩展,我不确定我是否必须在后台页面上实现这个逻辑,或者它可以在其他任何地方,这里的任何帮助将不胜感激.

I want to make a chrome extension that executes some scripts after one page is loaded, I am not sure whether I have to implement this logic on the background page or it can be anywhere else, any help here will be greatly appreciated.

推荐答案

来自 背景脚本 您可以收听 chrome.tabs.onUpdated 事件并检查回调上的属性 changeInfo.status.它可以是加载完成.如果它完成,请执行操作.

From a background script you can listen to the chrome.tabs.onUpdated event and check the property changeInfo.status on the callback. It can be loading or complete. If it is complete, do the action.

示例:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {

    // do your things

  }
})

因为这可能会在每个选项卡完成时触发,您还可以检查选项卡是否在其 同名属性,像这样:

Because this will probably trigger on every tab completion, you can also check if the tab is active on its homonymous attribute, like this:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete' && tab.active) {

    // do your things

  }
})

这篇关于Chrome 扩展:让它在每个页面加载时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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