Greasy Fork is available in English.

不要翻译github上的代码

避免google网页翻译github和npm```<pre>```标签内的内容

Version au 23/01/2019. Voir la dernière version.

// ==UserScript==
// @name         不要翻译github上的代码
// @namespace    http://floatsyi.com/
// @version      0.21
// @description  避免google网页翻译github和npm```<pre>```标签内的内容
// @author       floatsyi
// @include      *://github.com*
// @include      *://www.npmjs.com*
// @match        *://github.com*
// @match      *://www.npmjs.com*
// @grant        MIT
// ==/UserScript==

(function() {
    'use strict';
     var addCodeEle = function (ele) {ele.innerHTML = '<code>' + ele.innerHTML + '</code>'}
     var hasCodeEleChild = function(ele) {return !!ele.querySelector('code')}
     var href = window.location.href
     var isChange = function(){
         var yesOrNo = !(href === window.location.href)
         href = window.location.href
         return yesOrNo
     }
     var isLoaded = function(){return (!document.querySelector('.is-loading') && (document.querySelectorAll('.file').length > 0 || document.querySelectorAll('pre').length > 0))}
     var time = 0
     var doNotTranslateCode = function() {
         var files = document.querySelectorAll('.file')
         var pres = document.querySelectorAll('pre')
         if (files.length > 0) {
             if (window.location.href.search(/.md/i) !== -1) {
               if (pres.length > 0) {
                  pres.forEach(function(pre){if (!hasCodeEleChild(pre)) addCodeEle(pre)})
               }
             } else {
               files.forEach(function(file){addCodeEle(file)})
             }
         } else if (pres.length > 0) {
             pres.forEach(function(pre){if (!hasCodeEleChild(pre)) addCodeEle(pre)})
         } else {
           console.log('不要翻译github上的代码: 执行条件不足')
         }
         time = 0
     }
     doNotTranslateCode()
     document.addEventListener('click', function(e){
         clearInterval(id)
         if (isChange() && window.location.href.search(/npmjs.com/i) === -1) {
          var id = setInterval(function(){
              // console.log(time)
              if (isLoaded() || time >= 200) { // 10 * 1000ms / 50ms = 200
                  if (!(time >= 200)) {
                    console.log('不要翻译github上的代码: done')
                    doNotTranslateCode()
                 } else {
                    console.log('不要翻译github上的代码: 加载超时,如有异常请刷新浏览器')
                    time = 0
                 }
                 clearInterval(id)
              } else {
                 time++
              }
          },50)
       }
     })
     window.addEventListener("popstate", doNotTranslateCode)
})();