#119 emoji sets achieve without user agent

This commit is contained in:
dragonwocky 2020-09-26 16:48:36 +10:00
parent 03c12b137d
commit 40336e78c1
4 changed files with 107 additions and 51 deletions

View File

@ -22,15 +22,15 @@ module.exports = {
const notion_elem = document.querySelector('.notion-app-inner'); const notion_elem = document.querySelector('.notion-app-inner');
if (!notion_elem) return; if (!notion_elem) return;
clearInterval(attempt_interval); clearInterval(attempt_interval);
const observer = new MutationObserver(process); const observer = new MutationObserver(handle);
observer.observe(notion_elem, { observer.observe(notion_elem, {
childList: true, childList: true,
subtree: true, subtree: true,
}); });
let pageHistory = []; let pageHistory = [];
process(); handle();
function process(list, observer) { function handle(list, observer) {
const pageID = (location.search const pageID = (location.search
.slice(1) .slice(1)
.split('&') .split('&')

View File

@ -55,13 +55,13 @@ module.exports = {
); );
}); });
process(); handle();
const observer = new MutationObserver(process); const observer = new MutationObserver(handle);
observer.observe(notion_elem, { observer.observe(notion_elem, {
childList: true, childList: true,
subtree: true, subtree: true,
}); });
function process(list, observer) { function handle(list, observer) {
if (document.querySelector('#calendar-scroll-to-week')) return; if (document.querySelector('#calendar-scroll-to-week')) return;
const arrow = document.querySelector( const arrow = document.querySelector(
'.notion-selectable.notion-collection_view_page-block .chevronLeft' '.notion-selectable.notion-collection_view_page-block .chevronLeft'

View File

@ -11,7 +11,7 @@ module.exports = {
tags: ['extension'], tags: ['extension'],
name: 'emoji sets', name: 'emoji sets',
desc: 'pick from a variety of emoji styles to use.', desc: 'pick from a variety of emoji styles to use.',
version: '0.2.0', version: '0.3.0',
author: 'dragonwocky', author: 'dragonwocky',
options: [ options: [
{ {
@ -38,28 +38,13 @@ module.exports = {
], ],
hacks: { hacks: {
'renderer/preload.js'(store, __exports) { 'renderer/preload.js'(store, __exports) {
const useNative =
(store().style === 'microsoft' && process.platform === 'win32') ||
(store().style === 'apple' && process.platform === 'darwin');
Object.defineProperty(navigator, 'userAgent', {
get: function () {
// macOS useragent uses system emojis instead of images
// = no need to download
return useNative
? 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Safari/605.1.15 Notion/2.0.9 Electron/6.1.5'
: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Notion/2.0.9 Chrome/76.0.3809.146 Electron/6.1.5 Safari/537.36';
},
});
if (!useNative) {
let tweaked = false; let tweaked = false;
document.addEventListener('readystatechange', (event) => { document.addEventListener('readystatechange', (event) => {
if (document.readyState !== 'complete') return false; if (document.readyState !== 'complete') return false;
let queue = []; let queue = [];
const observer = new MutationObserver((list, observer) => { const observer = new MutationObserver((list, observer) => {
if (!queue.length) requestAnimationFrame(process); if (!queue.length) requestAnimationFrame(handle);
queue.push(...list); queue.push(...list);
}); });
observer.observe(document.body, { observer.observe(document.body, {
@ -67,29 +52,95 @@ module.exports = {
subtree: true, subtree: true,
characterData: true, characterData: true,
}); });
function process() { function handle() {
queue = []; queue = [];
if (store().style !== 'twitter' || tweaked) { const isMac = process.platform === 'darwin',
native =
(store().style === 'microsoft' && process.platform === 'win32') ||
(store().style === 'apple' && isMac);
if (store().style !== (isMac ? 'apple' : 'twitter') || tweaked) {
if (isMac) {
if (native) {
document
.querySelectorAll('span[role="image"][aria-label]')
.forEach((el) => {
el.style.background = '';
el.style.color = 'currentColor';
});
} else {
document
.querySelectorAll('span[role="image"][aria-label]')
.forEach((el) => {
if (!el.style.background.includes(store().style)) {
el.style.background = `url(https://emojicdn.elk.sh/${el.getAttribute(
'aria-label'
)}?style=${store().style})`;
el.style.width = el.parentElement.style.fontSize;
el.style.backgroundSize = 'contain';
el.style.backgroundRepeat = 'no-repeat';
el.style.color = 'transparent';
}
});
}
} else {
document document
.querySelectorAll( .querySelectorAll(
'[src*="notion-emojis.s3"]:not(.notion-emoji)' '[src*="notion-emojis.s3"]:not(.notion-emoji)'
) )
.forEach((el) => el.remove()); .forEach((el) => el.remove());
if (native) {
document.querySelectorAll('.notion-emoji').forEach((el) => { document.querySelectorAll('.notion-emoji').forEach((el) => {
el.style.setProperty( if (
'background', el.parentElement.querySelectorAll(
`url(https://emojicdn.elk.sh/${el.getAttribute( 'span[role="image"][aria-label]'
'alt' ).length !==
)}?style=${store().style})` el.parentElement.querySelectorAll('.notion-emoji').length
) {
el.insertAdjacentHTML(
'beforebegin',
`<span
role="image"
aria-label="${el.getAttribute('alt')}"
style='font-family: "Apple Color Emoji", "Segoe UI Emoji",
NotoColorEmoji, "Noto Color Emoji", "Segoe UI Symbol",
"Android Emoji", EmojiSymbols; line-height: 1em;'
>${el.getAttribute('alt')}</span>`
); );
el.style.setProperty('background-size', 'contain'); el.style.display = 'none';
el.style.setProperty('opacity', '1'); if (el.parentElement.getAttribute('contenteditable'))
el.remove();
}
}); });
} else {
document.querySelectorAll('.notion-emoji').forEach((el) => {
el.parentElement
.querySelectorAll('span[role="image"][aria-label]')
.forEach((text) => text.remove());
if (!el.style.background.includes(store().style)) {
el.style.background = `url(https://emojicdn.elk.sh/${el.getAttribute(
'aria-label'
)}?style=${store().style})`;
el.style.display = 'inline-block';
el.style.backgroundSize = 'contain';
el.style.backgroundRepeat = 'no-repeat';
el.style.opacity = 1;
}
});
}
}
tweaked = true; tweaked = true;
} }
} }
}); });
}
}, },
}, },
}; };
// span[role="image"][aria-label]
/* */
// <div style="position: relative; width: 36px; height: 36px;">
// <img class="notion-emoji" alt="😀" aria-label="😀" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" style="width: 36px; height: 36px; background: url(&quot;/images/twitter-emoji-spritesheet-64.png&quot;) 53.5714% 62.5% / 5700% 5700%; opacity: 1; transition: opacity 100ms ease-out 0s;">
// <img alt="😀" aria-label="😀" src="https://notion-emojis.s3-us-west-2.amazonaws.com/v0/svg-twitter/1f600.svg" style="position: absolute; top: 0px; left: 0px; opacity: 0; width: 36px; height: 36px;">
// </div>
// <img class="notion-emoji" alt="✝" aria-label="✝" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" style="width: 100%; height: 100%; background: url(&quot;/images/twitter-emoji-spritesheet-64.png&quot;) 98.2143% 25% / 5700% 5700%;">

View File

@ -0,0 +1,5 @@
.notion-emoji::after {
content: attr(aria-label, '');
width: 1em;
height: 1em;
}