Export HJ Words

从沪江生词本导出单词

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Export HJ Words
// @namespace    http://blog.nocode.site/
// @version      0.4
// @description  从沪江生词本导出单词
// @author       Ecat
// @match        https://www.hjdict.com/scb
// @match        https://dict.hjenglish.com/scb
// @connect      www.hjdict.com
// @grant        GM_log
// ==/UserScript==

(function() {
    'use strict';
    // 获取log函数
    let log;
    if (typeof GM_log == "function"){
        log = GM_log;
    }else{
        log = console.log;
    }
    // 获取xmlHttp对象
    let xmlHttp = null;
    if (window.XMLHttpRequest)
    {// code for IE7, Firefox, Opera, etc.
        xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {// code for IE6, IE5
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // 导出回调函数
    let export_fn = function(){
        // 获取单词列表
        let words = document.querySelectorAll(".wordlist td a");
        if (words.length == 0){
            alert("没有需要导出的单词哟");
            return;
        }
        // 开始导出处理
        this.setAttribute("disabled","disabled");
        this.innerText="导出中";
        words=Array.prototype.map.call(words,(w)=>{
            xmlHttp.open("GET",w.href,false);
            xmlHttp.send(null);
            let res = xmlHttp.responseText.replace(/\n/g,"");
            // 抽出单词
            let wordinfo = res.match(/class=\"word-info\".*?<h2>(.*?)</)[1];
            this.innerText = wordinfo;
            // 抽出音标 替换掉多余空格
            wordinfo += "\t" + res.match(/class=\"pronounces\">(.*?)<\Sdiv/)[1].replace(/<.*?>/g,"").replace(/^\s+/,"").replace(/\s+/g," ");
            // 抽出简单释义 保留html标签
            wordinfo += "\t" + res.match(/class=\"simple\">(.*?)<\Sdiv>/)[1];
            return wordinfo;
        });
        // 创建下载链接
        let words_b = new Blob([words.join("\n")],{type:"text/plain",endings:"native"});
        let dw_url = URL.createObjectURL(words_b);
        let a = document.createElement("a");
        a.href = dw_url;
        a.download = "out.txt";
        a.innerText = "下载";
        a.style.display="none";
        this.parentElement.appendChild(a);
        a.click();
        // 复原按钮状态
        this.removeAttribute("disabled");
        this.innerText="导出";
        log(dw_url);
    }
    // 添加导出按钮
    let rank_fix = document.querySelector(".rank");
    let export_btn = document.createElement("button");
    export_btn.setAttribute("class","button");
    export_btn.style.right="125px";
    export_btn.innerText = "导出";
    export_btn.addEventListener("click",export_fn);
    rank_fix.parentElement.appendChild(export_btn);
    log(rank_fix);

})();