Copy Notion Database item property values by clicking Shift + LeftMouseButton on it
当前为
// ==UserScript==
// @name Notion Copy Properties
// @namespace https://www.notion.so/
// @version 0.7
// @description Copy Notion Database item property values by clicking Shift + LeftMouseButton on it
// @author Maxim Kurbatov
// @match https://www.notion.so/*
// @grant GM_addStyle
// @run-at document-end
// ==/UserScript==
var debug = false;
GM_addStyle(`#notion-app .notion-frame > div.notion-scroller.vertical.horizontal > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(1) > div > div:nth-child(2) > div:hover,
#notion-app div.notion-default-overlay-container div.notion-scroller.vertical > div:nth-child(2) > div:nth-child(3) > div:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(1) > div > div:nth-child(2) > div:hover
{ font-style: italic; }`);
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var target = false;
if ( mutation.target.nodeName == "DIV" ) target = mutation.target;
else target = mutation.target.parentElement;
if ( target.getAttribute("contentEditable") == "true" ) target = target.parentElement;
if ( ! target ) return;
addCopyHandler(target);
});
});
var config = { characterData: true, attributes: false, childList: true, subtree: true };
var mutgt = document.body;
observer.observe(mutgt, config);
var lastBlock = false;
function addCopyHandler(elm) {
elm = elm || document;
//if ( typeof elm.querySelectorAll !== 'function' ) return;
//var blocks = elm.querySelectorAll("#notion-app .notion-focusable");
const blocks = document.querySelectorAll(`#notion-app .notion-frame > div.notion-scroller.vertical.horizontal > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(1) > div > div:nth-child(2) > div,
#notion-app div.notion-default-overlay-container div.notion-scroller.vertical > div:nth-child(2) > div:nth-child(3) > div:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(1) > div > div:nth-child(2) > div`);
blocks.forEach(function(block) {
block.addEventListener('click', onClick);
});
}
function onClick(e) {
const elem = e.currentTarget;
console.log("Copying text: " + elem.innerText);
if (e.shiftKey) {
copyTextToClipboard(elem.innerText);
const color = elem.style.color;
elem.style.color = "red";
setTimeout(function() { elem.style.color = color; }, 300);
}
}
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}