-
-
Save shredthaGNAR/89794a8f3087cbde98c988fd3210204e to your computer and use it in GitHub Desktop.
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
// ==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