2014-10-28 194 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