如何获得页面的更新和URL的改变运行内容脚本? [英] How to get content script run on page's update and url's change?

查看:263
本文介绍了如何获得页面的更新和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.

这篇关于如何获得页面的更新和URL的改变运行内容脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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