Bunpro: Example Sentence Audio

Adds Google Translate audio to all the sentences that do not yet have audio.

目前為 2018-05-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Bunpro: Example Sentence Audio
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds Google Translate audio to all the sentences that do not yet have audio.
// @author       Kumirei
// @include      https://bunpro.jp/*
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @grant        none
// ==/UserScript==

(function() {
		// Need to remove the referrer; otherwise returns 404s
		var remRef = document.createElement('meta');
		remRef.name = 'referrer';
		remRef.content = 'no-referrer';
		document.querySelector('head').append(remRef);

		// The player element
		var first = "<li class=\"audio-holder\">" +
			        "    <audio controls controlslist=\"nodownload\">" +
			        "        <source src=\"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=";
		var last =  "            &tl=ja&total=1&idx=0?\" type=\"audio/mpeg\">" +
			        "    </audio>" +
		            "    <div class=\"audio-button-holder\">" +
					"        <div class=\"audio-reload\" id=\"audio_for_example_sentence_1324\">Reload</div>" +
					"        <div class=\"audio-speed-75\">Slow</div>" +
			        "        <div class=\"audio-speed-100\" style=\"display: none;\">Normal</div>" +
		            "    </div>" +
			        "</li>";

		// Detect context sentences
		waitForKeyElements('.japanese-example-sentence', function(e) {
				// Do nothing if there is already audio provided
				if (e[0].nextElementSibling.className != "audio-holder") {
						// Get sentence in plain text and add the player
						var sentence = parseSentence(e[0]);
						e.after(first + sentence + last);
				}
		});

		// Extract the sentence from the element
		function parseSentence(sentenceElem) {
				var sentence = "";
				sentenceElem.childNodes.forEach(function(elem) {
						// find the text in each kind of element and append it to the sentence string
						var name = elem.nodeName;
						if (name == "#text") {
								sentence += elem.data;
						}
						else if (name == "STRONG" || name == "SPAN") {
								if (name == "STRONG" && elem.children.length) {
										sentence += elem.children[0].childNodes[0].data;       // with kanji in url
										//sentence += elem.children[0].children[1].innerText;     // with kana in url
								}
								else {
										sentence += elem.innerText;
								}
						}
						else if (name == "RUBY") {
								sentence += elem.childNodes[0].data;       // with kanji in url
								//sentence += elem.children[1].innerText;     // with kana in url
						}
				});
				return sentence;
		}
})();