From 32eeaa46e90116fc5f009fe5e527a0fdf590aae6 Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Fri, 9 Sep 2016 12:15:37 +0900 Subject: [PATCH 1/8] style: async-await sentence --- Polyglot.safariextension/global.js | 46 +++++++++++----------------- Polyglot.safariextension/injected.js | 27 ++++++---------- package.json | 1 + webpack.config.js | 2 +- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/Polyglot.safariextension/global.js b/Polyglot.safariextension/global.js index 8c6c7f9..6d18930 100644 --- a/Polyglot.safariextension/global.js +++ b/Polyglot.safariextension/global.js @@ -14,30 +14,23 @@ 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; } @@ -58,18 +51,15 @@ 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,')); + const translatedText = data[0][0][0]; + target.page.dispatchMessage('updatePanel', translatedText); + } catch (err) { + target.page.dispatchMessage('updatePanel', err); + } } function handleGetSettings(msg) { diff --git a/Polyglot.safariextension/injected.js b/Polyglot.safariextension/injected.js index 0ecc357..675d59c 100644 --- a/Polyglot.safariextension/injected.js +++ b/Polyglot.safariextension/injected.js @@ -4,30 +4,23 @@ 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); } } diff --git a/package.json b/package.json index 6219aed..607799d 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "devDependencies": { "babel-loader": "^6.2.5", "babel-preset-es2015": "^6.14.0", + "babel-preset-stage-3": "^6.11.0", "standard-version": "^2.4.0", "webpack": "^1.13.2", "xo": "^0.16.0" diff --git a/webpack.config.js b/webpack.config.js index e3a72bc..f33cded 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -18,7 +18,7 @@ module.exports = { exclude: /(node_modules|bower_components)/, loader: 'babel', query: { - presets: ['es2015'] + presets: ['es2015', 'stage-3'] } }] } From 3a7288a0396a4999fab62ccb8998e670dc9dfff9 Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:02:30 +0900 Subject: [PATCH 2/8] fix(babel): add babel-polyfill to avoid error --- package.json | 2 ++ webpack.config.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 607799d..342dba0 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ "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", "babel-preset-stage-3": "^6.11.0", "standard-version": "^2.4.0", diff --git a/webpack.config.js b/webpack.config.js index f33cded..c41dd42 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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, From 6f7c2b4fa662ad2af4f30b15b1f5afd60cad984b Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:03:38 +0900 Subject: [PATCH 3/8] fix(settings): remove default language --- Polyglot.safariextension/Settings.plist | 42 ++++++++++++------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/Polyglot.safariextension/Settings.plist b/Polyglot.safariextension/Settings.plist index 5f6a459..7e1605c 100644 --- a/Polyglot.safariextension/Settings.plist +++ b/Polyglot.safariextension/Settings.plist @@ -3,8 +3,6 @@ - DefaultValue - ja Key targetLanguage Title @@ -226,26 +224,26 @@ Type CheckBox - - DefaultValue - false - Key - useCtrlKey - Title - Ctrl - Type - CheckBox - - - DefaultValue - false - Key - useAltKey - Title - Alt - Type - CheckBox - + + DefaultValue + false + Key + useCtrlKey + Title + Ctrl + Type + CheckBox + + + DefaultValue + false + Key + useAltKey + Title + Alt + Type + CheckBox + DefaultValue t From bd0ca88456a2a2cc7cbc80c639b95988953c351f Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:03:59 +0900 Subject: [PATCH 4/8] chore(deps): update deps --- Polyglot.safariextension/Info.plist | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Polyglot.safariextension/Info.plist b/Polyglot.safariextension/Info.plist index 861122e..f2e2c28 100644 --- a/Polyglot.safariextension/Info.plist +++ b/Polyglot.safariextension/Info.plist @@ -5,7 +5,7 @@ Author Yasuaki Uechi Builder Version - 11601.7.8 + 12602.2.14.0.7 CFBundleDisplayName Polyglot CFBundleIdentifier diff --git a/package.json b/package.json index 342dba0..e63f67a 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,9 @@ "babel-polyfill": "^6.16.0", "babel-preset-es2015": "^6.14.0", "babel-preset-stage-3": "^6.11.0", - "standard-version": "^2.4.0", + "standard-version": "^3.0.0", "webpack": "^1.13.2", - "xo": "^0.16.0" + "xo": "^0.17.0" }, "xo": { "envs": [ From f4971c8fe97985eb4d23fc9ab0db715ba7f3f997 Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:04:35 +0900 Subject: [PATCH 5/8] style: fix code style --- Polyglot.safariextension/global.js | 2 +- Polyglot.safariextension/injected.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Polyglot.safariextension/global.js b/Polyglot.safariextension/global.js index 6d18930..462a165 100644 --- a/Polyglot.safariextension/global.js +++ b/Polyglot.safariextension/global.js @@ -34,7 +34,7 @@ async function handleFinishedGetSelectedText(msg) { if (msg.message === '') { return; } - var target = msg.target; + const target = msg.target; target.page.dispatchMessage('showPanel', '
Loading
'); if (settings.targetLanguage === '') { diff --git a/Polyglot.safariextension/injected.js b/Polyglot.safariextension/injected.js index 675d59c..2d3076c 100644 --- a/Polyglot.safariextension/injected.js +++ b/Polyglot.safariextension/injected.js @@ -6,6 +6,7 @@ const PANEL_ID = 'polyglot__panel'; if (window.top === window) { window.addEventListener('keypress', handleKeypress, false); window.addEventListener('mouseup', handleMouseUp, false); + safari.self.addEventListener('message', handleMessage, false); safari.self.tab.dispatchMessage('getSettings'); } @@ -45,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() { From ffb93bcedc5d9d506e5a08343c6fa1169fab39a2 Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:06:23 +0900 Subject: [PATCH 6/8] fix(translate): translate multiple sentences --- Polyglot.safariextension/global.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Polyglot.safariextension/global.js b/Polyglot.safariextension/global.js index 462a165..11314e9 100644 --- a/Polyglot.safariextension/global.js +++ b/Polyglot.safariextension/global.js @@ -55,7 +55,10 @@ async function handleFinishedGetSelectedText(msg) { const response = await fetch(api); const body = await response.text(); const data = JSON.parse(body.replace(/,,/g, ',null,').replace(/,,/g, ',null,')); - const translatedText = data[0][0][0]; + console.log(data[0]); + const translatedText = data[0] + .map(sentence => sentence[0]) + .join('
'); target.page.dispatchMessage('updatePanel', translatedText); } catch (err) { target.page.dispatchMessage('updatePanel', err); From 3d7f0a7fa074f27da145bc6624846570368176dc Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:07:05 +0900 Subject: [PATCH 7/8] fix(settings): update settings after changed --- Polyglot.safariextension/global.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Polyglot.safariextension/global.js b/Polyglot.safariextension/global.js index 11314e9..311b1c3 100644 --- a/Polyglot.safariextension/global.js +++ b/Polyglot.safariextension/global.js @@ -72,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); } From 560f9a19b5208d57123d93c64a7bd273cd9d3faf Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Sun, 13 Nov 2016 23:07:22 +0900 Subject: [PATCH 8/8] chore(linter): suppress warning --- Polyglot.safariextension/global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Polyglot.safariextension/global.js b/Polyglot.safariextension/global.js index 311b1c3..bdc9100 100644 --- a/Polyglot.safariextension/global.js +++ b/Polyglot.safariextension/global.js @@ -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 = {};