Skip to content

Instantly share code, notes, and snippets.

@lowteq
Last active August 27, 2021 10:57
Show Gist options
  • Save lowteq/ae6e0c71952c84d7d20d266e1a093b6d to your computer and use it in GitHub Desktop.
Save lowteq/ae6e0c71952c84d7d20d266e1a093b6d to your computer and use it in GitHub Desktop.
clozemasterのための文字列を簡単にコピーするボタンをweblioに追加するChrome Tampermonkey用UserScript
// ==UserScript==
// @name copy weblio for clozemaster
// @namespace http://tampermonkey.net/
// @version 0.1
// @description clozemasterのための文字列を簡単にコピーするボタンをweblioに追加します
// @author lowteq
// @match https://ejje.weblio.jp/sentence/content/*
// @icon https://www.google.com/s2/favicons?domain=weblio.jp
// @grant none
// ==/UserScript==
(function() {
'use strict';
let word = document.querySelector(".qotHT").childNodes[0].textContent;
console.log("word = " + word);
let sentenceblocks = document.querySelectorAll(".qotC");
sentenceblocks.forEach(function(sentenceblock){
//console.log(sentenceblock);
var engsentence = sentenceblock.querySelector(".qotCE").textContent.replace(word,"{{" + word + "}}").replace("例文帳に追加","");
var jpsentence = Array.prototype.filter.call(sentenceblock.querySelector(".qotCJ").childNodes,node =>{return node.nodeName != "SPAN"}).reduce((a,b)=>{return (((typeof a) == "string")?a:a.textContent) + b.textContent}).textContent;
addCopyButton(sentenceblock,engsentence + " " + jpsentence);
});
})();
function addCopyButton(parent,copytext){
const copyButton = document.createElement('input');
copyButton.classList.add('copyforclozemaster');
copyButton.type = 'button';
copyButton.value = 'Copy sentence for clozemaster';
copyButton.addEventListener('click',()=>{
execCopy(copytext);
});
parent.prepend(copyButton);
}
function execCopy(str){
var tmp = document.createElement("div");
var pre = document.createElement('pre');
pre.style.webkitUserSelect = 'auto';
pre.style.userSelect = 'auto';
tmp.appendChild(pre).textContent = str;
var s = tmp.style;
s.position = 'fixed';
s.right = '200%';
document.body.appendChild(tmp);
document.getSelection().selectAllChildren(tmp);
var result = document.execCommand("copy");
document.body.removeChild(tmp);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment