Skip to content

Commit

Permalink
Merge pull request #13 from uetchy/async-await
Browse files Browse the repository at this point in the history
style: async-await sentence
  • Loading branch information
uetchy authored Nov 13, 2016
2 parents f685d66 + 560f9a1 commit 2780582
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Polyglot.safariextension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>Author</key>
<string>Yasuaki Uechi</string>
<key>Builder Version</key>
<string>11601.7.8</string>
<string>12602.2.14.0.7</string>
<key>CFBundleDisplayName</key>
<string>Polyglot</string>
<key>CFBundleIdentifier</key>
Expand Down
42 changes: 20 additions & 22 deletions Polyglot.safariextension/Settings.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<plist version="1.0">
<array>
<dict>
<key>DefaultValue</key>
<string>ja</string>
<key>Key</key>
<string>targetLanguage</string>
<key>Title</key>
Expand Down Expand Up @@ -226,26 +224,26 @@
<key>Type</key>
<string>CheckBox</string>
</dict>
<dict>
<key>DefaultValue</key>
<string>false</string>
<key>Key</key>
<string>useCtrlKey</string>
<key>Title</key>
<string>Ctrl</string>
<key>Type</key>
<string>CheckBox</string>
</dict>
<dict>
<key>DefaultValue</key>
<string>false</string>
<key>Key</key>
<string>useAltKey</string>
<key>Title</key>
<string>Alt</string>
<key>Type</key>
<string>CheckBox</string>
</dict>
<dict>
<key>DefaultValue</key>
<string>false</string>
<key>Key</key>
<string>useCtrlKey</string>
<key>Title</key>
<string>Ctrl</string>
<key>Type</key>
<string>CheckBox</string>
</dict>
<dict>
<key>DefaultValue</key>
<string>false</string>
<key>Key</key>
<string>useAltKey</string>
<key>Title</key>
<string>Alt</string>
<key>Type</key>
<string>CheckBox</string>
</dict>
<dict>
<key>DefaultValue</key>
<string>t</string>
Expand Down
54 changes: 24 additions & 30 deletions Polyglot.safariextension/global.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import url from 'url';
import 'whatwg-fetch';
import 'whatwg-fetch'; // eslint-disable-line import/no-unassigned-import

// Get settings
let settings = {};
Expand All @@ -14,34 +14,27 @@ safari.extension.settings.addEventListener('change', settingsChanged, false);

// Perform commands from users
function performCommand(event) {
switch (event.command) {
case 'translateSelectedText':
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('getSelectedText');
break;
default:

const {command} = event;
if (command === 'translateSelectedText') {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('getSelectedText');
}
}

// Handle message from injected script
function handleMessage(msg) {
switch (msg.name) {
case 'finishedGetSelectedText':
handleFinishedGetSelectedText(msg);
break;
case 'getSettings':
handleGetSettings(msg);
break;
default:
const {name} = msg;
if (name === 'finishedGetSelectedText') {
handleFinishedGetSelectedText(msg);
} else if (name === 'getSettings') {
handleGetSettings(msg);
}
}

function handleFinishedGetSelectedText(msg) {
console.log(msg);
async function handleFinishedGetSelectedText(msg) {
if (msg.message === '') {
return;
}
var target = msg.target;
const target = msg.target;
target.page.dispatchMessage('showPanel', '<div class="polyglot__loader">Loading</div>');

if (settings.targetLanguage === '') {
Expand All @@ -58,18 +51,18 @@ function handleFinishedGetSelectedText(msg) {
}});
const api = 'http://translate.googleapis.com/translate_a/single' + query;

fetch(api)
.then(response => {
return response.text();
})
.then(body => {
const data = JSON.parse(body.replace(/,,/g, ',null,').replace(/,,/g, ',null,'));
const translatedText = data[0][0][0];
target.page.dispatchMessage('updatePanel', translatedText);
})
.catch(err => {
target.page.dispatchMessage('updatePanel', err);
});
try {
const response = await fetch(api);
const body = await response.text();
const data = JSON.parse(body.replace(/,,/g, ',null,').replace(/,,/g, ',null,'));
console.log(data[0]);
const translatedText = data[0]
.map(sentence => sentence[0])
.join('<br/>');
target.page.dispatchMessage('updatePanel', translatedText);
} catch (err) {
target.page.dispatchMessage('updatePanel', err);
}
}

function handleGetSettings(msg) {
Expand All @@ -79,4 +72,5 @@ function handleGetSettings(msg) {
// Update setting values immediately
function settingsChanged(event) {
settings[event.key] = event.newValue;
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('settingsReceived', settings);
}
30 changes: 12 additions & 18 deletions Polyglot.safariextension/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@ const PANEL_ID = 'polyglot__panel';

// Only initialize in a top-level page
if (window.top === window) {
safari.self.addEventListener('message', handleMessage, false);
window.addEventListener('keypress', handleKeypress, false);
window.addEventListener('mouseup', handleMouseUp, false);

safari.self.addEventListener('message', handleMessage, false);
safari.self.tab.dispatchMessage('getSettings');
}

// Get selected text and return to global script
function handleMessage(msg) {
switch (msg.name) {
case 'settingsReceived':
settings = msg.message;
break;
case 'getSelectedText':
getSelectedText();
break;
case 'showPanel':
showPanel(msg.message);
break;
case 'updatePanel':
updatePanel(msg.message);
break;
default:

const name = msg.name;
if (name === 'settingsReceived') {
settings = msg.message;
} else if (name === 'getSelectedText') {
getSelectedText();
} else if (name === 'showPanel') {
showPanel(msg.message);
} else if (name === 'updatePanel') {
updatePanel(msg.message);
}
}

Expand All @@ -52,8 +46,8 @@ function handleKeypress(e) {
}

function getSelectedText() {
const sel = window.getSelection().toString();
safari.self.tab.dispatchMessage('finishedGetSelectedText', sel);
const selectedText = window.getSelection().toString();
safari.self.tab.dispatchMessage('finishedGetSelectedText', selectedText);
}

function removePanel() {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
"whatwg-fetch": "^1.0.0"
},
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.5",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.14.0",
"standard-version": "^2.4.0",
"babel-preset-stage-3": "^6.11.0",
"standard-version": "^3.0.0",
"webpack": "^1.13.2",
"xo": "^0.16.0"
"xo": "^0.17.0"
},
"xo": {
"envs": [
Expand Down
6 changes: 3 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const extensionPath = resolve(__dirname, 'Polyglot.safariextension');
module.exports = {
context: extensionPath,
entry: {
global: './global.js',
injected: './injected.js'
global: ['babel-polyfill', './global.js'],
injected: ['babel-polyfill', './injected.js']
},
output: {
path: extensionPath,
Expand All @@ -18,7 +18,7 @@ module.exports = {
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['es2015']
presets: ['es2015', 'stage-3']
}
}]
}
Expand Down

0 comments on commit 2780582

Please sign in to comment.