Skip to content

Instantly share code, notes, and snippets.

@lideming
Last active October 23, 2022 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lideming/34d9997b0712894adb25ca824c21b518 to your computer and use it in GitHub Desktop.
Save lideming/34d9997b0712894adb25ca824c21b518 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name LyricsCopy
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Copy lyrics from lyrics website
// @author Li Deming
// @match https://utaten.com/lyric/*
// @match https://zh.moegirl.org.cn/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function getUtaten() {
var lyricsBox = document.querySelector('.hiragana');
var r = '';
var lyricWork = document.querySelector('.lyricWork');
if (lyricWork) {
for (var child = lyricWork.firstElementChild; child; child = child.nextElementSibling) {
r += child.textContent + ': ';
child = child.nextElementSibling;
r += child.textContent + '\n';
}
r += '\n';
}
var line = '';
for (var x = lyricsBox.firstChild; x != null; x = x.nextSibling) {
if (x.nodeType == Node.TEXT_NODE) {
line += x.textContent;
} else if (x.nodeType == Node.ELEMENT_NODE) {
/** @type {HTMLElement} */
const element = x;
if (element.tagName == 'SPAN' && element.className == 'ruby') {
line += '[' + element.childNodes[0].textContent + ']{' + element.childNodes[1].textContent + '}';
} else if (element.tagName == 'BR') {
r += line.replace(/(^\s+)|(\s+$)/g, '') + '\n';
line = '';
}
}
}
return r;
}
function getMoeGirl() {
document.querySelectorAll('.Lyrics').forEach(lyrics => {
let lang = lyrics.querySelector('.Lyrics-original [lang]')?.lang;
let transLang = lyrics.querySelector('.Lyrics-translated [lang]')?.lang;;
let r = '';
if (lang) {
r += '[lang:'
r += lang;
if (transLang) r += '/' + transLang;
r += ']\n\n';
}
function visit(node) {
if (node.className == 'template-ruby-hidden') return;
if (node.className == 'Lyrics-translated') {
if (node.textContent) {
r += '\n/' + node.textContent;
}
} else if (node.tagName == 'RB') {
r += '[';
node.childNodes.forEach(visit);
r += ']';
} else if (node.tagName == 'RT') {
r += '{';
node.childNodes.forEach(visit);
r += '}';
} else if (node instanceof Text) {
r += node.textContent;
} else {
node.childNodes.forEach(visit);
}
}
lyrics.querySelectorAll('.Lyrics-line').forEach(line => {
visit(line);
r += '\n';
})
console.info(r);
})
}
window.setTimeout(() => {
if (window.location.origin == 'https://utaten.com') {
console.log(getUtaten());
} else if (window.location.origin == 'https://zh.moegirl.org.cn') {
getMoeGirl();
}
}, 2000);
window.lc = {
getUtaten,
getMoeGirl,
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment