【轉】GitHub漢化腳本(谷歌瀏覽器)


  1 // ==UserScript==
  2 // @name         GitHub 漢化插件
  3 // @description  漢化 GitHub 界面的部分菜單及內容。
  4 // @copyright    2016, 樓教主 (http://www.52cik.com/)
  5 // @icon         https://assets-cdn.github.com/pinned-octocat.svg
  6 // @version      1.6.4
  7 // @author       樓教主
  8 // @license      MIT
  9 // @homepageURL  https://github.com/52cik/github-hans
 10 // @match        http://*.github.com/*
 11 // @match        https://*.github.com/*
 12 // @require      https://52cik.github.io/github-hans/locals.js?v1.6.4
 13 // @run-at       document-end
 14 // @grant        none
 15 // ==/UserScript==
 16 
 17 (function (window, document, undefined) {
 18     'use strict';
 19 
 20     var lang = 'zh'; // 中文
 21 
 22     // 2016-04-18 github 將 jquery 以 amd 加載,不暴露到全局了。
 23     // var $ = require('github/jquery')['default'];
 24 
 25     // 要翻譯的頁面
 26     var page = getPage();
 27 
 28     transTitle(); // 頁面標題翻譯
 29     timeElement(); // 時間節點翻譯
 30     // setTimeout(contributions, 100); // 貢獻日歷翻譯 (日歷是內嵌或ajax的, 所以基於回調事件處理)
 31     walk(document.body); // 立即翻譯頁面
 32 
 33     // 2017-03-19 github 屏蔽 require 改為 Promise 形式的 ghImport
 34     define('github-hans-ajax', ['./jquery'], function($) {
 35         $(document).ajaxComplete(function () {
 36             transTitle();
 37             walk(document.body); // ajax 請求后再次翻譯頁面
 38         });
 39     });
 40     ghImport('github-hans-ajax')['catch'](function(e) {
 41         setTimeout(function() { throw e });
 42     });
 43 
 44     /**
 45      * 遍歷節點
 46      *
 47      * @param {Element} node 節點
 48      */
 49     function walk(node) {
 50         var nodes = node.childNodes;
 51 
 52         for (var i = 0, len = nodes.length; i < len; i++) {
 53             var el = nodes[i];
 54             // todo 1. 修復多屬性翻譯問題; 2. 添加事件翻譯, 如論預覽信息;
 55 
 56             if (el.nodeType === Node.ELEMENT_NODE) { // 元素節點處理
 57 
 58                 // 元素節點屬性翻譯
 59                 if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') { // 輸入框 按鈕 文本域
 60                     if (el.type === 'button' || el.type === 'submit') {
 61                         transElement(el, 'value');
 62                     } else {
 63                         transElement(el, 'placeholder');
 64                     }
 65                 } else if (el.hasAttribute('aria-label')) { // 帶提示的元素,類似 tooltip 效果的
 66                     transElement(el, 'aria-label', true);
 67 
 68                     if (el.hasAttribute('data-copied-hint')) { // 復制成功提示
 69                         transElement(el.dataset, 'copiedHint');
 70                     }
 71                 } else if (el.tagName === 'OPTGROUP') { // 翻譯 <optgroup> 的 label 屬性
 72                     transElement(el, 'label');
 73                 }
 74 
 75                 if (el.hasAttribute('data-disable-with')) { // 按鈕等待提示
 76                     transElement(el.dataset, 'disableWith');
 77                 }
 78 
 79                 // 跳過 readme, 文件列表, 代碼顯示
 80                 if (el.id !== 'readme' && !I18N.conf.reIgnore.test(el.className)) {
 81                     walk(el); // 遍歷子節點
 82                 }
 83             } else if (el.nodeType === Node.TEXT_NODE) { // 文本節點翻譯
 84                 transElement(el, 'data');
 85             }
 86 
 87         }
 88     }
 89 
 90     /**
 91      * 獲取翻譯頁面
 92      */
 93     function getPage() {
 94         // 先匹配 body 的 class
 95         var page = document.body.className.match(I18N.conf.rePageClass);
 96 
 97         if (!page) { // 擴展 url 匹配
 98             page = location.href.match(I18N.conf.rePageUrl);
 99         }
100 
101         if (!page) { // 擴展 pathname 匹配
102             page = location.pathname.match(I18N.conf.rePagePath);
103         }
104 
105         return page ? page[1] || 'homepage' : false; // 取頁面 key
106     }
107 
108     /**
109      * 翻譯頁面標題
110      */
111     function transTitle() {
112         var title = translate(document.title, 'title');
113 
114         if (title === false) { // 無翻譯則退出
115             return false;
116         }
117 
118         document.title = title;
119     }
120 
121 
122     /**
123      * 翻譯節點對應屬性內容
124      *
125      * @param {object} el 對象
126      * @param {string} field 屬性字段
127      * @param {boolean} isAttr 是否是 attr 屬性
128      *
129      * @returns {boolean}
130      */
131     function transElement(el, field, isAttr) {
132         var transText = false; // 翻譯后的文本
133 
134         if (isAttr === undefined) { // 非屬性翻譯
135             transText = translate(el[field], page);
136         } else {
137             transText = translate(el.getAttribute(field), page);
138         }
139 
140         if (transText === false) { // 無翻譯則退出
141             return false;
142         }
143 
144         // 替換翻譯后的內容
145         if (isAttr === undefined) {
146             el[field] = transText;
147         } else {
148             el.setAttribute(field, transText);
149         }
150     }
151 
152 
153     /**
154      * 翻譯文本
155      *
156      * @param {string} text 待翻譯字符串
157      * @param {string} page 頁面字段
158      *
159      * @returns {string|boolean}
160      */
161     function translate(text, page) { // 翻譯
162         var str;
163         var _key = text.trim(); // 去除首尾空格的 key
164         var _key_neat = _key
165             .replace(/\xa0/g, ' ') // 替換 &nbsp; 空格導致的 bug
166             .replace(/\s{2,}/g, ' '); // 去除多余換行空格等字符,(試驗測試階段,有問題再恢復)
167 
168         if (_key_neat === '') {
169             return false;
170         } // 內容為空不翻譯
171 
172         str = transPage('pubilc', _key_neat); // 公共翻譯
173 
174         if (str !== false && str !== _key_neat) { // 公共翻譯完成
175             str = transPage('pubilc', str) || str;  // 二次公共翻譯(為了彌補正則部分翻譯的情況)
176             return text.replace(_key, str);  // 替換原字符,保留空白部分
177         }
178 
179         if (page === false) {
180             return false;
181         } // 未知頁面不翻譯
182 
183         str = transPage(page, _key_neat); // 翻譯已知頁面
184         if (str === false || str === '') {
185             return false;
186         } // 未知內容不翻譯
187 
188         str = transPage('pubilc', str) || str; // 二次公共翻譯(為了彌補正則部分翻譯的情況)
189         return text.replace(_key, str); // 替換原字符,保留空白部分
190     }
191 
192 
193     /**
194      * 翻譯頁面內容
195      *
196      * @param {string} page 頁面
197      * @param {string} key 待翻譯內容
198      *
199      * @returns {string|boolean}
200      */
201     function transPage(page, key) {
202         var str; // 翻譯結果
203         var res; // 正則數組
204 
205         // 靜態翻譯
206         str = I18N[lang][page]['static'][key];
207         if (str) {
208             return str;
209         }
210 
211         // 正則翻譯
212         res = I18N[lang][page].regexp;
213         if (res) {
214             for (var i = 0, len = res.length; i < len; i++) {
215                 str = key.replace(res[i][0], res[i][1]);
216                 if (str !== key) {
217                     return str;
218                 }
219             }
220         }
221 
222         return false; // 沒有翻譯條目
223     }
224 
225 
226     /**
227      * 時間節點翻譯
228      */
229     function timeElement() {
230         if (!window.RelativeTimeElement) { // 防止報錯
231             return;
232         }
233 
234         var RelativeTimeElement$getFormattedDate = RelativeTimeElement.prototype.getFormattedDate;
235         var TimeAgoElement$getFormattedDate = TimeAgoElement.prototype.getFormattedDate;
236         // var LocalTimeElement$getFormattedDate = LocalTimeElement.prototype.getFormattedDate;
237 
238         var RelativeTime = function (str, el) { // 相對時間解析
239             if (/^on ([\w ]+)$/.test(str)) {
240                 return '於 ' + el.title.replace(/ .+$/, '');
241             }
242 
243             // 使用字典公共翻譯的第二個正則翻譯相對時間
244             var time_ago = I18N[lang].pubilc.regexp[1];
245             return str.replace(time_ago[0], time_ago[1]);
246         };
247 
248         RelativeTimeElement.prototype.getFormattedDate = function () {
249             var str = RelativeTimeElement$getFormattedDate.call(this);
250             return RelativeTime(str, this);
251         };
252 
253         TimeAgoElement.prototype.getFormattedDate = function () {
254             var str = TimeAgoElement$getFormattedDate.call(this);
255             return RelativeTime(str, this);
256         };
257 
258         LocalTimeElement.prototype.getFormattedDate = function () {
259             return this.title.replace(/ .+$/, '');
260         };
261 
262         // 遍歷 time 元素進行翻譯
263         // 2016-04-16 github 改版,不再用 time 標簽了。
264         var times = document.querySelectorAll('time, relative-time, time-ago, local-time');
265         Array.prototype.forEach.call(times, function (el) {
266             if (el.getFormattedDate) { // 跳過未注冊的 time 元素
267                 el.textContent = el.getFormattedDate();
268             }
269         });
270     }
271 
272 
273     /**
274      * 貢獻日歷 基於事件翻譯
275      */
276     function contributions() {
277         var tip = document.getElementsByClassName('svg-tip-one-line');
278 
279         // 等待 IncludeFragmentElement 元素加載完畢后綁定事件
280         // var observe = require('github/observe').observe;
281 
282         define('github/hans-contributions', ['./observe'], function (observe) {
283             observe(".js-calendar-graph-svg", function () {
284                 setTimeout(function () { // 延時綁定 mouseover 事件,否則沒法翻譯
285                     var $calendar = $('.js-calendar-graph');
286                     walk($calendar[0]); // 翻譯日歷部分
287 
288                     $calendar.on('mouseover', '.day', function () {
289                         if (tip.length === 0) { // 沒有 tip 元素時退出防止報錯
290                             return true;
291                         }
292 
293                         var data = $(this).data(); // 獲取節點上的 data
294                         var $tip = $(tip[0]);
295 
296                         $tip.html(data.count + ' 次貢獻 ' + data.date);
297 
298                         var rect = this.getBoundingClientRect(); // 獲取元素位置
299                         var left = rect.left + window.pageXOffset - tip[0].offsetWidth / 2 + 5.5;
300 
301                         $tip.css('left', left);
302                     });
303                 }, 999);
304             });
305         });
306 
307         ghImport('github/hans-contributions')['catch'](function(e) {
308             setTimeout(function() { throw e });
309         });
310     }
311 
312 })(window, document);

轉載於樓教主GitHub:https://github.com/52cik/github-hans/blob/gh-pages/main.js


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM