So I got this code for a website using Tampermonkey, and I want it to open a window in the center of the screen after the button is pressed. How can I do that?
(function() {
'use strict';
//button
var button = document.createElement("button");
button.setAttribute("class", "mdl-button mdl-js-button geofs-f-standard-ui");
button.setAttribute("id", "Button-toggle");
button.setAttribute("style", "background:clear");
button.innerHTML = "test";
document.getElementsByClassName("geofs-ui-bottom")[0].appendChild(button);
var buttonToggle = document.getElementById("Button-toggle");
//Event Listener
buttonToggle.addEventListener("click", function() {
buttonToggle.setAttribute("style", "background:green");
console.log("Button Pressed");
});
})();
You can open a new window with JavaScript It works on code pen well. https://codepen.io/ash_000001/pen/PoORmyg
You can change the position with this line of code:
const myWin = window.open("", "", "left=700, top=350, width=200, height=100");
function myFunction() {
const myWin = window.open("", "", "left=700, top=350, width=200, height=100");
let x = myWin.screenX;
let y = myWin.screenY;
document.getElementById("ao").innerHTML =
"myWin.screenX= " + x + "<br>myWin.screenY= " + y;
}
<button onclick="myFunction()">Open Window</button>
<p id="ao"></p>