diff --git a/repo/icon-sets/client.mjs b/repo/icon-sets/client.mjs index 1cc4aa8..61d0aa2 100644 --- a/repo/icon-sets/client.mjs +++ b/repo/icon-sets/client.mjs @@ -6,8 +6,17 @@ * (https://notion-enhancer.github.io/) under the MIT license */ +const getImgData = (url) => + new Promise(async (res, rej) => { + const blob = await fetch(url).then((res) => res.blob()), + reader = new FileReader(); + reader.onload = (e) => res(e.target.result); + reader.readAsDataURL(blob); + }); + export default async function ({ web, fs, components, notion }, db) { const recentUploads = await db.get(['recent_uploads'], []), + preventQualityReduction = await db.get(['prevent_quality_reduction']), $triangleSvg = web.html` `; @@ -131,7 +140,7 @@ export default async function ({ web, fs, components, notion }, db) { recentUploads.splice(i, 1); db.set(['recent_uploads'], recentUploads); $icon.remove(); - } else setIcon(url); + } else setIcon({ signed, url }); }); loadPromises.push( new Promise((res, rej) => { @@ -174,7 +183,7 @@ export default async function ({ web, fs, components, notion }, db) { $icon = web.render(web.html``, $img); web.render($set, $icon); $icon.addEventListener('click', (event) => { - if (!event.shiftKey) setIcon(iconUrl); + if (!event.shiftKey) setIcon({ signed: iconUrl, url: iconUrl }); }); if (!sprite) { loadPromises.push( @@ -253,12 +262,15 @@ export default async function ({ web, fs, components, notion }, db) { // otherwise both may be visible on reopen displayEmojiTab(true); - async function setIcon(iconUrl) { + async function setIcon({ signed, url }) { // without this react gets upset displayEmojiTab(); $linkTab.firstChild.click(); await new Promise(requestAnimationFrame); + $mediaMenu.parentElement.style.opacity = '0'; + const iconUrl = preventQualityReduction ? await getImgData(signed) : url; + // call native setter, imitate human input const $notionLinkInput = $mediaMenu.querySelector(mediaLinkInputSelector), proto = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value'); @@ -276,8 +288,9 @@ export default async function ({ web, fs, components, notion }, db) { const submitLinkIcon = () => { const url = $iconsLinkInput.firstElementChild.value; if (!url) return; - setIcon(url); - recentUploads.push({ signed: notion.sign(url, notion.getPageID()), url: url }); + const icon = { signed: notion.sign(url, notion.getPageID()), url: url }; + setIcon(icon); + recentUploads.push(icon); db.set(['recent_uploads'], recentUploads); }; $iconsLinkInput.onkeyup = (event) => { @@ -289,9 +302,10 @@ export default async function ({ web, fs, components, notion }, db) { $iconsUploadSubmit.onclick = () => $iconsUploadFile.click(); $iconsUploadFile.onchange = async (event) => { const file = event.target.files[0], - url = await notion.upload(file); - setIcon(url); - recentUploads.push({ signed: notion.sign(url, notion.getPageID()), url: url }); + url = await notion.upload(file), + icon = { signed: notion.sign(url, notion.getPageID()), url: url }; + setIcon(icon); + recentUploads.push(icon); db.set(['recent_uploads'], recentUploads); }; }; diff --git a/repo/icon-sets/mod.json b/repo/icon-sets/mod.json index 258294c..ff163cc 100644 --- a/repo/icon-sets/mod.json +++ b/repo/icon-sets/mod.json @@ -26,6 +26,13 @@ "label": "load default icon sets from github", "value": true }, + { + "type": "toggle", + "key": "prevent_quality_reduction", + "label": "prevent icon quality reduction", + "tooltip": "**this may break icons in widgets** - encodes and submits images as data urls to prevent notion from optimising them (reduces image quality by ~20%)", + "value": true + }, { "type": "file", "key": "json",