后台代码在任何弹出式单击时运行
Background code runs on any popup click
我正在尝试在后台页面中获取登录代码,以便在浏览器启动时仅运行一次。但似乎每次单击弹出窗口时,bgpage 中的全局代码都会运行。此外,它在不同的范围内运行。有可能解决这个问题吗?
// background.js
var someCustomType = new SomeCustomType();
function SomeCustomType() {
this.firstProperty;
}
function thisShouldBeCalledOnce() {
someCustomType.firstProperty = 'defined';
alert('someCustomType.firstProperty=' + someCustomType.firstProperty);
console.log('thisShouldBeCalledOnce');
}
if (true) {
// Alwase 'undefined'
alert('someCustomType.firstProperty=' + someCustomType.firstProperty);
thisShouldBeCalledOnce();
}
简单的代码示例:
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
]
}
弹出窗口.html:只是一些虚拟代码:
<html>
<head>
</head>
<body>
<div>
<p> sometext</p>
<button type="button">Click Me</button>
</div>
</body>
</html>
背景.js:
var someCustomType = new SomeCustomType();
function SomeCustomType() {
this.firstProperty;
}
function thisShouldBeCalledOnce() {
someCustomType.firstProperty = 'defined';
alert('someCustomType.firstProperty=' + someCustomType.firstProperty);
console.log('thisShouldBeCalledOnce');
}
thisShouldBeCalledOnce();
在这种情况下,警报被触发一次,当你加载扩展时,当你打开弹出窗口时,不会显示任何警报。问候。
相关文章:
- 单击按钮前运行事件
- 单击链接时停止运行javascript
- 在单击事件中,第一次单击时激发代码一次,其余代码在所有单击时运行
- 在Node Webkit应用程序中从DOM单击按钮时运行批处理文件
- 计时器自动运行,而不是单击javascript按钮
- 运行“;非“;单击另一个函数中的函数仅一次
- 单击1个表单中的2个提交按钮中的1个时,如何运行脚本
- 在同一页面上单击按钮运行不同的代码
- 附加 jQuery 代码以在加载和按钮单击时运行
- 我需要在单击时运行两个函数.即使嵌套在一起,我也无法让它们都工作
- 如何制作按钮.单击“开始时不运行”
- 如何加载一个html页面并在其中只需单击一次即可运行函数
- 每次用户单击按钮时运行if语句检查器
- 运行Javascript更改RadioButtonList单击时的值
- 第二次单击时,使用JQUERY运行另一个函数
- 使用运行时创建的链接来触发ASP.Net中隐藏按钮的单击处理程序
- 单击javascript中的按钮运行python脚本
- 单击运行清除表单的JS函数的按钮后,视图无法恢复
- 单击运行两次
- jquery.单击“运行无事件函数”