Skip to content

Commit

Permalink
add background page to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
DremyGit committed Mar 20, 2019
1 parent 43ec770 commit 5eaca26
Show file tree
Hide file tree
Showing 9 changed files with 3,270 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.idea
/node_modules
/.tmp
/tmp
/dist
dist.*
.DS_Store
6 changes: 5 additions & 1 deletion app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"default_locale": "en",
"description": "翻译来自Bing词典,可自动识别中英互译,可将翻译结果直接插入到页面内容之中",
"short_name": "__MSG_shortName__",
"version": "1.3.0",
"version": "1.4.0",
"icons": {
"128": "icon.png"
},
Expand All @@ -21,6 +21,10 @@
"js": ["chrome-dict.js"]
}
],
"background": {
"scripts": ["background.js"],
"presistent": false
},
"permissions": [
"storage"
]
Expand Down
10 changes: 10 additions & 0 deletions app/scripts/background/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
var url = `https://cn.bing.com/dict/search?mkt=zh-cn&q=${encodeURIComponent(request.word)}`
fetch(url)
.then(response => response.text())
.then(text => sendResponse(text))
.catch(error => sendResponse('未找到'))
return true; // Will respond asynchronously.
}
);
1 change: 1 addition & 0 deletions app/scripts/entry-background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default from './background';
2 changes: 1 addition & 1 deletion app/scripts/popup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'es6-promise';
import search from '../search';
import { saveKey2Storage, findKeyFromStorage, key2KeyName, keyName2Key} from '../common/key';

const version = '1.3.0';
const version = '1.4.0';

var input = document.getElementById('text');

Expand Down
4 changes: 3 additions & 1 deletion app/scripts/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import "whatwg-fetch"

export function doSearch(word) {
const url = `https://cn.bing.com/dict/search?mkt=zh-cn&q=${encodeURIComponent(word)}`;
return fetch(url).then(res => res.text())
return new Promise((resolve) => {
chrome.runtime.sendMessage({ word }, resolve)
});
}

export default function (text) {
Expand Down
5 changes: 3 additions & 2 deletions webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
const paths = {

app: './app',
output: './.tmp'
output: './tmp'
};
module.exports = {
devtool: 'cheap-module-source-map',
//devtool: 'cheap-module-eval-source-map',
entry: {
index: paths.app + '/scripts/entry-index.js',
"chrome-dict": paths.app + '/scripts/entry-content.js'
"chrome-dict": paths.app + '/scripts/entry-content.js',
background: paths.app + '/scripts/entry-background.js'
},
output: {
path: paths.output,
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const paths = {
module.exports = {
entry: {
index: paths.app + '/scripts/entry-index.js',
"chrome-dict": paths.app + '/scripts/entry-content.js'
"chrome-dict": paths.app + '/scripts/entry-content.js',
background: paths.app + '/scripts/entry-background.js'
},
output: {
path: paths.output,
Expand Down
Loading

0 comments on commit 5eaca26

Please sign in to comment.