Created
September 7, 2023 05:58
-
-
Save roine/d6794a1b4f08f5fcf8c2f67ce63adfbf to your computer and use it in GitHub Desktop.
helpers to build user scripts
This file contains hidden or 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
/** | |
* Observe dom mutation and return a promise with the element when element is either present or appear on the page | |
*/ | |
function waitForElm(selector) { | |
return new Promise(resolve => { | |
if (document.querySelector(selector)) { | |
return resolve(document.querySelector(selector)); | |
} | |
const observer = new MutationObserver(mutations => { | |
if (document.querySelector(selector)) { | |
observer.disconnect(); | |
resolve(document.querySelector(selector)); | |
} | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
}); | |
} | |
/** | |
* Observe dom mutation and return a promise when the element is either not present or removed | |
*/ | |
function waitForElmRemoved(selector) { | |
return new Promise(resolve => { | |
if (!document.querySelector(selector)) { | |
return resolve(document.querySelector(selector)); | |
} | |
const observer = new MutationObserver(mutations => { | |
if (!document.querySelector(selector)) { | |
observer.disconnect(); | |
resolve(); | |
} | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
}); | |
} | |
/** | |
* Copy to clipboard | |
*/ | |
const copyToClipboard = (value) => { | |
const dummy = document.createElement(‘textarea’) | |
document.body.appendChild(dummy) | |
dummy.value = value | |
dummy.select() | |
document.execCommand(‘copy’) | |
document.body.removeChild(dummy) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment