Greasy Fork is available in English.

微软翻译组件

微软翻译组件 右下角点击翻译

2017-03-28 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name 微软翻译组件
// @description 微软翻译组件  右下角点击翻译
// @include     *
// @exclude     *.jpg
// @exclude     *.png
// @exclude     *.jpeg
// @exclude     *.gif
// @exclude     *.pdf
// @exclude     *115.com/*
// @require     https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.3/js.cookie.min.js
// @grant       aogg
// @version 2.2.10
// @namespace https://greasyfork.org/users/25818
// ==/UserScript==



(function (){
  
  var width = '79';
  var height = '23';
  if (
    parent !== parent.parent || document.documentElement.clientWidth <= width * 2 || document.documentElement.clientHeight <= height * 2 ||
    (document.body.childElementCount === 1 && document.body.firstElementChild.nodeName === 'SCRIPT') // 只有一个script就不显示
  ){ 
    // 控制层次,避免无限调用,如:http://www.w3school.com.cn/html/html_entities.asp
    // 控制宽高小的不显示
    return;
  }


  // 重置cookie
  console.log('remove cookie mstto');
  Cookies.remove('mstto');

  var locals = ''; // zh-chs
  var source = null;
  var status = false; // 是否执行了main方法

  /**
  var noTranslator = ['wangpan'];
  if (self.frameElement && noTranslator.indexOf(self.frameElement.name) !== -1){
      return;
  }
  */

  var scriptNode = document.createElement('script');
  scriptNode.innerHTML = "setTimeout(function(){{var s=document.createElement('script');s.type='text/javascript';s.charset='UTF-8';s.src=((location && location.href && location.href.indexOf('https') == 0)?'https://ssl.microsofttranslator.com':'http://www.microsofttranslator.com')+'/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKkd965FeEGM5JtQ**&ctf=False&ui=false&settings=Manual&from=';var p=document.head[0]||document.documentElement;p.insertBefore(s,p.firstChild); }},0);"
  document.body.appendChild(scriptNode);

  var divNode = document.createElement('div');
  divNode.id = 'MicrosoftTranslatorWidget';
  divNode.style.color = 'white';
  divNode.style.backgroundColor = '#555555';
  divNode.style.position = 'fixed';
  divNode.style.right = '0';
  divNode.style.bottom = '1px';
  divNode.style.zIndex = '9999999';
  divNode.title = '翻译为,或双击隐藏';
  divNode.style.width= width + 'px';
  divNode.style.height= height + 'px';
  // divNode.style.display= 'none';
  // divNode.setAttribute('style', 'color:white;background-color:#555555;position: fixed;right: 0;bottom: 0;');
  document.body.appendChild(divNode);


  document.onreadystatechange = main;
  setTimeout(main, 1000); // 最少1s内显示



  function main(){
    if (status || document.readyState !== 'complete'){
      return;
    }
    status = true;

    var selectHtml = document.createElement('select');
    selectHtml.style.backgroundColor = 'rgb(178, 178, 178)';
    selectHtml.ondblclick = function(){ // 双击隐藏
      this.parentNode.style.display = 'none';
    };
    selectHtml.style.margin = 0;
    selectHtml.style.padding = 0;
    selectHtml.style.fontSize = '13.3px';
    selectHtml.style.width= width + 'px';
    selectHtml.style.height= height + 'px';
    selectHtml.onchange = function(){
      source = this.options[this.selectedIndex].getAttribute('data-source') || null;
      locals = this.value;
      translateStart();
    };
    // Microsoft.Translator.Widget.GetLanguagesForTranslateLocalized()获取所有支持的翻译选项
    selectHtml.innerHTML = "\
      <option id='MicrosoftTranslatorWidget-option-select' value=''>翻译为</option>\
      <option value='zh-chs'>简体中文</option>\
      <option value='zh-cht'>繁体中文</option>\
      <option value='yue'>粤语</option>\
      <option value='ja'>日文</option>\
      <option id='MicrosoftTranslatorWidget-option-en' value='en'>英文</option>\
  ";
    var parentDiv = document.body.children.namedItem('MicrosoftTranslatorWidget');
    // parentDiv.style.display = 'block';
    parentDiv.appendChild(selectHtml);

    translateStart();
  }


  function translateStart(){
    if (status && locals && Microsoft){
      // null, 'es', onProgress, onError, onComplete, onRestoreOriginal, 2000
      Microsoft.Translator.Widget.Translate(source, locals, null, null, onComplete);
    }
  }

  function onComplete(){
    var option = document.body.children.namedItem('MicrosoftTranslatorWidget').children.item(0).children;
    option.namedItem('MicrosoftTranslatorWidget-option-en').innerText = '英文';
    option.namedItem('MicrosoftTranslatorWidget-option-select').innerText = '翻译为';

  }


  function addGlobalStyle(css) {
      var head, style;
      head = document.getElementsByTagName('head')[0];
      if (!head) { return; }
      style = document.createElement('style');
      style.type = 'text/css';
      style.innerHTML = css;
      head.appendChild(style);
  }


  console.log('完成');
  
  
})()