我如何制作它,以便我的Chrome扩展程序只注入一次脚本? [英] How do I make it so my Chrome extension will only inject a script once?

查看:159
本文介绍了我如何制作它,以便我的Chrome扩展程序只注入一次脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用程序化注入将我的扩展程序的代码注入页面仅当浏览器操作被点击时。

I'm using programmatic injection to inject my extension's code into a page only when the browser action is clicked.

这就是我的扩展的活动页面(根据文档中的示例):

This is what I have on my extension's event page (per the example in the documentation):

chrome.browserAction.onClicked.addListener(function callback(tab){
  chrome.tabs.executeScript(null, {file: "content-script.js"});
});

然而,这种方式的运行方式是每次注入脚本按钮被点击。

However, the way this works, the script is injected every time the button is clicked.

如何更改它以便在随后的按钮按下时不会注入脚本 - 以便仅在第一次插入 该按钮是在该页面上单击的?

How can I change it so that the script is not injected on subsequent button presses - so that it is inserted only the first time the button is clicked on that page?

推荐答案

在您的内容中放置一个gloabl变量来判断内容是否已执行。

Put a gloabl variable in your contentscript to judge if the contentscript has executed.

if (something) { return; }

这篇关于我如何制作它,以便我的Chrome扩展程序只注入一次脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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