The day card's copy button showed "Cannot read properties of undefined (reading 'write')" in magpie web opened over plain http (a LAN address) or in a webview with no async Clipboard API: pageshare.js called navigator.clipboard.write unguarded. It now tries the API when it exists, else copies the picture as a selected <img> with the copy command, else throws a message that says to save it instead (translated in zh, zh-TW, ja, de). The desktop window was not affected: it already hands the PNG to /api/plugin-pages/image. app.js's text copy already falls back (/api/copy, writeText, execCommand), so no sibling is left on the raw API. Run on mote: page-image-no-clipboard.test.cjs (new, chromium and webkit, en and zh, navigator.clipboard removed), 4 of 4 red on the old pageshare.js and green after; plugin-page.test.cjs and i18n-keys.test.cjs green. go test ./internal/gui: only TestCoreScriptBudget red, on main already (#5918); pageshare.js and i18n.js are not counted in it. Not tried: a real http LAN page pasting the image into another app; the execCommand path was observed selecting the PNG, not read back from the OS clipboard.
79 lines
3.8 KiB
JavaScript
79 lines
3.8 KiB
JavaScript
// A picture a plugin's page drew (magpie.image.copy, magpie.image.save,
|
|
// plugin-runtime.md): its frame can't download a file or reach the
|
|
// clipboard, and the app's window can't copy a picture, so magpie does it
|
|
// (pageimage.go); in magpie web this server is another computer, and the
|
|
// browser's window does it here. pluginpages.js imports this the first
|
|
// time a page shares one (or asks for a file), so it weighs nothing on the core.
|
|
|
|
// A file of the page's own folder, for a page that cannot ask for it itself
|
|
// (a proxy that signs readers in by cookie turns its frame away,
|
|
// pageinline.go); this request carries the window's cookie. page is the
|
|
// page's own address, want the file's.
|
|
window.pageAsset = async function (page, want) {
|
|
const dir = new URL(page.replace(/[^/]*$/, ""), location.href).href;
|
|
const u = new URL(String(want || ""), location.href);
|
|
if (!u.href.startsWith(dir) || u.hash) throw new Error(t("No such call: {what}", { what: "asset" }));
|
|
const res = await fetch(u.href, { credentials: "same-origin" });
|
|
if (!res.ok) throw new Error(String(res.status));
|
|
return { type: res.headers.get("content-type") || "", data: await res.arrayBuffer() };
|
|
};
|
|
|
|
window.pageShare = async function (spec, save, params) {
|
|
const png = params?.png;
|
|
if (!(png instanceof Blob) || png.type !== "image/png" || !png.size || png.size > 16 << 20) throw new Error(t("A picture is a PNG of 16 MB at most"));
|
|
const name = String(params?.name || "").slice(0, 120);
|
|
if (web) {
|
|
if (save) {
|
|
const a = document.createElement("a");
|
|
a.href = URL.createObjectURL(png);
|
|
a.download = (name.replace(/[<>:"/\\|?*\u0000-\u001f]/g, "-").replace(/\.png$/i, "").trim() || "magpie") + ".png";
|
|
a.click();
|
|
setTimeout(() => URL.revokeObjectURL(a.href), 60e3);
|
|
return { path: "" };
|
|
}
|
|
// navigator.clipboard is missing on plain http (a LAN address) and in
|
|
// some webviews: the picture is copied as a selected <img> instead, and
|
|
// when neither works the page is told to save, not handed a TypeError
|
|
if (navigator.clipboard?.write && window.ClipboardItem) {
|
|
try { await navigator.clipboard.write([new ClipboardItem({ "image/png": png })]); return { ok: true }; } catch {}
|
|
}
|
|
if (await copyPicture(png)) return { ok: true };
|
|
throw new Error(t("This browser can't copy a picture on this address (it needs https or localhost). Save it instead."));
|
|
}
|
|
const bytes = new Uint8Array(await png.arrayBuffer());
|
|
let bin = "";
|
|
for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
|
|
return api("plugin-pages/image", { spec, action: save ? "save" : "copy", name, png: btoa(bin) });
|
|
};
|
|
|
|
// copyPicture selects an <img> of the PNG in a box out of sight and uses the
|
|
// page's copy command, which puts the picture on the clipboard where the
|
|
// async API is missing; it says whether the browser took the copy.
|
|
async function copyPicture(png) {
|
|
const url = await new Promise((res) => { const r = new FileReader(); r.onload = () => res(String(r.result)); r.onerror = () => res(""); r.readAsDataURL(png); });
|
|
if (!url) return false;
|
|
const was = document.activeElement;
|
|
const box = document.createElement("div");
|
|
box.contentEditable = "true";
|
|
box.style.cssText = "position:fixed;top:0;left:0;width:1px;height:1px;opacity:0;overflow:hidden;pointer-events:none";
|
|
const img = new Image();
|
|
img.src = url;
|
|
box.append(img);
|
|
document.body.append(box);
|
|
let ok = false;
|
|
try {
|
|
await img.decode().catch(() => {});
|
|
box.focus({ preventScroll: true });
|
|
const sel = getSelection();
|
|
const r = document.createRange();
|
|
r.selectNode(img);
|
|
sel.removeAllRanges();
|
|
sel.addRange(r);
|
|
ok = document.execCommand("copy");
|
|
sel.removeAllRanges();
|
|
} catch { ok = false; }
|
|
box.remove();
|
|
was?.focus?.({ preventScroll: true });
|
|
return ok;
|
|
}
|