为什么我的Window.Tampermonkey脚本中的Close不起作用?

问题描述 投票:0回答:1
(function() {
    document.evaluate('/html/body/nav/section[4]/div/form[1]/input[4]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();
    Sleep(15);
    window.close();
})();

所以单击功能有效,但是一旦添加“睡眠”窗口,关闭将不再有效?我还在学习Javascript,所以有人可以帮忙吗?我要做的就是单击“投票”按钮,然后关闭活动选项卡。

javascript xpath sleep tampermonkey window.closed
1个回答
0
投票

尝试此功能,该功能将在元素存在时等待。

function waitWhileElementPresent(cssLocator, timeoutInSeconds) {
    var currentTime = new Date().getTime();
    var endTime = currentTime + timeoutInSeconds * 1000;
    var checkExist = setInterval(function () {
        if (document.querySelectorAll(cssLocator).length == 0) {
            clearInterval(checkExist);
            console.log('waited until element not present.');
            return;
        } else if (endTime < new Date().getTime()) {
            clearInterval(checkExist);
            console.log('element still found after ' + timeoutInSeconds + ' seconds.');
            return;
        } else { 
            console.log('waiting for element not present ...'); 
        } 
    }, 100);
}

这里是另一个等待该元素存在的函数

function waitUntilElementPresent(cssLocator, timeoutInSeconds) {
    var currentTime = new Date().getTime();
    var endTime = currentTime + timeoutInSeconds * 1000;
    var checkExist = setInterval(function () {
        if (document.querySelectorAll(cssLocator).length) {
            clearInterval(checkExist);
            return;
        } else if (endTime < new Date().getTime()) {
            clearInterval(checkExist);
            console.log('not found in specified time.');
            return;
        } else {
            console.log('waiting for element to be present…');
        } 
    }, 100); 
}
© www.soinside.com 2019 - 2024. All rights reserved.