diff --git a/simple-translate/Settings.js b/simple-translate/Settings.js new file mode 100644 index 0000000..9d751f9 --- /dev/null +++ b/simple-translate/Settings.js @@ -0,0 +1,274 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//初回起動時にオプションページを表示して設定を初期化 +//Display option page at initial startup and initialize settings +/* +browser.runtime.onInstalled.addListener(function(){ + browser.runtime.openOptionsPage(); +}); +*/ +function settingsObj() {}; +(function () { + + //オプションページを書き換え,設定の初期化 + //Rewrite option page, initialize setting + settingsObj.prototype.initOptionsPage = function () { + return new Promise(function (resolve, reject) { + labelSet(); + getSettingsByHtml(); + overRideSettingsByStorage().then(function () { + overRideHtml(); + saveSettings(); + resolve(); + }); + }) + }; + //オプションページから設定を保存 + //Save settings from options page + settingsObj.prototype.saveOptionsPage = function () { + return new Promise(function (resolve, reject) { + getSettingsByHtml(); + saveSettings().then(function () { + resolve(); + }); + }) + }; + //設定を初期化 + //Initialize setting + settingsObj.prototype.init = function () { + return new Promise(function (resolve, reject) { + getSettings().then(function () { + resolve(Settings); + }) + }) + } + //設定を返す + //return settings + settingsObj.prototype.get = function () { + return Settings; + }; + //受け取ったオブジェクトを保存 + //Save the received object + settingsObj.prototype.save = function (settings) { + return new Promise(function (resolve, reject) { + for (let i in settings) { + Settings[i] = settings[i]; + } + saveSettings().then(function () { + resolve(); + }); + }) + }; + //設定を削除 + //Delete settings + settingsObj.prototype.clear = function (setting) { + return new Promise(function (resolve, reject) { + delete Settings[setting]; + saveSettings().then(function () { + resolve(); + }) + }) + } + //全ての設定を削除 + //Delete all settings + settingsObj.prototype.clearAll = function () { + return new Promise(function (resolve, reject) { + Settings = new settingsObj(); + saveSettings().then(function () { + resolve(); + }) + }) + } + + //let Settings = new settingsObj(); + let Settings={}; + //S = new settingsObj(); //外部から呼び出し Call from outside + + //spanやoptionのid,buttonのclassに"Label"が含まれるときi18nから値を取得して書き換え + //When "label" is included in span and option id, button class Retrieve the value from i18n and rewrite it + function labelSet() { + + //span idにLableが含まれていたら + let spans = document.getElementsByTagName("span"); + for (let i in spans) { + if (spans[i].id == undefined || spans[i].id.indexOf("Label") == -1) continue; + let label = browser.i18n.getMessage(spans[i].id); + if (label == "") continue; + spans[i].innerHTML = label; + } + + //button, submit, text classにLabelが含まれていたら + let inputs = document.getElementsByTagName("input"); + for (let i in inputs) { + if (inputs[i].id == undefined || inputs[i].className.indexOf("Label") == -1) continue; + let label=browser.i18n.getMessage(inputs[i].className); + if(label=="")continue; + + switch (inputs[i].type) { + case "button": + case "submit": + inputs[i].value = label; + break; + case "text": + inputs[i].placeholder=label; + } + } + + //options idにLabelが含まれていたら + let options=document.getElementsByTagName("option"); + for(let i in options){ + if (options[i].id == undefined || options[i].id.indexOf("Label") == -1) continue; + let label=browser.i18n.getMessage(options[i].id); + if(label=="")continue; + + options[i].innerHTML=label; + } + } + + //storageからSettingsの項目を取得して存在しない物をSettingsに上書き + //Retrieve the Settings item from storage and overwrite Settings that do not exist + function overRideSettingsByStorage() { + return new Promise(function (resolve, reject) { + browser.storage.local.get("Settings", function (value) { + + for (let i in Settings) { + if (value.Settings!=undefined && value.Settings[i] != undefined) { + Settings[i] = value.Settings[i]; + } + } + for (let i in value.Settings) { + if (Settings[i] == undefined) Settings[i] = value.Settings[i]; + } + resolve(); + }) + }) + } + + //オプションページにSettingsを反映 + //Reflect Settings on option page + function overRideHtml() { + let inputs = document.getElementsByTagName("input"); + for (let i in inputs) { + if(inputs[i].id==undefined) continue; + if (inputs[i].className != undefined && inputs[i].className.indexOf("noSetting") != -1) continue; + + switch (inputs[i].type) { + case "text": + case "number": + case "search": + case "tel": + case "url": + case "email": + case "password": + case "datetime": + case "month": + case "week": + case "time": + case "datetime-local": + case "range": + case "color": + inputs[i].value = Settings[inputs[i].id]; + break; + case "checkbox": + inputs[i].checked = Settings[inputs[i].id]; + break; + case "radio": + if (Settings[inputs[i].name] == inputs[i].value) { + inputs[i].checked = true; + } + break; + } + } + let textareas=document.getElementsByTagName("textarea"); + for(let i in textareas){ + if(textareas[i].id==undefined) continue; + if (textareas[i].className != undefined && textareas[i].className.indexOf("noSetting") != -1) continue; + textareas[i].value=Settings[textareas[i].id]; + } + + let selects=document.getElementsByTagName("select"); + for(let i in selects){ + if(selects[i].id==undefined) continue; + if (selects[i].className != undefined && inputs[i].className.indexOf("noSetting") != -1) continue; + + selects[i].value=Settings[selects[i].id]; + } + } + + //オプションページから設定の値を取得 + //Get setting value from option page + function getSettingsByHtml() { + let inputs = document.getElementsByTagName("input"); + + for (let i in inputs) { + if(inputs[i].id==undefined) continue; + if (inputs[i].className != undefined && inputs[i].className.indexOf("noSetting") != -1) continue; + + switch (inputs[i].type) { + case "text": + case "number": + case "search": + case "tel": + case "url": + case "email": + case "password": + case "datetime": + case "month": + case "week": + case "time": + case "datetime-local": + case "range": + case "color": + Settings[inputs[i].id] = inputs[i].value; + break; + case "checkbox": + Settings[inputs[i].id] = inputs[i].checked; + break; + case "radio": + if (inputs[i].checked == true) { + Settings[inputs[i].name] = inputs[i].value; + } + break; + } + } + + let textareas=document.getElementsByTagName("textarea"); + for(let i in textareas){ + if(textareas[i].id==undefined) continue; + if (textareas[i].className != undefined && textareas[i].className.indexOf("noSetting") != -1) continue; + Settings[textareas[i].id]=textareas[i].value; + } + + let selects=document.getElementsByTagName("select"); + for(let i in selects){ + if(selects[i].id==undefined) continue; + if (selects[i].className != undefined && selects[i].className.indexOf("noSetting") != -1) continue; + + Settings[selects[i].id]=selects[i].value; + } + } + + //ストレージが変更されたらget + browser.storage.onChanged.addListener(getSettings); + function getSettings() { + return new Promise(function (resolve, reject) { + browser.storage.local.get("Settings", function (value) { + Settings = value.Settings; + resolve(Settings); + }); + }) + } + + function saveSettings() { + return new Promise(function (resolve, reject) { + browser.storage.local.set({ + 'Settings': Settings + }).then(function () { + resolve(Settings); + }); + }) + } + +}()); \ No newline at end of file diff --git a/simple-translate/_locales/en/messages.json b/simple-translate/_locales/en/messages.json index bc5afbc..80ce3e5 100644 --- a/simple-translate/_locales/en/messages.json +++ b/simple-translate/_locales/en/messages.json @@ -11,7 +11,7 @@ "showLink":{ "message": "Translate this page" }, - "LangListLabel":{ + "targetLangLabel":{ "message":"Target language" }, "langList":{ @@ -40,5 +40,26 @@ }, "translateLinkMenu":{ "message": "Translate selected link" + }, + "panelSizeLabel":{ + "message": "Size of translation panel" + }, + "buttonSizeLabel":{ + "message": "Size of button" + }, + "buttonPositionLabel":{ + "message": "Display position of button" + }, + "rightUpLabel":{ + "message": "Upper right" + }, + "rightDownLabel":{ + "message": "Bottom right" + }, + "fontSizeLabel":{ + "message": "Font size" + }, + "saveLabel":{ + "message": "Save" } } \ No newline at end of file diff --git a/simple-translate/_locales/ja/messages.json b/simple-translate/_locales/ja/messages.json index e9c139b..8277a35 100644 --- a/simple-translate/_locales/ja/messages.json +++ b/simple-translate/_locales/ja/messages.json @@ -11,7 +11,7 @@ "showLink":{ "message": "このページを翻訳" }, - "LangListLabel":{ + "targetLangLabel":{ "message":"翻訳先の言語" }, "langList":{ @@ -40,5 +40,26 @@ }, "translateLinkMenu":{ "message": "選択したリンクを翻訳" + }, + "panelSizeLabel":{ + "message": "翻訳パネルのサイズ" + }, + "buttonSizeLabel":{ + "message": "ボタンのサイズ" + }, + "buttonPositionLabel":{ + "message": "ボタンの表示位置" + }, + "rightUpLabel":{ + "message": "右上" + }, + "rightDownLabel":{ + "message": "右下" + }, + "fontSizeLabel":{ + "message": "フォントサイズ" + }, + "saveLabel":{ + "message": "保存" } } \ No newline at end of file diff --git a/simple-translate/background.js b/simple-translate/background.js index 5b95785..ce3b4f8 100644 --- a/simple-translate/background.js +++ b/simple-translate/background.js @@ -1,20 +1,26 @@ -getSetting(); -browser.storage.onChanged.addListener(getSetting) +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -//設定の読み出し -function getSetting() { - browser.storage.local.get(["targetLang", "ifShowButton", "ifCheckLang", "ifShowMenu"], function (value) { - if (value.targetLang == undefined) initialSetting(); //初回起動時 - targetLang = value.targetLang; - ifShowButton = value.ifShowButton; - ifCheckLang = value.ifCheckLang; - ifShowMenu = value.ifShowMenu; - if (ifShowMenu) menuCreate(); - else menuRemove(); - }); +//初回起動時にオプションページを表示して設定を初期化 +browser.runtime.onInstalled.addListener(function(){ + browser.runtime.openOptionsPage(); +}); + +let S = new settingsObj() +browser.storage.onChanged.addListener(showMenu); + +S.init().then(function(){ + showMenu(); +}); +function showMenu(){ + if(S.get().ifShowMenu){ + menuRemove(); + menuCreate(); + } + else menuRemove(); } -//設定の初期化 function initialSetting() { switch (browser.i18n.getUILanguage()) { //一部の言語はブラウザの設定に合わせる case "ja": @@ -22,19 +28,13 @@ function initialSetting() { case "zh-TW": case "ko": targetLang = browser.i18n.getUILanguage(); + secondTargetLang="en"; break; default: targetLang = "en"; + secondTargetLang="ja"; break; - } - browser.storage.local.set({ - 'targetLang': targetLang, - 'ifShowButton': true, - 'ifCheckLang': true, - 'ifShowMenu': true - }, function () { - getSetting(); - }); + } } //メニューを表示 @@ -91,7 +91,7 @@ function translateTextMenu(info, tab) { //ページ全体を翻訳 function translatePageMenu(info, tab) { browser.tabs.create({ - 'url': "https://translate.google.co.jp/translate?hl=" + targetLang + "&sl=auto&u=" + encodeURIComponent(info.pageUrl), + 'url': "https://translate.google.co.jp/translate?hl=" + S.get().targetLang + "&sl=auto&u=" + encodeURIComponent(info.pageUrl), 'active': true, 'index': tab.index + 1 }); @@ -100,7 +100,7 @@ function translatePageMenu(info, tab) { //リンクを翻訳 function translateLinkMenu(info, tab) { browser.tabs.create({ - 'url': "https://translate.google.co.jp/translate?hl=" + targetLang + "&sl=auto&u=" + encodeURIComponent(info.linkUrl), + 'url': "https://translate.google.co.jp/translate?hl=" + S.get().targetLang + "&sl=auto&u=" + encodeURIComponent(info.linkUrl), 'active': true, 'index': tab.index + 1 }); diff --git a/simple-translate/manifest.json b/simple-translate/manifest.json index 4e2d659..251f734 100644 --- a/simple-translate/manifest.json +++ b/simple-translate/manifest.json @@ -1,6 +1,6 @@ { "manifest_version": 2, - "version": "1.2.1", + "version": "1.3.1", "name": "__MSG_extName__", "description": "__MSG_extDescription__", @@ -12,7 +12,7 @@ } }, - "permissions": ["", "storage", "contextMenus"], + "permissions": ["", "storage", "contextMenus", "clipboardRead"], "options_ui": { "page": "options/options.html" @@ -28,7 +28,7 @@ }, "background": { - "scripts": ["background.js"] + "scripts": ["Settings.js", "background.js"] }, "browser_action": { @@ -49,7 +49,7 @@ { "matches": ["http://*/*", "https://*/*", ""], "css": ["simple-translate.css"], - "js": ["simple-translate.js"] + "js": ["Settings.js", "simple-translate.js"] } ] } diff --git a/simple-translate/options/options.css b/simple-translate/options/options.css index 63573c6..1b4fbe7 100644 --- a/simple-translate/options/options.css +++ b/simple-translate/options/options.css @@ -3,4 +3,24 @@ width: 100%; background-color: #d7d7db; height: 1px; border: none; -} \ No newline at end of file +} +input[type="number"] { + -moz-appearance:textfield; + width: 50px; +} +#save{ + width: auto; + height: 30px; + + margin-left: ; + margin-right: auto; + + + color: #111; + border: 1px solid #bbb; + border-radius: 2px; + background-color: #fbfbfb; +} +#save:hover{ + background: #f5f5f5; +} diff --git a/simple-translate/options/options.html b/simple-translate/options/options.html index c7b1ea0..e0beb31 100644 --- a/simple-translate/options/options.html +++ b/simple-translate/options/options.html @@ -12,21 +12,21 @@

-

-

-


-

@@ -35,7 +35,43 @@

+
+

+ 翻訳パネルのサイズ + + +

+

+ +

+ +

+ ボタンの表示位置 + + +

+ +

+ +

+ +
+ + + diff --git a/simple-translate/options/options.js b/simple-translate/options/options.js index 9f86cf4..144c12f 100644 --- a/simple-translate/options/options.js +++ b/simple-translate/options/options.js @@ -1,44 +1,17 @@ -var targetLang = document.getElementById("targetLang"); -var ifShowButton = document.getElementsByName("ifShowButton").item(0); -var ifCheckLang = document.getElementsByName("ifCheckLang").item(0); -var ifShowMenu = document.getElementsByName("ifShowMenu").item(0); -var ifChangeSecondLang = document.getElementsByName("ifChangeSecondLang").item(0); -var secondTargetLang = document.getElementById("secondTargetLang"); +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +let S = new settingsObj() +let targetLang = document.getElementById("targetLang"); +let secondTargetLang = document.getElementById("secondTargetLang"); targetLang.innerHTML=browser.i18n.getMessage("langList"); secondTargetLang.innerHTML=browser.i18n.getMessage("langList"); -document.getElementById("targetLangLabel").innerHTML=browser.i18n.getMessage("langListLabel"); -document.getElementById("ifShowButtonLabel").innerHTML=browser.i18n.getMessage("ifShowButtonLabel"); -document.getElementById("ifCheckLangLabel").innerHTML=browser.i18n.getMessage("ifCheckLangLabel"); -document.getElementById("ifShowMenuLabel").innerHTML=browser.i18n.getMessage("ifShowMenuLabel"); -document.getElementById("ifChangeSecondLangLabel").innerHTML=browser.i18n.getMessage("ifChangeSecondLangLabel"); -document.getElementById("secondTargetLangLabel").innerHTML=browser.i18n.getMessage("secondTargetLangLabel"); +S.initOptionsPage().then(function(){ + document.getElementById("save").addEventListener('click', function(){ + S.saveOptionsPage(); + }); +}) -//設定を読み込んで反映 -browser.storage.local.get(["targetLang", "ifShowButton", "ifCheckLang", "ifShowMenu", "ifChangeSecondLang", "secondTargetLang"], function (value) { - targetLang.value = value.targetLang; - ifShowButton.checked=value.ifShowButton; - ifCheckLang.checked=value.ifCheckLang; - ifShowMenu.checked=value.ifShowMenu; - ifChangeSecondLang.checked=value.ifChangeSecondLang; - secondTargetLang.value=value.secondTargetLang; -}); - -function save() { - browser.storage.local.set({ - 'targetLang': targetLang.value, - 'ifShowButton': ifShowButton.checked, - 'ifCheckLang': ifCheckLang.checked, - 'ifShowMenu': ifShowMenu.checked, - 'ifChangeSecondLang': ifChangeSecondLang.checked, - 'secondTargetLang': secondTargetLang.value - }, function () {}); -} - -targetLang.addEventListener("change", save); -ifShowButton.addEventListener("change", save); -ifCheckLang.addEventListener("change", save); -ifShowMenu.addEventListener("change", save); -ifChangeSecondLang.addEventListener("change", save); -secondTargetLang.addEventListener("change", save); \ No newline at end of file diff --git a/simple-translate/popup/popup.css b/simple-translate/popup/popup.css index bf0043a..7b74a33 100644 --- a/simple-translate/popup/popup.css +++ b/simple-translate/popup/popup.css @@ -1,5 +1,5 @@ body { - font-family: 'Hiragino Kaku Gothic Pro', 'ヒラギノ角ゴ Pro W3', 'Meiryo', 'メイリオ', 'Osaka', 'MS PGothic', 'arial', 'helvetica', 'sans-serif'; + font-family: 'Sugoe UI','Hiragino Kaku Gothic Pro', 'ヒラギノ角ゴ Pro W3', 'Meiryo', 'メイリオ', 'Osaka', 'MS PGothic', 'arial', 'helvetica', 'sans-serif'; text-align: left; font-size: 13px; height: auto; @@ -20,9 +20,8 @@ hr { p {} textarea { - font-family: 'Hiragino Kaku Gothic Pro', 'ヒラギノ角ゴ Pro W3', 'Meiryo', 'メイリオ', 'Osaka', 'MS PGothic', 'arial', 'helvetica', 'sans-serif'; + font: inherit; text-align: left; - font-size: 13px; resize: none; overflow: auto; max-height: 250px; @@ -42,6 +41,7 @@ textarea { #target { max-height: 250px; + min-height: 20px; overflow-y: auto; word-wrap: break-word; } @@ -51,9 +51,8 @@ textarea { } #langList { - font-family: 'Hiragino Kaku Gothic Pro', 'ヒラギノ角ゴ Pro W3', 'Meiryo', 'メイリオ', 'Osaka', 'MS PGothic', 'arial', 'helvetica', 'sans-serif'; + font: inherit; text-align: left; - font-size: 13px; float: right; margin-bottom: 20px; max-width: 140px; diff --git a/simple-translate/popup/popup.html b/simple-translate/popup/popup.html index 0455b13..1f9daf4 100644 --- a/simple-translate/popup/popup.html +++ b/simple-translate/popup/popup.html @@ -10,7 +10,7 @@
- +

@@ -20,6 +20,7 @@ + diff --git a/simple-translate/popup/popup.js b/simple-translate/popup/popup.js index 82798bd..61a3a44 100644 --- a/simple-translate/popup/popup.js +++ b/simple-translate/popup/popup.js @@ -1,10 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +let S=new settingsObj(); +//設定を読み出し +S.init().then(function(value){ + defaultTargetLang = value.targetLang; + targetLang = value.targetLang; + secondTargetLang = value.secondTargetLang; + ifChangeSecondLang=value.ifChangeSecondLang; + langList.value = targetLang; //リスト初期値をセット + langList.addEventListener("change", changeLang); + + document.body.style.fontSize=value.fontSize; +}) + let target = document.getElementById("target"); let langList = document.getElementById("langList"); let textarea = document.getElementById("textarea"); -const initialText = browser.i18n.getMessage("initialTextArea"); langList.innerHTML = browser.i18n.getMessage("langList"); -textarea.innerText = initialText; +const initialText = browser.i18n.getMessage("initialTextArea"); +textarea.placeholder= initialText; let targetLang; let secondTargetLang; @@ -12,19 +29,11 @@ let defaultTargetLang; let ifChangeSecondLang; let sourceWord = ""; -browser.storage.onChanged.addListener(getTargetLang); -getTargetLang(); //翻訳先言語初期化 -//設定を読み出し -function getTargetLang() { - browser.storage.local.get(["targetLang", "secondTargetLang","ifChangeSecondLang"], function (value) { - defaultTargetLang = value.targetLang; - targetLang = value.targetLang; - secondTargetLang = value.secondTargetLang; - ifChangeSecondLang=value.ifChangeSecondLang; - langList.value = targetLang; //リスト初期値をセット - langList.addEventListener("change", changeLang); - }); -} +/* +//Firefoxの仕様上popup.htmlでfocusが効かないため使えない +textarea.focus(); +document.execCommand("paste"); +*/ //翻訳先言語変更時に更新 function changeLang() { @@ -73,7 +82,16 @@ function refleshSource() { } } +textarea.addEventListener("paste", resize) + textarea.addEventListener("keydown", resize); + +textarea.addEventListener("keyup", function (event) { + //if (event.keyCode == 13) resize(); + resize(); + inputText(); +}); + //テキストボックスをリサイズ function resize() { setTimeout(function () { @@ -87,17 +105,9 @@ textarea.addEventListener("click", textAreaClick, { }); //テキストエリアクリック時の処理 function textAreaClick() { - if (textarea.value == initialText) { - textarea.value = ""; - } else { - textarea.select(); - } + textarea.select(); } -textarea.addEventListener("keyup", function (event) { - if (event.keyCode == 13) resize(); - inputText(); -}); //文字入力時の処理 function inputText() { sourceWord = textarea.value; diff --git a/simple-translate/simple-translate.css b/simple-translate/simple-translate.css index 59865ef..de28243 100644 --- a/simple-translate/simple-translate.css +++ b/simple-translate/simple-translate.css @@ -2,8 +2,9 @@ all: initial; background-color: #fff; box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08); - border-radius: 3px; - background-image: url("icons/16.png"); + border-radius: 10%; + background-image: url("icons/512.png"); + background-size:75%; background-repeat: no-repeat; background-position: center; height: 22px; @@ -21,7 +22,6 @@ #simple-translate-panel { all: initial; background-color: #fff; - /*opacity: 0;*/ box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08); border-radius: 3px; min-height: 20px; @@ -30,7 +30,6 @@ height: auto; min-width: 10px; max-width: 300px; - width:300px;/*消すかも*/ position: fixed; padding: 10px 18px; z-index: 2147483646; @@ -42,14 +41,11 @@ #simple-translate-panel p { all: initial; - font-size: 13px; - font-family: 'Hiragino Kaku Gothic Pro', 'ヒラギノ角ゴ Pro W3', Meiryo, メイリオ, Osaka, 'MS PGothic', arial, helvetica, sans-serif; + font-family: 'Sugoe UI','Hiragino Kaku Gothic Pro', 'ヒラギノ角ゴ Pro W3', 'Meiryo', 'メイリオ', 'Osaka', 'MS PGothic', 'arial', 'helvetica', 'sans-serif' !important; text-align: left; display: inline-block; margin: 0; word-wrap: break-word; - transition-duration: 200ms; - animation-name: simple-translate-fadein; } #simple-translate-panel p::-moz-selection { diff --git a/simple-translate/simple-translate.js b/simple-translate/simple-translate.js index 89bbda9..51b19b0 100644 --- a/simple-translate/simple-translate.js +++ b/simple-translate/simple-translate.js @@ -1,26 +1,15 @@ -document.body.insertAdjacentHTML("beforeend", "

...

"); //body末尾にボタン配置 +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +document.body.insertAdjacentHTML("beforeend", "

...

"); //body末尾にボタン配置 var button = document.getElementById("simple-translate-button"); var panel = document.getElementById("simple-translate-panel"); -var popup = document.getElementById("simple-translate-popup"); var selectionWord; var clickPosition; -var targetLang; -var ifShowButton; -var ifCheckLang; - - -getSetting(); -browser.storage.onChanged.addListener(getSetting); -//設定を読み出し -function getSetting() { - browser.storage.local.get(["targetLang", "ifShowButton", "ifCheckLang"], function (value) { - targetLang = value.targetLang; - ifShowButton = value.ifShowButton; - ifCheckLang = value.ifCheckLang; - }); -} - +let S=new settingsObj(); +S.init(); window.addEventListener("mouseup", Select, false); //テキスト選択時の処理 ダブルクリックした時2回処理が走るのを何とかしたい function Select(e) { @@ -29,7 +18,7 @@ function Select(e) { selectionWord = String(window.getSelection()); if ((selectionWord.length !== 0) && (e.button == 0) && (e.target.id !== "simple-translate-panel") && (e.target.parentElement.id !== "simple-translate-panel")) { //選択範囲が存在かつ左クリックかつパネル以外のとき clickPosition=e; - if (ifShowButton) {//ボタンを表示 + if (S.get().ifShowButton) {//ボタンを表示 checkLang().then(function (results) { if (results) popupButton(e); }); @@ -41,12 +30,12 @@ function Select(e) { //選択テキストの言語をチェックして返す function checkLang() { return new Promise(function (resolve, reject) { - if(ifCheckLang){ //設定がオンなら + if(S.get().ifCheckLang){ //設定がオンなら getRequest(selectionWord.substr(0, 100)) //先頭100文字を抽出して言語を取得 .then(function (results) { let lang = results.response[2]; let percentage = results.response[6]; - resolve(lang != targetLang && percentage > 0); //真偽値を返す + resolve(lang != S.get().targetLang && percentage > 0); //真偽値を返す }); }else { //設定がオフならtrueを返す resolve(true); @@ -56,11 +45,17 @@ function checkLang() { //ボタンを表示 function popupButton(e) { - if (ifShowButton) { - button.style.left = e.clientX + 10 + 'px'; - button.style.top = e.clientY + 5 + 'px'; - button.style.display = 'block'; - } + let position; + let buttonSize=S.get().buttonSize; + + if(S.get().buttonPosition=="rightUp") position=(-1*buttonSize)-10; + else if(S.get().buttonPosition=="rightDown") position=10; + + button.style.left = e.clientX + 10 + 'px'; + button.style.top = e.clientY + position + 'px'; + button.style.width=S.get().buttonSize+"px"; + button.style.height=S.get().buttonSize+"px"; + button.style.display = 'block'; } button.addEventListener("click", function (e) { translate(); @@ -85,7 +80,7 @@ function getRequest(word) { return new Promise(function (resolve, reject) { let xhr = new XMLHttpRequest(); xhr.responseType = 'json'; - let url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" + targetLang + "&dt=t&q=" + encodeURIComponent(word); + let url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" + S.get().targetLang + "&dt=t&q=" + encodeURIComponent(word); xhr.open("GET", url); xhr.send(); xhr.onload = function () { @@ -129,7 +124,7 @@ function hidePanel(e) { //パネルがウィンドウ外にはみ出る時に位置を調整 function panelPosition(e) { var p = new Object(); - panel.style.width = '300px'; + panel.style.width = S.get().width+'px';//300px var panelHeight = panel.clientHeight; var panelWidth = parseInt(window.getComputedStyle(panel.getElementsByTagName("p")[0], null).width); //一旦パネルの横幅を300にしてpの横幅を取得 @@ -137,16 +132,20 @@ function panelPosition(e) { if (e.clientX + panelWidth > window.innerWidth - 80) { p.x = window.innerWidth - panelWidth - 80; } else { - p.x = e.clientX + 10; + p.x = e.clientX; } if (e.clientY + panelHeight > window.innerHeight - 30) { p.y = window.innerHeight - panelHeight - 30; } else { - p.y = e.clientY + 10; + p.y = e.clientY; } panel.style.width = 'auto'; //panelWidth + 'px'; panel.style.top = p.y + 'px'; panel.style.left = p.x + 'px'; + + panel.style.maxWidth=S.get().width+"px"; + panel.style.maxHeight=S.get().height+"px"; + panel.getElementsByTagName("p")[0].style.fontSize=S.get().fontSize+"px"; } diff --git a/simple-translate/web-ext-artifacts/simple_translate-1.3.0.zip b/simple-translate/web-ext-artifacts/simple_translate-1.3.0.zip new file mode 100644 index 0000000..8ea2d50 Binary files /dev/null and b/simple-translate/web-ext-artifacts/simple_translate-1.3.0.zip differ diff --git a/simple-translate/web-ext-artifacts/simple_translate-1.3.1.zip b/simple-translate/web-ext-artifacts/simple_translate-1.3.1.zip new file mode 100644 index 0000000..37c15c7 Binary files /dev/null and b/simple-translate/web-ext-artifacts/simple_translate-1.3.1.zip differ