我在TamperMonkey中有一个脚本,我想运行一次然后停止。它应该提示用户,然后填写方框,然后停止。但它一直在问...
function logIn() {
s = prompt('Enter your username')
document.getElementById("Header_Login_tbUsername").value = s;
s2 = prompt('Enter your password')
document.getElementById("Header_Login_tbPassword").value = s2;
document.getElementById('Header_Login_btLogin').click();
a = prompt('Paste the link')
window.location.replace(a);
}
logIn();发布于 2014-10-28 09:14:41
您可以将后一个cookie函数添加到脚本中,以创建、检查和删除cookie。
每次运行脚本时,检查cookies是否存在:
function logIn() {
var cookieValue = 'myLogin';
var exists = readCookie(cookieValue);
// If the cookie is not set, prompt to enter login and create cookie.
if (!exists) {
createCookie(cookieValue, '', 1); // Store for 1 day.
promptLogin();
}
}
function promptLogin() {
s = prompt('Enter your username');
document.getElementById("Header_Login_tbUsername").value = s;
s2 = prompt('Enter your password');
document.getElementById("Header_Login_tbPassword").value = s2;
document.getElementById('Header_Login_btLogin').click();
a = prompt('Paste the link');
window.location.replace(a);
}
logIn();有关以下代码的更深入讨论,请查看QuirksMode: Cookies。
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + days * 86400000);
expires = "; expires =" + date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}https://stackoverflow.com/questions/26599230
复制相似问题