带有帖子的JavaScript书签中的新标签



希望每个人都过得愉快!我试图创建一个书签,该书签会自动在页面上使用发布标头打开链接,但只有最后一个链接扩展到了一个新页面。有什么原因吗?(如果是这样,我该如何修复它?)

javascript: (function() {
        function openWindowWithPost(url, data) {
            var form = document.createElement("form");
            form.target = "_blank";
            form.method = "POST";
            form.action = url;
            form.style.display = "none";
            for (var key in data) {
                var input = document.createElement("input");
                input.type = "hidden";
                input.name = key;
                input.value = data[key];
                form.appendChild(input);
            }
            document.body.appendChild(form);
            form.submit();
            document.body.removeChild(form);
        }
        var els = document.getElementsByTagName("a");
        for (var i = 0, l = els.length; i < l; i++) {
            var el = els[i];
            console.log(typeof el.href);
            console.log(el.href);
            if (el.href.startsWith('example.com')) {
                console.log(el.href.slice(39));
                openWindowWithPost("example.php", {
                    id: el.href.slice(39),
                    pdf: "-"
                });
            }
        }
    })();

谢谢!

您的JavaScript代码正常运行,好像是页面中的代码(您可能知道),既有其特权 and 的限制。

浏览器不允许在页面中的JavaScript代码打开无限数量的窗口,因为当它们允许时,它会被邪恶站点滥用。因此,当页面上的代码尝试执行此操作时,浏览器会防止它。的细节它如何防止它,并且允许发生的事情是浏览器特定的。

您可能需要一个要打开的每个窗口的用户事件(例如,重复触发书签,并让Bookmarklet一次执行一个链接)。

最新更新