Created
April 17, 2020 09:51
-
-
Save sulami/3e00205bdb28856ad4b470124755ab6a to your computer and use it in GitHub Desktop.
Jisho: Copy with Reading
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 Jisho.org copy with reading | |
// @namespace https://jisho.org/search/* | |
// @version 0.4 | |
// @description Quickly copies a word with the reading to the clipboard | |
// @author Robin Schroer (originally Jace Sangco) | |
// @match https://jisho.org/search/* | |
// @grant none | |
// ==/UserScript== | |
var meaningSeperator = " "; | |
var kanjiHiraganaMeaningSeperator = " "; | |
function copyTextToClipboard(text) { | |
var textArea = document.createElement("textarea"); | |
textArea.style.position = 'fixed'; | |
textArea.style.top = 0; | |
textArea.style.left = 0; | |
textArea.style.width = '2em'; | |
textArea.style.height = '2em'; | |
textArea.style.padding = 0; | |
textArea.style.border = 'none'; | |
textArea.style.outline = 'none'; | |
textArea.style.boxShadow = 'none'; | |
textArea.style.background = 'transparent'; | |
textArea.value = text; | |
document.body.appendChild(textArea); | |
textArea.select(); | |
try { | |
var successful = document.execCommand('copy'); | |
var msg = successful ? 'successful' : 'unsuccessful'; | |
console.log('Copying text command was ' + msg); | |
} catch (err) { | |
console.log('Oops, unable to copy'); | |
alert("Unable to copy"); | |
} | |
document.body.removeChild(textArea); | |
} | |
function getMeaningString(kotobaDiv) { | |
var meanings = kotobaDiv.querySelectorAll(".meaning-meaning"); | |
var meaningStr = Array.prototype.reduce.call(meanings, function(acc, meaning, i){ | |
var textNodeType = 3; | |
if (meaning.childNodes[0].nodeType !== textNodeType) { // Ignore non-raw-text classes beacuse they don't have actual meaning | |
return acc; | |
} | |
return acc + (i + 1) + ". " + meaning.innerHTML + meaningSeperator; | |
}, "").trim(); | |
return meaningStr; | |
} | |
function getKanjiString(kotobaDiv) { | |
var kanjiStr = ""; | |
var elements = kotobaDiv.querySelectorAll(".f-dropdown"); | |
Array.prototype.forEach.call(elements, function(ul, i){ | |
var li = ul.childNodes[0]; | |
var a = li.childNodes[0]; | |
if (a.innerHTML.indexOf("Sentence search for") > -1) { // Ignore anything that doesn't start with this magic string | |
kanjiStr = a.innerHTML.replace("Sentence search for","").trim(); | |
} | |
}); | |
return kanjiStr; | |
} | |
function getHiraganaStr(kotobaDiv) { | |
var hiraganaStr = ""; | |
var elements = kotobaDiv.querySelectorAll(".f-dropdown"); | |
Array.prototype.forEach.call(elements, function(ul, i){ | |
if (ul.childNodes.length > 1) { | |
var li = ul.childNodes[1]; | |
var a = li.childNodes[0]; | |
if (a.innerHTML.indexOf("Sentence search for") > -1) { // Ignore anything that doesn't start with this magic string | |
hiraganaStr = a.innerHTML.replace("Sentence search for","").trim(); | |
} | |
} | |
}); | |
return hiraganaStr; | |
} | |
function addCopyLink(kotobaDiv) { | |
var kanjiStr = getKanjiString(kotobaDiv); | |
var hiraganaStr = getHiraganaStr(kotobaDiv); | |
var meaningStr = getMeaningString(kotobaDiv); | |
var copyStr = (hiraganaStr == "") ? kanjiStr : // + kanjiHiraganaMeaningSeperator // + meaningStr : | |
kanjiStr + kanjiHiraganaMeaningSeperator + "【" + hiraganaStr + "】"; // + kanjiHiraganaMeaningSeperator + meaningStr; | |
var a = document.createElement("a"); | |
a.innerHTML = "Copy to clipboard"; | |
a.addEventListener("click", function() { copyTextToClipboard(copyStr);}); | |
var li = document.createElement("li"); | |
li.appendChild(a); | |
var dropdowns = kotobaDiv.querySelectorAll(".f-dropdown"); | |
Array.prototype.forEach.call(dropdowns, function(ul){ | |
ul.insertBefore(li, ul.firstChild); | |
}); | |
} | |
function addAllCopyLinksToPage() { | |
var exactBlockQuery = document.querySelectorAll(".exact_block"); | |
if (exactBlockQuery.length > 0) { | |
var exactBlock = exactBlockQuery[0].childNodes; | |
Array.prototype.forEach.call(exactBlock, function(el){ | |
if (el.nodeName.toLowerCase() == "div") { | |
addCopyLink(el); | |
} | |
}); | |
} | |
var conceptsQuery = document.querySelectorAll(".concepts"); | |
if (conceptsQuery.length > 0) { | |
var concepts = conceptsQuery[0].childNodes; | |
Array.prototype.forEach.call(concepts, function(el){ | |
if (el.nodeName.toLowerCase() == "div") { | |
addCopyLink(el); | |
} | |
}); | |
} | |
} | |
addAllCopyLinksToPage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment