您可以将后面的cookie函数添加到脚本中以创建,检查和删除cookie。
每个脚本运行时,请检查是否存在饼干:
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);
}
创建一个[cookie](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie)。 – 2014-10-28 00:55:41
处理GreaseMonkey,但应该是便携式的[问题](http://stackoverflow.com/questions/13452626/create-a-cookie-with-javascript-in-greasemonkey)。 ***注意:**这个问题似乎是您的完美解决方案。* – 2014-10-28 00:57:59
您将不得不提供详细信息。脚本运行的URL是什么?什么是你重定向的URL('location.replace(a)')?编辑问题以提供此信息。 – 2014-10-28 02:25:01