Dictionary audio link revealer

Add audio file link of dictionary pages

当前为 2019-11-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Dictionary audio link revealer
// @name:zh-TW   網路字典發音下載
// @namespace    https://github.com/solomonhuang/dictionary-audio-link-revealer
// @version      0.1.0
// @description  Add audio file link of dictionary pages
// @description:zh-TW 在網路字典的發音按鈕旁邊新增聲音檔下載。
// @author       Solomon Huang
// @license      MIT
// @match        https://dictionary.cambridge.org/dictionary/*
// @match        https://tw.dictionary.search.yahoo.com/*
// @grant        none
// @supportURL   https://github.com/solomonhuang/dictionary-audio-link-revealer/issues
// ==/UserScript==

(function() {
    'use strict';

    var currentDict = location.hostname

    function createICON() {
        let icon = document.createElementNS('http://www.w3.org/2000/svg','svg')
        // SVG icon alighment style
        // Elliot Dahl ( https://twitter.com/Elliotdahl )
        // https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4
        icon.setAttribute('style','height:1em;width:1em;top: .125em;position: relative;display: inline-flex;align-self: center;')
        let c = document.createElementNS('http://www.w3.org/2000/svg','circle')
        c.setAttribute('cx', '50%')
        c.setAttribute('cy', '50%')
        c.setAttribute('r', '40%')
        c.setAttribute('stroke', 'green')
        c.setAttribute('storke-width', '10%')
        c.setAttribute('fill', 'yellow')
        icon.appendChild(c)
        return icon
    }

    function createLink(src) {
        let link = document.createElement('a')
        link.href = src
        link.style = 'text-decoration:none'
        link.appendChild(createICON())
        return link
    }

    if (/dictionary\.cambridge\.org/.test(currentDict)) {
        let daud = document.querySelectorAll('.daud')
        for (var i = 0; i < daud.length; i++) {
            let a = daud[i].getElementsByTagName('audio')
            let src = a[0].children[0].src
            let link = document.createElement('a')
            link.href = src
            link.style = 'text-decoration:none'
            link.appendChild(createICON())
            daud[i].children[1].append(link)
        }
    }

    if (/tw\.dictionary\.search\.yahoo\.com/.test(currentDict)) {
        let dict_sound = document.querySelectorAll('.dict-sound')
        // TODO: fix with MutationObserver
        // https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
        setTimeout(() => {
            for (let i = 0; i < dict_sound.length; i++) {
                let src = dict_sound[i].children[0].src
                dict_sound[i].parentElement.append(createLink(src))
            }
        },1000)
    }

})();