首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止TamperMonkey中的函数

如何停止TamperMonkey中的函数
EN

Stack Overflow用户
提问于 2014-10-28 08:52:36
回答 1查看 703关注 0票数 0

我在TamperMonkey中有一个脚本,我想运行一次然后停止。它应该提示用户,然后填写方框,然后停止。但它一直在问...

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2014-10-28 09:14:41

您可以将后一个cookie函数添加到脚本中,以创建、检查和删除cookie。

每次运行脚本时,检查cookies是否存在:

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26599230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档