Skip to content

Commit

Permalink
Merge pull request #64 from GDG-Xian/feature/bing_support
Browse files Browse the repository at this point in the history
添加必应翻译
  • Loading branch information
greatghoul authored Jun 17, 2016
2 parents b30288a + 7775a2a commit 7f5ba9d
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 更新记录

v1.6 - 2016-06-17

- 支持必应翻译

v1.5.4 - 2016-05-28

- 解决链接划词模式劫持全局快捷键导致页面链接点击异常的问题
Expand Down
14 changes: 9 additions & 5 deletions src/css/popup.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ body {
}

.btn-translator {
width: 50px;
width: 29px;
height: 16px;
margin-right: 55px;
background-size: 50px 16px;
margin-right: 69px;
background-size: 16px 16px;
background-repeat: no-repeat;
background-position: right center;
background-position: center center;

&.youdao {
background-size: 29px 16px;
background-image: url(../img/translators/youdao.png);
}

&.baidu {
background-size: 50px 12px;
background-image: url(../img/translators/baidu.png);
}

&.bing {
background-image: url(../img/translators/bing.png);
}
}

.transit-result {
Expand Down
Binary file modified src/img/translators/baidu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/img/translators/bing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/img/translators/youdao.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 3 additions & 7 deletions src/js/config/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var app = require('../lib/crxkit');
import app from '../lib/crxkit';

app.setup({
name: 'transit',
Expand All @@ -15,17 +15,13 @@ app.setup({
app.showUpdateNotes = function() {
chrome.notifications.create("update_notes", {
type: "list",
title: "TransIt V1.5.4 更新记录",
title: "TransIt V1.6 更新记录",
message: "",
iconUrl: "img/icon48.png",
items: [
{
title: '',
message: '解决链接划词模式劫持全局快捷键导致页面链接点击异常的问题'
},
{
title: '',
message: '链接划词的快捷键更新为 Ctrl+Shift+L'
message: '支持必应翻译'
}
]
}, function () {});
Expand Down
2 changes: 0 additions & 2 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*
* TransIt ContentScript
*
* jshint strict: true
*/

import $ from 'jquery';
Expand Down
4 changes: 2 additions & 2 deletions src/js/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* jshint strict: true
*/

var translators = require('./translators');
var app = require('./config/application');
import translators from './translators';
import app from './config/application';

// Key name to store current text in local storage
const CURRENT_TEXT_KEY = 'transit_current_text';
Expand Down
5 changes: 4 additions & 1 deletion src/js/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export function clearSelection() {

export function sanitizeHTML(html) {
var match = html.match(/<body[\s\S]*<\/body>/img);
return match[0].replace(/<script([\s\S]*?)<\/script>/img, '');
return match[0].replace(/<script([\s\S]*?)<\/script>/img, '')
.replace(/<style([\s\S]*?)<\/style>/img, '')
.replace(/<img([\s\S]*?)>/img, '')
.replace(/<video([\s\S]*?)>/img, '');
}

export function stopPropagation(event) {
Expand Down
9 changes: 4 additions & 5 deletions src/js/ng/controllers/options_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ angular
};

$scope.nextTranslator = function() {
if ($scope.options.translator == 'baidu') {
$scope.options.translator = 'youdao';
} else {
$scope.options.translator = 'baidu';
}
const translators = ['baidu', 'youdao', 'bing'];

let index = translators.indexOf($scope.options.translator) + 1;
$scope.options.translator = translators[index % translators.length];
};

app.initOptions(function() {
Expand Down
114 changes: 114 additions & 0 deletions src/js/translators/bing_translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Bing Translator
*/
import sugar from 'sugar';
import $ from 'jquery';
import { sanitizeHTML } from '../lib/utils';

const DICT_URL = 'http://cn.bing.com/dict/search';
const TRANSLATE_URL = 'http://cn.bing.com/translator/api/Translate/TranslateArray?from=-&to=zh-CHS';
const REFERER = 'http://cn.bing.com/dict/?mkt=zh-cn&setlang=zh';

export default class BingTranslator {
constructor() {
this.name = 'bing';
}

_parseMean(index, meanNode) {
const $mean = $(meanNode);
const def = $mean.find('.def').text();
let pos = $mean.find('.pos').text();

if (pos == '网络') {
pos = index > 0 ? '<br/><strong>网络:</strong><br/>' : '';
} else {
pos = `${pos} `;
}

return `${pos}${def}`;
}

_parseWord(page) {
var $result = $(sanitizeHTML(page));

if ($result.find('.qdef').length) {
var response = {};

var $phonetic = $result.find('.hd_prUS');
if ($phonetic.length) {
response.phonetic = $phonetic.text().replace('美 ', '');
}

var $means = $result.find('.hd_area + ul > li');
response.translation =
$means.map(this._parseMean).toArray().join('<br/>');

return response;
} else if ($result.find('.p1-11')) {
return { translation: $result.find('.p1-11').text() };
} else {
return null;
}
}

_parseText(data) {
const translation = data.items.map(item => item.text).join('<br/><br/>');

return { translation: translation };
}

_requestWord(text, callback) {
const settings = {
url: DICT_URL,
data: { q: text },
headers: {
'Accept-Language': 'zh-CN,zh;q=0.8'
}
};

$.ajax(settings)
.done(page => callback(this._parseWord(page)))
.fail(() => callback(null));
}

_buildLine(text, index) {
console.log(text, index);
const timestamp = new Date().getTime();

return {
id: timestamp + index,
text: text
};
}

_splitLines(text) {
return text.split(/\s*\n\s*/mg).map(this._buildLine);
}

_requestText(text, callback) {
const settings = {
url: TRANSLATE_URL,
type: 'POST',
data: JSON.stringify(this._splitLines(text)),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
headers: {
'Accept-Language': 'zh-CN,zh;q=0.8'
}
};

$.ajax(settings)
.done(data => callback(this._parseText(data)))
.fail(() => callback(null));
}

translate(text, callback) {
if (/^\s*$/.test(text)) {
callback(null);
} else if (/^[a-zA-Z]+$/.test(text)) {
this._requestWord(text, callback);
} else {
this._requestText(text, callback);
}
}
}
11 changes: 8 additions & 3 deletions src/js/translators/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module.exports = {
baidu: require('./baidu_translator'),
youdao: require('./youdao_translator'),
import BaiduTranslator from './baidu_translator';
import YoudaoTranslator from './youdao_translator';
import BingTranslator from './bing_translator';

export default {
baidu: BaiduTranslator,
youdao: YoudaoTranslator,
bing: new BingTranslator(),
};
8 changes: 5 additions & 3 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "TransIt",
"description": "让划词翻译更简单",
"version": "1.5.4",
"version": "1.6",

"icons": {
"48": "img/icon48.png",
Expand Down Expand Up @@ -40,8 +40,10 @@
"storage",
"notifications",
"http://fanyi.youdao.com/openapi.do",
"http://openapi.baidu.com/",
"http://trit.herokuapp.com/api/items"
"http://dict.baidu.com/s",
"http://fanyi.baidu.com/v2transapi",
"http://cn.bing.com/dict/search",
"http://cn.bing.com/translator/api/Translate/TranslateArray"
],

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
Expand Down
3 changes: 2 additions & 1 deletion src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<label class="control-label">翻译服务</label>
<div class="controls">
<label class="radio-inline"><input type="radio" name="translator" ng-model="options.translator" value="youdao" /> 有道翻译</label>
<label class="radio-inline"><input type="radio" name="translator" ng-model="options.translator" value="baidu" disabled /> 百度翻译</label>
<label class="radio-inline"><input type="radio" name="translator" ng-model="options.translator" value="baidu" /> 百度翻译</label>
<label class="radio-inline"><input type="radio" name="translator" ng-model="options.translator" value="bing" /> 必应翻译</label>
</div>
</div>

Expand Down

0 comments on commit 7f5ba9d

Please sign in to comment.