Last active
October 16, 2022 11:28
-
-
Save scarf005/3251c47aa3a126008212df3045b9e0fa to your computer and use it in GitHub Desktop.
userscript utils
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
'use strict' | |
/** @param {string} s */ | |
export const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() | |
/** | |
* @param {string} cls | |
* @returns {HTMLElement[]} | |
*/ | |
export const getDomArray = (cls) => Array.from(document.querySelectorAll(cls)) | |
/** | |
* @param {string} tag | |
* @param {Object.<string, any>} attrs | |
*/ | |
export const createElement = (tag, attrs) => { | |
const elem = document.createElement(tag) | |
for (const [ key, value ] of Object.entries(attrs)) { | |
elem[ key ] = value | |
} | |
return elem | |
} | |
/** | |
* @param {string} selector | |
* @param {string} text | |
*/ | |
export const querySelectorIncludesText = (selector, text) => { | |
return getDomArray(selector) | |
.find(e => e.textContent.includes(text)) | |
} | |
/** @param {string} css */ | |
export const insertCSS = (css) => document.head.appendChild(createElement('style', { textContent: css })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment