如何获得页面的更新和URL的改变运行内容脚本?

问题描述:

我的内容脚本只在一个域中运行。但是,如果我只用一个标签,内容脚本运行只有1次。我希望我的脚本来对每一个新的请求运行(从bla.com/home例如重新加载页面和URL变化bla.com/servoce~~V等)。

My content script runs only on one domain. But if I use only one tab, content script runs only 1 time. I want my script to run on every new request (page reload and URL changes for example from bla.com/home to bla.com/servoce etc.).

内容脚本可以侦听页面的变化。有几个方法可以做到这一点,但我的猜测是要在chrome.tabs的onUpdated事件:

The content script can listen for page changes. There's a few ways to do this, but my guess is you want the onUpdated event in chrome.tabs:

chrome.tabs.onUpdated.addListener(function callback)

在这里有更多这方面的细节: https://developer.chrome.com/extensions/tabs一>

有也是在chrome.webRequest API可能工作Web请求的监听器:

There is also a listener for web requests in the chrome.webRequest api that might work:

chrome.webRequest.onBeforeRequest.addListener(function callback)

有关这个选项,你就必须检测,如果该请求将导致重定向或没有。

For this option, you'd have to detect if the request will cause a redirect or not.

每个选项应该让你呼吁重定向一些JavaScript。

Either option should allow you to call some javascript on redirect.