简体   繁体   English

Chrome扩展程序-如何在页面加载时执行JS脚本

[英]Chrome Extension - How to execute a JS script on page load

The following is that I'm trying to achieve 以下是我正在努力实现的

Every time 'www.google.com' loads, a script/function triggers. 每次加载“ www.google.com”时,都会触发一个脚本/函数。 However, the functions itself reloads the page via 'location.reload();' 但是,函数本身通过“ location.reload();”重新加载页面。 Basically, an infinite loop of reloads. 基本上是无限循环的重载。 Script reloads page -> Script injected -> Script reloads page -> etc. 脚本重新加载页面->脚本注入->脚本重新加载页面->等等。

I want the tab @ google.com to keep refreshing while I work/browse the net via other tabs in the same Chrome browser Window. 我希望在通过同一Chrome浏览器窗口中的其他标签工作/浏览网络时,标签google.com保持刷新。

How should I approach this. 我应该如何处理。 I've been looking into background and content scripts but I'm stuck. 我一直在研究后台脚本和内容脚本,但我陷入了困境。 Assistance is greatly appreciated. 非常感谢您的协助。

I presume you have no problem getting a content script to run once on page load. 我想您可以让内容脚本在页面加载时运行一次没有问题。 I'm not going to help you with that... 我不会帮你的...

You're going to have to 你将不得不

  • have the content script send a message to the background script 让内容脚本向后台脚本发送消息
  • use chrome.alarms.create(string name, object alarmInfo) in the background script 在后台脚本中使用chrome.alarms.create(string name, object alarmInfo)
  • listen for the onAlarm event 监听onAlarm事件
  • use chrome.tabs.sendMessage() to send a message to make the content script reload the page 使用chrome.tabs.sendMessage()发送消息以使内容脚本重新加载页面

This is, of course, totally untested! 当然,这是完全未经测试的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM