Last active
April 4, 2023 00:13
-
-
Save pacochi/0568e742d53902f99f533aa1b4ed51e2 to your computer and use it in GitHub Desktop.
Tangolf の結果コピー
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 Tangolf の結果コピー | |
// @namespace hen.acho.co | |
// @match https://tangolf.hatelabo.jp/ | |
// @version 1.230403 | |
// @description ツイートボタンをコピーボタンにします。 | |
// @downloadURL https://gist.github.com/pacochi/0568e742d53902f99f533aa1b4ed51e2/raw/tangolf_copy.user.js | |
// @run-at document-idle | |
// @grant none | |
// ==/UserScript== | |
(() => { | |
const getTweetButton = (node = document) => node?.querySelector?.('a[class^="_shareButton_"]'); | |
const getResetButton = (node = document) => node?.querySelector?.('button[class^="_resetButton_"]'); | |
const observeBody = e => { | |
new MutationObserver((mutations, observer) => { | |
if ( | |
mutations.reduce((nodes, mutation) => nodes.concat([...mutation.addedNodes]), []) | |
.find(node => getTweetButton(node)) | |
) { | |
observer.disconnect(); | |
replaceButton(); | |
} | |
}).observe(document.body, { childList: true, subtree: true }); | |
}; | |
const addObserver = () => { | |
const resetButton = getResetButton(); | |
resetButton.addEventListener('click', observeBody, {once: true}); | |
}; | |
const replaceButton = () => { | |
const tweetButton = getTweetButton(); | |
tweetButton.innerText = 'スコアをコピー!'; | |
tweetButton.addEventListener('click', copyResult); | |
addObserver(); | |
}; | |
const copyResult = e => { | |
const params = (new URL(e.target.href)).searchParams; | |
const result = params.get('text') ?? ''; | |
const hash = params.get('hashtags') ? '\n#' + params.get('hashtags').replace(/,/, ' #') : ''; | |
const url = params.get('url') ? '\n' + params.get('url') : ''; | |
navigator.clipboard.writeText(result + url + hash); | |
e.target.innerText = 'スコアをコピー🆗'; | |
e.stopPropagation(); | |
e.preventDefault(); | |
}; | |
if (!getTweetButton()) observeBody(); | |
else replaceButton(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment