函数在父 window 而不是子上运行

Function runs on parent window instead of child

这作为小书签运行。

创建 windows、提取值并将它们写入 window "log" 非常完美。

我想将 "log" window 的内容保存到 scrypt.txt,但保存的是父 window 的内容。我做错了什么?

javascript:
setInterval(logging,60000);
w1 = window.open("https://scrypt.cc/index.php");
log = window.open("");

function logging(){
    if(w1.document.body.innerHTML == 'Server is currently busy. Please try again later.'){
        w1.location.href = 'https://scrypt.cc/index.php';
        console.log("busy");
    }else{
        console.log("ok");
        log.document.body.innerHTML = '';
        var re=/var\s*dayprofitperkhs\s*=\s*([0-9\.]+)\s/gi;
        var matches=re.exec(w1.document.body.innerHTML);
        log.document.write(RegExp. + "<p></p>");
        log.document.write(w1.$('#t9_2').val() + "<p></p>");
        log.setTimeout(save,1000);
        w1.location.href = 'https://scrypt.cc/index.php';
    }
}
function save() {
    a = log.document.createElement('a');
    a.href = log.location.href;
    a.download = 'scrypt.txt';
    log.document.body.appendChild(a);
    a.click();
    a.parentNode.removeChild(a);
}

编辑:另存为 *.html,另存为 a.href 效果完美:

a.href = 'data:text/html;base64,' + btoa(log.document.body.outerHTML);
a.download = 'values.html';

您的 save() 函数正在使用主 window 的 document。如果你想让它在 log() 上下文中 运行,那么你需要使用 log.documentlog.location.

function save() {
    a = log.document.createElement('a');
    a.href = log.location.href;
    a.download = 'scrypt.txt';
    log.document.body.appendChild(a);
    a.click();
    a.parentNode.removeChild(a);
}

这只是一个猜测,但您可以尝试将所有保存代码移动到日志 window 中的脚本标记中,如下所示:

setInterval(logging, 60000);
var w1 = window.open("https://scrypt.cc/index.php");
var log = window.open("");

function logging() {
    if (w1.document.body.innerHTML == 'Server is currently busy. Please try again later.') {
        w1.location.href = 'https://scrypt.cc/index.php';
        console.log("busy");
    } else {
        console.log("ok");
        log.document.body.innerHTML = '';
        var re = /var\s*dayprofitperkhs\s*=\s*([0-9\.]+)\s/gi;
        var matches = re.exec(w1.document.body.innerHTML);
        log.document.write(RegExp. + "<p></p>");
        log.document.write(w1.$('#t9_2').val() + "<p></p>");

        // insert script tag to create the save link and click it
        var sc = "<scr" + "ipt>";
        sc += "function save() {";
        sc += "var a = document.createElement('a');";
        sc += "a.href = location.href;";
        sc += "a.download = 'scrypt.txt';";
        sc += "document.body.appendChild(a);";
        sc += "a.click();";
        sc += "a.parentNode.removeChild(a);";
        sc += "}";
        sc += "setTimeout(save, 1000);";
        sc += "</scr" + "ipt>";
        log.document.write(sc);

        w1.location.href = 'https://scrypt.cc/index.php';
    }
}

这是一个可行的解决方案:

javascript:
setInterval(logging,60000);
w1 = window.open("https://scrypt.cc/index.php");
log = window.open("");

function logging(){
    if(w1.document.body.innerHTML == 'Server is currently busy. Please try again later.'){
        w1.location.href = 'https://scrypt.cc/index.php';
        console.log("server busy");
    }else{
        log.document.body.innerHTML = '';
        var re=/var\s*dayprofitperkhs\s*=\s*([0-9\.]+)\s/gi;
        var matches=re.exec(w1.document.body.innerHTML);
        log.document.write("<p>" + RegExp. + "</p>");
        log.document.write("<p>" + w1.$('#t9_2').val() + "</p>");
        log.setTimeout(save,1000);
        w1.location.href = 'https://scrypt.cc/index.php';
    }
}
function save() {
    console.log("file saved");
    a = document.createElement('a');
    a.href = 'data:text/html;base64,' + btoa(log.document.body.outerHTML);
    a.download = 'values.html';
    document.body.appendChild(a);
    a.click();
    a.parentNode.removeChild(a);
}