2014-10-28 191 views
0

我在TamperMonkey中有一个脚本,我想要运行一次然后停止。它应该提示用户然后填写框然后停止。但它一直在问......如何停止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(); 
+0

创建一个[cookie](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie)。 – 2014-10-28 00:55:41

+0

处理GreaseMonkey,但应该是便携式的[问题](http://stackoverflow.com/questions/13452626/create-a-cookie-with-javascript-in-greasemonkey)。 ***注意:**这个问题似乎是您的完美解决方案。* – 2014-10-28 00:57:59

+0

您将不得不提供详细信息。脚本运行的URL是什么?什么是你重定向的URL('location.replace(a)')?编辑问题以提供此信息。 – 2014-10-28 02:25:01

回答

1

您可以将后面的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); 
} 
+0

在创建cookie之前,您可能需要验证登录是否成功,否则它不会再次请求您。也就是说,直到你删除cookie。 – 2014-10-28 17:48:46