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": ["
-
-
-
-
+ 翻訳パネルのサイズ
+
+
+
+