// ==UserScript== // @namespace ATGT // @name bing-dict // @name:zh-CN 必应词典 // @description Select any word in any web to show it's definition from Bing Dict. // @description:zh-CN 划词翻译,使用必应词典 // @version 1.2 // @match http://*/* // @match https://*/* // -match https://github.com/* // @grant GM.xmlHttpRequest // @run-at document-end // @downloadURL none // ==/UserScript== /* Change Log: v1.2: 14 Jan 2018, Escape search word and result. v1.1: 13 Jan 2018, Reset style for result div. v1.0: 12 Jan 2018, Initial version. */ //alert("hello"); console.log("!!!!!!!!!!!!!!!!!!!!!bing-dict!!!!!!!!!!!!!!!!!!!!!!!!"); (function () { (function addStyleSheet() { var style = document.createElement("STYLE"); style.type = "text/css"; var css = ` div#ATGT-bing-dict-result-wrapper-reset { all: initial; * { all: initial; } } div#ATGT-bing-dict-result-wrapper { display: block; position: fixed; left: 2px; bottom: 2px; max-width: 30%; z-index: 2100000000; padding: 0; margin: 0; color: black; background-color: rgba(255,255,255,0.9); font-size: small; font-family: sans-serif; } div#ATGT-bing-dict-result-wrapper .headword { font-weight: bold; font-size: medium; } div#ATGT-bing-dict-result-wrapper a:link { color: #37a; text-decoration: none; } div#ATGT-bing-dict-result-wrapper a:hover { color: white; background-color: #37a; } div#ATGT-bing-dict-result-wrapper .headsentence { } div#ATGT-bing-dict-result-wrapper ul { list-style-type: none; padding: 1px; margin: 0px; } div#ATGT-bing-dict-result-wrapper ul li{ margin-top: 1px; } div#ATGT-bing-dict-result-wrapper ul li span { float:left; color: white; background-color: gray; text-align: center; padding: 0 2px; margin-right: 3px; } `; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); })(); var dictResultDiv = (function createDictResultDiv() { var div_wrapper_reset = document.createElement("DIV"); div_wrapper_reset.id = "ATGT-bing-dict-result-wrapper-reset"; var div = document.createElement("DIV"); div.id = "ATGT-bing-dict-result-wrapper"; div_wrapper_reset.appendChild(div); /* div.style.position = "fixed"; div.style.left = "5px"; div.style.bottom = "5px"; div.style.textAlign = "left"; div.style.fontSize = "small"; div.style.maxWidth = "30%"; div.innerHTML = ""; div.style.backgroundColor = "rgba(255,255,255,0.95)"; div.style.color = "rgb(0,0,0)"; */ document.body.appendChild(div_wrapper_reset); return div; })(); var dictCache = {}; var lastSearchWord = ""; var entityMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/', '`': '`', '=': '=' }; function escapeHtml (string) { return String(string).replace(/[&<>"'`=\/]/g, function (s) { return entityMap[s]; }); } function parseDictResultDefinition(qdef, url) { //console.log("parseDictResultDefinition"); //console.log("qdef ", qdef); var hd_area = qdef.childNodes[0]; try { var headword = escapeHtml(hd_area.querySelector("#headword").innerText); } catch (e) { var headword = ""; } var hd_pr = ""; try { /* en to cn */ hd_pr = escapeHtml(hd_area.querySelector(".hd_prUS").innerText) + " "+escapeHtml(hd_area.querySelector(".hd_pr").innerText); } catch (e) { } try { if (!hd_pr) /* cn to en */ hd_pr = escapeHtml(hd_area.querySelector(".hd_tf_lh").innerText); } catch (e) { } headword = "