Last active
February 16, 2024 16:50
-
-
Save flozz/9afe6c76b67b82cfd1c3eddac10640c9 to your computer and use it in GitHub Desktop.
Lock all visited pages on Notion.io
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 - Lock pages (FR) | |
// @include *://www.notion.so/*/* | |
// @version 2 | |
// @grant none | |
// ==/UserScript== | |
// I am bored that nobody lock pages on Notion. Edition mode interferes with my | |
// VIM plugin and I accidentally modify pages instead of navigating... | |
// | |
// I created this Greasemonkey script to automatically lock each visited page. | |
// This script is licensed under WTFPL 1.0. TL;DR: Do what the fuck you want | |
// with it. | |
let _LAST_URL = null; | |
function getDotMenu() { | |
return document.getElementsByClassName("notion-topbar-more-button")[0]; | |
} | |
function getLockMenuitem() { | |
for (let element of document.querySelectorAll('[role="menuitem"]')) { | |
if (element.innerText.includes("Verrouiller")) { // French for "Lock" | |
return element; | |
} | |
} | |
return null; | |
} | |
function getOverlay() { | |
return document.getElementsByClassName("notion-overlay-container")[0]?.children[1]?.children[0]?.children[0]; | |
} | |
function defer(fn) { | |
setTimeout(fn, 0); | |
} | |
function isPageLocked() { | |
return Boolean(document.querySelectorAll(".notion-topbar .lockedFilled + div").length); | |
} | |
function doLockPage() { | |
if (isPageLocked()) { | |
return; | |
} | |
const menuElement = getDotMenu(); | |
if (!menuElement) { | |
return; | |
} | |
menuElement.click(); | |
defer(() => { | |
const menuitemElement = getLockMenuitem(); | |
if (!menuitemElement) { | |
return; | |
} | |
menuitemElement.click(); | |
}); | |
defer(() => { | |
const overlayElement = getOverlay(); | |
if (!overlayElement) { | |
return; | |
} | |
overlayElement.click(); | |
}); | |
} | |
function navigationWatcher() { | |
if (window.location.href !== _LAST_URL) { | |
setTimeout(doLockPage, 1000); | |
_LAST_URL = window.location.href; | |
} | |
setTimeout(navigationWatcher, 500); | |
} | |
// Wait Notion is fully loaded... | |
setTimeout(navigationWatcher, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment