Skip to content

Instantly share code, notes, and snippets.

@scarf005
Last active October 16, 2022 11:28
Show Gist options
  • Save scarf005/3251c47aa3a126008212df3045b9e0fa to your computer and use it in GitHub Desktop.
Save scarf005/3251c47aa3a126008212df3045b9e0fa to your computer and use it in GitHub Desktop.
userscript utils
'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