简体   繁体   English

初始化时调用函数Google Chrome扩展程序

[英]Function call at initialisation Google Chrome Extension

I would like to set as part of a Google Chrome extension that it will perform an action as soon as the browser is opened. 我想将其设置为Google Chrome扩展程序的一部分,使其在打开浏览器后立即执行操作。
It should record the time it performed this action and should only perform this action once each time the browser is opened. 它应该记录执行此操作的时间,并且每次打开浏览器时都应仅执行一次此操作。

I've looked into certain events but DOMContentLoaded would trigger too often and other events such as online might work but if the user disconnects and reconnects it would continue to trigger. 我已经研究了某些事件,但是DOMContentLoaded会触发得太多,而其他事件(例如在线)可能会起作用,但是如果用户断开连接并重新连接,它将继续触发。

From my understanding I can have background JavaScript at the top level that will run when the extension is initialised, but I don't know how often this will trigger and what exactly causes this to trigger etc. I also tried Chrome background pages but again, I'm not certain how often this may trigger. 据我了解,我可以在扩展初始化时在顶层运行背景JavaScript,但是我不知道它会触发的频率以及导致触发的确切原因等。我也尝试了Chrome的背景页面 ,我不确定这可能触发多久。

I tried setting the function to trigger upon closing instead with window.onbeforeunload but this didn't work. 我尝试将函数设置为在关闭时使用window.onbeforeunload触发,但这没有用。

The intention is for this function to clear selected parts of the user's history upon closing (Or my solution upon opening) similar to Incognito and Mozilla Firefox's "Clear history on close". 此功能的目的是在关闭时清除用户历史记录的选定部分(或在打开时提供我的解决方案),类似于Incognito和Mozilla Firefox的“关闭时清除历史记录”。 This may perform other actions in the future. 将来可能会执行其他操作。

You would want to use a background page on which you use the following API https://developer.chrome.com/extensions/runtime#event-onStartup 您可能想使用一个背景页面,在该页面上使用以下API https://developer.chrome.com/extensions/runtime#event-onStartup

Just listen for the event and run your code when it's triggered. 只需监听事件并在触发事件时运行代码即可。

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

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