Skip to content

Instantly share code, notes, and snippets.

@KendraTang
Last active December 23, 2021 05:24
Show Gist options
  • Save KendraTang/60a555314209109912545cda3dcbe597 to your computer and use it in GitHub Desktop.
Save KendraTang/60a555314209109912545cda3dcbe597 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Copy Shopify
// @version 0.1
// @description Add copy button to shopify admin-api graphql schemas
// @author tkain
// @match https://shopify.dev/docs/admin-api/graphql/reference/*
// @grant GM_setClipboard
// ==/UserScript==
const parseAndCopy = (table) => {
let list = []
for (const row of table.getElementsByTagName('tr')) {
const rowStrs = []
const [field, desc] = Array.from(row.getElementsByTagName('td'))
if (desc.children.length > 1) {
rowStrs.push(`# ${desc.children[1].innerText.replace(/\s/g, ' ')}`)
}
rowStrs.push(`"${desc.children[0].innerText}"`)
const m = field.innerText.match(/(\w+)\s\(\s*(.+)\s*\)/)
rowStrs.push(`${m[1]}: ${m[2]}`)
const rowStr = rowStrs.join('\n')
if (m[1] === 'id') {
list.unshift(rowStr)
} else {
list.push(rowStr)
}
}
const s = list.join('\n\n')
GM_setClipboard(s, 'text')
}
const insertButtons = () => {
for (const tableContainer of document.querySelectorAll('.fields, .arguments')) {
const button = document.createElement('button')
button.innerText = 'Copy'
button.setAttribute('style', `box-shadow: 1px 1px 0 2px #72c8f3;
padding: 2px 8px;
border-radius: 3px;
margin: 8px 0;
color: #24aef3`)
button.addEventListener('click', () => parseAndCopy(tableContainer))
tableContainer.prepend(button)
}
}
insertButtons()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment