Last active
September 23, 2024 11:39
-
-
Save nire0510/131fee254b7f21a7da1376f7ebf782f8 to your computer and use it in GitHub Desktop.
Userscripts for Tampermonkey and such
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Notion RTL | |
// @namespace https://rtl.notion.so/ | |
// @version 1.02 | |
// @description RTL support for Notion web application | |
// @author Nir Elbaz | |
// @match https://www.notion.so/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=notion.so | |
// @grant none | |
// ==/UserScript== | |
(async function() { | |
let observer; | |
window.addEventListener('beforeunload', () => { | |
observer.disconnect(); | |
}); | |
window.navigation.addEventListener('navigate', () => { | |
observer.disconnect(); | |
run(); | |
}); | |
run(); | |
function setDirection(layout) { | |
const hebCharsCount = layout.textContent.match(/[\u0591-\u07FF]/g)?.length; | |
const nonHebCharsCount = layout.textContent.replace('Add iconAdd coverAdd comment', '')?.length - hebCharsCount; | |
if (hebCharsCount > nonHebCharsCount || (hebCharsCount > 0 && !nonHebCharsCount)) { | |
layout.style.direction = 'rtl'; | |
} | |
else { | |
layout.style.direction = 'ltr'; | |
} | |
} | |
function debounce(func, timeout = 1000) { | |
let timer; | |
return (...args) => { | |
clearTimeout(timer); | |
timer = setTimeout(() => { func.apply(this, args); }, timeout); | |
}; | |
} | |
function sleep(ms) { | |
return new Promise((resolve, reject) => { | |
window.setTimeout(() => resolve(), ms); | |
}); | |
} | |
async function run() { | |
let layout; | |
while (!layout) { | |
await sleep(500); | |
layout = document.querySelector('.layout'); | |
} | |
const config = { attributes: false, childList: true, subtree: true }; | |
const debouncedSetDirection = debounce(() => setDirection(layout)); | |
const callback = (mutationList, observer) => { debouncedSetDirection(); }; | |
observer = new MutationObserver(callback); | |
observer.observe(layout, config); | |
setDirection(layout); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment