Improved prompts factory.

This commit is contained in:
hackademix 2024-10-19 23:32:46 +02:00
parent e3feaee105
commit 21c818ce76
No known key found for this signature in database
GPG Key ID: 231A83AFDA9C2434
3 changed files with 76 additions and 34 deletions

View File

@ -37,58 +37,94 @@ var Prompts = (() => {
async open(data) { async open(data) {
promptData = data; promptData = data;
this.close(); this.close();
let url = browser.runtime.getURL("ui/prompt.html");
let {width, height, left, top, parent } = data.features; let {width, height, left, top, parent } = data.features;
let options = { let options = {
url: browser.runtime.getURL("ui/prompt.html"), url,
type: "popup", type: "popup",
width, }
height,
}; if (!parent) {
parent = await browser.windows.getCurrent();
}
if (UA.isMozilla) { if (UA.isMozilla) {
options.allowScriptsToClose = true; options.allowScriptsToClose = true;
} }
if (!("windows" in browser)) { if (!("windows" in browser)) {
// Android, most likely // Android, most likely
this.currentTab = await browser.tabs.create({url: options.url}); this.currentTab = await browser.tabs.create({url});
return; return;
} }
if (!parent) parent = await browser.windows.getCurrent()
let popup = this.currentWindow = await browser.windows.create(options); const centerOnParent = (dim) => {
const { width, height } = dim;
dim.left =
left === undefined
? Math.round(parent.left + (parent.width - width) / 2)
: left;
dim.top =
top === undefined
? Math.round(parent.top + (parent.height - height) / 2)
: top;
return dim;
};
if (width && height) {
let size = { width, height };
url += `?size=${JSON.stringify(size)}`;
if (parent) {
({ left, top } = Object.assign(options, centerOnParent(size)));
}
}
debug("Prompt pre-opening options", options, left, top, width, height); // DEV_ONLY
let popup = (this.currentWindow = await browser.windows.create(options));
if (parent) { if (parent) {
// center to the given parent window (default last focused browser tab) ({ left, top } = centerOnParent({
if (left === undefined) left = Math.round(parent.left + (parent.width - popup.width) / 2); width: width || popup.width,
if (top === undefined) top = Math.round(parent.top + (parent.height - popup.height) / 2); height: height || popup.height,
}));
} else { } else {
// features.parent explicitly nulled: use given left & top or default to auto-centering on main screen // use given left & top or default to auto-centering on main screen
if (left === undefined) ({left} = popup); if (left === undefined) ({ left } = popup);
if (top === undefined) ({top} = popup); if (top === undefined) ({ top } = popup);
} }
// work around for letterboxing changes (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882) debug("Prompt post-opening options", popup, options, left, top, width, height);
let {width: popupWidth, height: popupHeight} = popup;
if (width && height && (popupWidth !== width || popupHeight !== height)) {
left += Math.round((popupWidth - width) / 2);
top += Math.round((popupHeight - height) / 2);
await browser.windows.update(popup.id,
{left, top, width, height, focused: false});
}
for (let attempts = 2; attempts-- > 0;) { // work around for resistFingerprinting new window rounding (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882)
// position gets set only 2nd time, moz bug? if (
await browser.windows.update(popup.id, width &&
{left, top, focused: false}); height &&
} (popup.width !== width ||
if (parent) { popup.height !== height ||
await browser.windows.update(parent.id, {focused: true}); popup.left !== left ||
popup.top !== top)
) {
popup = await browser.windows.update(popup.id, {
left,
top,
width,
height,
});
for (let attempts = 2; attempts-- > 0; ) {
debug("Resizing", popup, { left, top, width, height }); // DEV_ONY
popup = await browser.windows.update(popup.id, { width, height });
if (popup.width == width || popup.height == height) {
break;
}
}
} }
} }
async close() { async close() {
if (this.currentWindow) { if (this.currentWindow) {
try { try {
await browser.windows.remove(this.currentWindow.id); await browser.windows.remove(this.currentWindow.id);
} catch (e) { } catch (e) {
debug(e);
} }
this.currentWindow = null; this.currentWindow = null;
} else if (this.currentTab) { } else if (this.currentTab) {

View File

@ -19,9 +19,8 @@
*/ */
(async () => { (async () => {
document.documentElement.classList.toggle("mobile", !!UA.mobile);
let data = await Messages.send("getPromptData"); let data = await Messages.send("getPromptData");
debug(data); debug("Prompt data", data);
if (!data) { if (!data) {
error("Missing promptData"); error("Missing promptData");
window.close(); window.close();

View File

@ -24,12 +24,19 @@ if ("windows" in browser) document.addEventListener("DOMContentLoaded", async e
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1402110 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1402110
let win = await browser.windows.getCurrent({populate: true}); let win = await browser.windows.getCurrent({populate: true});
if (win.tabs[0].url === document.URL) { if (win.tabs[0].url === document.URL) {
debug("Resize hack"); let size = decodeURIComponent(location.search).match(/\bsize=(\{\s*"width":[^}]+\})/);
try {
size = size && JSON.parse(size[1]);
} catch (e) {
size = null;
}
let {width, height} = size || win;
debug("Resize hack", win, size, width, height); // DEV_ONLY
await browser.windows.update(win.id, { await browser.windows.update(win.id, {
width: win.width + 1 width: width + 1, height
}); });
await browser.windows.update(win.id, { await browser.windows.update(win.id, {
width: win.width width, height
}); });
} }
}); });