Javascript 函数在父窗口而不是子窗口上运行

Javascript 函数在父窗口而不是子窗口上运行,javascript,debugging,createelement,Javascript,Debugging,Createelement,它作为bookmarklet运行 创建窗口、提取值并将其写入窗口日志的过程非常完美 我想将日志窗口的内容保存到scrypt.txt,但是父窗口的内容会被保存。我做错了什么 javascript: setInterval(logging,60000); w1 = window.open("https://scrypt.cc/index.php"); log = window.open(""); function logging(){ if(w1.document.body.innerHT

它作为bookmarklet运行

创建窗口、提取值并将其写入窗口日志的过程非常完美

我想将日志窗口的内容保存到scrypt.txt,但是父窗口的内容会被保存。我做错了什么

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.$1 + "<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);
}
保存功能正在使用主窗口的文档。如果希望它在日志上下文中运行,则需要使用log.document和log.location

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

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.$1 + "<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.$1 + "</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);
}

由于同源保护,除非您的主窗口也来自https://scrypt.cc,则无法访问https://scrypt.cc/index.php 我从那个站点运行bookmarklet,所以它基本上是在w1中打开的,只是为了能够刷新它。在另一个窗口中获取这些值效果很好,但保存该窗口不起作用,它保存了主窗口。您是否尝试将a元素添加到w1的文档而不是父窗口的文档中?@Prusse:我该怎么做?log.document.body.appendChilda;部分=相同的结果。。。父窗口的内容获取grabbed@WannesDemaeght-日志窗口中是否有正确的内容?如果是这样,那么问题在于你的下载操作,而你并没有真正透露。另外,请显示修复保存功能,以便我们查看您是否正确修复了它。正确的内容将显示在“日志”窗口中,如果不清楚,请道歉。我将使用固定的“保存”功能进行更新。你说我没有透露下载操作是什么意思?”“保存”是下载操作…@wannesdemageht-单击操作在保存中实际执行的操作。大概,这就是窗口保存的方式。但是,我们看不到任何代码。@wannesdemageht-您缺少log.document.body.appendChilda;
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.$1 + "<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.$1 + "</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);
}