Hope everyone is having a great day! I was trying to create a bookmarklet that automatically opens links on the page with POST headers, yet only the last link is extended into a new page. Is there any reasons why to this?(If so, how could I look about fixing it?)
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: "-"
});
}
}
})();
Thank you!
Your JavaScript code runs as though it were code in the page (as you probably know), both with its privileges and its restrictions.
Browsers don't allow JavaScript code in pages to open an unlimited number of windows, because when they used to allow that, it was abused by nefarious sites. So when code on a page tries to do that, the browser prevents it. The specifics of how it prevents it and to what degreee it allows things to happen are browser-specific.
You'll probably need to have a user-event per window you want to open (e.g., trigger the bookmarklet repeatedly, and have the bookmarklet do one link at a time).