Skip to content

Instantly share code, notes, and snippets.

@nire0510
Last active September 22, 2024 12:03
Show Gist options
  • Save nire0510/131fee254b7f21a7da1376f7ebf782f8 to your computer and use it in GitHub Desktop.
Save nire0510/131fee254b7f21a7da1376f7ebf782f8 to your computer and use it in GitHub Desktop.
Userscripts for Tampermonkey and such
// ==UserScript==
// @name Notion RTL
// @namespace https://rtl.notion.so/
// @version 1.01
// @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(/[א-ת\u0621-\u064A\u0660-\u0669]/g)?.length;
const nonHebCharsCount = layout.textContent.replace('Add iconAdd coverAdd comment', '').match(/[a-z]/ig)?.length;
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