我正在制作一个 chrome 扩展程序以登录到 wifi 系统,目前已将其设置为他们必须单击扩展程序才能登录。但我希望它每 30 分钟使用后台 js 对他们进行登录我希望它在 30 分钟过去后检查我保存的 cookie,然后登录。但它仅在您首次安装时有效。
这是我的 manifest.json:
{
"manifest_version": 2,
"name": "BCA Auto Login",
"description": "This extension automatically signs you into the BCA wifi",
"version": "1.0",
"permissions": [ "cookies", "http://*/*", "https://*/*" ],
"content_scripts": [{
"matches": ["http://*/*","https://*/*"],
"js": ["jquery.js","login.js"]
}],
"background": {
"scripts": ["jquery.js", "background.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
这是我的 background.js:
$(document).ready(function(){
chrome.cookies.get({ url: 'urlofCookie.com', name: 'time' },
//the cookie is a time in minutes
function (cookie) {
if (cookie) {
var current = new Date();
if((current.getMinutes() - cookie) >= 30){
$.ajax({
type : 'POST',
url : 'https://signinURL',
data : {
username: 'username',
password: 'password',
},
success : workedFunction
});
}
}
else{
cookieNotFound();
}
});
});
最佳答案
您只在页面加载时调用该函数,难怪在那之后它不会执行。如果你想定期执行一个 Action ,你可以使用 chrome.alarms API 和 create an alarm 每 30 分钟触发一次(您甚至不需要 cookie,除非您也将其用于其他用途)。
例如:
var wifiAlarmName = "wifiSignIn";
chrome.alarms.create(wifiAlarmName, {
delayInMinutes: 0,
periodInMinutes: 30
});
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name === wifiAlarmName) {
// Sign in
...
}
});
关于javascript - 如何让 chrome 扩展在页面加载时在后台执行功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20296324/