avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 1 | var isoLangs={af:{name:"Afrikaans",nativeName:"Afrikaans"},sq:{name:"Albanian",nativeName:"Shqip"},am:{name:"Amharic",nativeName:"\u12a0\u121b\u122d\u129b"},ar:{name:"Arabic",nativeName:"\u0627\u0644\u0639\u0631\u0628\u064a\u0629"},hy:{name:"Armenian",nativeName:"\u0540\u0561\u0575\u0565\u0580\u0565\u0576"},az:{name:"Azerbaijani",nativeName:"az\u0259rbaycan dili"},eu:{name:"Basque",nativeName:"euskara"},be:{name:"Belarusian",nativeName:"\u0411\u0435\u043b\u0430\u0440\u0443\u0441\u043a\u0430\u044f"}, |
| 2 | bn:{name:"Bengali",nativeName:"\u09ac\u09be\u0982\u09b2\u09be"},bs:{name:"Bosnian",nativeName:"bosanski jezik"},bg:{name:"Bulgarian",nativeName:"\u0431\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 \u0435\u0437\u0438\u043a"},ca:{name:"Catalan",nativeName:"Catal\u00e0"},ceb:{name:"Cebuano",nativeName:"Bisaya"},ny:{name:"Chichewa",nativeName:"chiChe\u0175a"},zh:{name:"Chinese",nativeName:"\u4e2d\u6587 (Zh\u014dngw\u00e9n)"},co:{name:"Corsican",nativeName:"corsu"},hr:{name:"Croatian",nativeName:"hrvatski"}, |
| 3 | cs:{name:"Czech",nativeName:"\u010desky"},da:{name:"Danish",nativeName:"dansk"},nl:{name:"Dutch",nativeName:"Nederlands"},en:{name:"English",nativeName:"English"},eo:{name:"Esperanto",nativeName:"Esperanto"},et:{name:"Estonian",nativeName:"eesti"},tl:{name:"Filipino",nativeName:"Pilipino"},fi:{name:"Finnish",nativeName:"suomi"},fr:{name:"French",nativeName:"fran\u00e7ais"},fy:{name:"Frisian",nativeName:"Frysk"},gl:{name:"Galician",nativeName:"Galego"},ka:{name:"Georgian",nativeName:"\u10e5\u10d0\u10e0\u10d7\u10e3\u10da\u10d8"}, |
| 4 | de:{name:"German",nativeName:"Deutsch"},el:{name:"Greek",nativeName:"\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac"},gu:{name:"Gujarati",nativeName:"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0"},ht:{name:"Haitian Creole",nativeName:"Krey\u00f2l ayisyen"},ha:{name:"Hausa",nativeName:"Hausa"},haw:{name:"Hawaiian",nativeName:"Hausa"},iw:{name:"Hebrew",nativeName:"\u05e2\u05d1\u05e8\u05d9\u05ea"},hi:{name:"Hindi",nativeName:"\u0939\u093f\u0928\u094d\u0926\u0940, \u0939\u093f\u0902\u0926\u0940"},hmn:{name:"Hmong", |
| 5 | nativeName:"lus Hmoob"},hu:{name:"Hungarian",nativeName:"Magyar"},is:{name:"Icelandic",nativeName:"\u00cdslenska"},ig:{name:"Igbo",nativeName:"As\u1ee5s\u1ee5 Igbo"},id:{name:"Indonesian",nativeName:"Bahasa Indonesia"},ga:{name:"Irish",nativeName:"Gaeilge"},it:{name:"Italian",nativeName:"Italiano"},ja:{name:"Japanese",nativeName:"\u65e5\u672c\u8a9e (\u306b\u307b\u3093\u3054\uff0f\u306b\u3063\u307d\u3093\u3054)"},jv:{name:"Javanese",nativeName:"basa Jawa"},kn:{name:"Kannada",nativeName:"\u0c95\u0ca8\u0ccd\u0ca8\u0ca1"}, |
| 6 | kk:{name:"Kazakh",nativeName:"\u049a\u0430\u0437\u0430\u049b \u0442\u0456\u043b\u0456"},km:{name:"Khmer",nativeName:"\u1797\u17b6\u179f\u17b6\u1781\u17d2\u1798\u17c2\u179a"},ko:{name:"Korean",nativeName:"\ud55c\uad6d\uc5b4 (\u97d3\u570b\u8a9e)"},ku:{name:"Kurdish (Kurmanji)",nativeName:"Kurd\u00ee"},ky:{name:"Kyrgyz",nativeName:"\u043a\u044b\u0440\u0433\u044b\u0437 \u0442\u0438\u043b\u0438"},lo:{name:"Lao",nativeName:"\u0e9e\u0eb2\u0eaa\u0eb2\u0ea5\u0eb2\u0ea7"},la:{name:"Latin",nativeName:"latine"}, |
| 7 | lv:{name:"Latvian",nativeName:"latvie\u0161u valoda"},lt:{name:"Lithuanian",nativeName:"lietuvi\u0173 kalba"},lb:{name:"Luxembourgish",nativeName:"L\u00ebtzebuergesch"},mk:{name:"Macedonian",nativeName:"\u043c\u0430\u043a\u0435\u0434\u043e\u043d\u0441\u043a\u0438 \u0458\u0430\u0437\u0438\u043a"},mg:{name:"Malagasy",nativeName:"Malagasy fiteny"},ms:{name:"Malay",nativeName:"bahasa Melayu"},ml:{name:"Malayalam",nativeName:"\u0d2e\u0d32\u0d2f\u0d3e\u0d33\u0d02"},mt:{name:"Maltese",nativeName:"Malti"}, |
| 8 | mi:{name:"M\u0101ori",nativeName:"te reo M\u0101ori"},mr:{name:"Marathi",nativeName:"\u092e\u0930\u093e\u0920\u0940"},mn:{name:"Mongolian",nativeName:"\u043c\u043e\u043d\u0433\u043e\u043b"},my:{name:"Myanmar",nativeName:"\u1017\u1019\u102c\u1005\u102c"},ne:{name:"Nepali",nativeName:"\u0928\u0947\u092a\u093e\u0932\u0940"},no:{name:"Norwegian",nativeName:"Norsk"},ps:{name:"Pashto",nativeName:"\u067e\u069a\u062a\u0648"},fa:{name:"Persian",nativeName:"\u0641\u0627\u0631\u0633\u06cc"},pl:{name:"Polish", |
| 9 | nativeName:"polski"},pt:{name:"Portuguese",nativeName:"Portugu\u00eas"},pa:{name:"Punjabi",nativeName:"\u0a2a\u0a70\u0a1c\u0a3e\u0a2c"},ro:{name:"Romanian",nativeName:"rom\u00e2n\u0103"},ru:{name:"Russian",nativeName:"\u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a"},sm:{name:"Samoan",nativeName:"gagana faa Samoa"},gd:{name:"Scots Gaelic",nativeName:"G\u00e0idhlig"},sr:{name:"Serbian",nativeName:"\u0441\u0440\u043f\u0441\u043a\u0438 \u0458\u0435\u0437\u0438\u043a"},st:{name:"Sesotho", |
| 10 | nativeName:"Sesotho"},sn:{name:"Shona",nativeName:"chiShona"},sd:{name:"Sindhi",nativeName:"\u0938\u093f\u0928\u094d\u0927\u0940"},si:{name:"Sinhala",nativeName:"\u0dc3\u0dd2\u0d82\u0dc4\u0dbd"},sk:{name:"Slovak",nativeName:"sloven\u010dina"},sl:{name:"Slovenian",nativeName:"sloven\u0161\u010dina"},so:{name:"Somali",nativeName:"Soomaaliga"},es:{name:"Spanish",nativeName:"espa\u00f1ol"},su:{name:"Sundanese",nativeName:"Basa Sunda"},sw:{name:"Swahili",nativeName:"Kiswahili"},sv:{name:"Swedish",nativeName:"svenska"}, |
| 11 | tg:{name:"Tajik",nativeName:"\u0442\u043e\u04b7\u0438\u043a\u04e3"},ta:{name:"Tamil",nativeName:"\u0ba4\u0bae\u0bbf\u0bb4\u0bcd"},te:{name:"Telugu",nativeName:"\u0c24\u0c46\u0c32\u0c41\u0c17\u0c41"},th:{name:"Thai",nativeName:"\u0e44\u0e17\u0e22"},tr:{name:"Turkish",nativeName:"T\u00fcrk\u00e7e"},uk:{name:"Ukrainian",nativeName:"\u0443\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430"},ur:{name:"Urdu",nativeName:"\u0627\u0631\u062f\u0648"},uz:{name:"Uzbek",nativeName:"zbek"},vi:{name:"Vietnamese", |
| 12 | nativeName:"Ti\u1ebfng Vi\u1ec7t"},cy:{name:"Welsh",nativeName:"Cymraeg"},xh:{name:"Xhosa",nativeName:"isiXhosa"},yi:{name:"Yiddish",nativeName:"\u05d9\u05d9\u05b4\u05d3\u05d9\u05e9"},yo:{name:"Yoruba",nativeName:"Yor\u00f9b\u00e1"},za:{name:"Zhuang, Chuang",nativeName:"Sa\u026f cue\u014b\u0185"},zu:{name:"Zulu",nativeName:"isiZulu"}}; |
| 13 | |
| 14 | function $(selector) { |
| 15 | return document.querySelector(selector); |
| 16 | } |
| 17 | |
| 18 | function $all(selector) { |
| 19 | return document.querySelectorAll(selector); |
| 20 | } |
| 21 | |
| 22 | function isEmpty(obj) { |
| 23 | return Object.keys(obj).length === 0; |
| 24 | } |
| 25 | |
| 26 | function i18n() { |
| 27 | $("#welcome").innerHTML = chrome.i18n.getMessage("options_welcome"); |
| 28 | $("#introduction").innerHTML = chrome.i18n.getMessage("options_introduction"); |
| 29 | //$("#languageselectheader").innerHTML = chrome.i18n.getMessage("options_languageselectheader"); |
| 30 | $("#otheroptionsheader").innerHTML = chrome.i18n.getMessage("options_otheroptionsheader"); |
| 31 | $("#varioustabs_label").innerHTML = chrome.i18n.getMessage("options_tabsoption_1"); |
| 32 | $("#uniquetab_label").innerHTML = chrome.i18n.getMessage("options_tabsoption_2"); |
| 33 | $("#panel_label").innerHTML = chrome.i18n.getMessage("options_tabsoption_3"); |
| 34 | $("#languages_add").innerHTML = chrome.i18n.getMessage("options_addlanguage_addbutton"); |
| 35 | $("#save").innerHTML = chrome.i18n.getMessage("options_savebutton"); |
| 36 | $("#credits").innerHTML = chrome.i18n.getMessage("options_credits"); |
| 37 | $("#credits_dialog h3").innerHTML = chrome.i18n.getMessage("options_credits"); |
| 38 | $("#languages_add_ok").innerHTML = chrome.i18n.getMessage("options_ok"); |
| 39 | $("#languages_add_cancel").innerHTML = chrome.i18n.getMessage("options_cancel"); |
| 40 | $("#credits_ok").innerHTML = chrome.i18n.getMessage("options_ok"); |
| 41 | $("#languages_add_dialog h3").innerHTML = chrome.i18n.getMessage("options_addlanguage"); |
| 42 | $("#language_label").innerHTML = chrome.i18n.getMessage("options_language_label"); |
| 43 | } |
| 44 | |
| 45 | function print_list_modal() { |
| 46 | $("#select_language").innerHTML = ""; |
| 47 | var heysortable = sortable.toArray(); |
| 48 | for (var language in isoLangs) { |
| 49 | if (!inArray(language, heysortable)) { |
| 50 | var el = document.createElement('option'); |
| 51 | el.setAttribute('value', language); |
| 52 | el.innerText = isoLangs[language]["name"]+" ("+isoLangs[language]["nativeName"]+")"; |
| 53 | $("#select_language").appendChild(el); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | function init() { |
| 59 | i18n(); |
| 60 | chrome.storage.sync.get(null, function(items) { |
| 61 | // If no settings are set |
| 62 | if (isEmpty(items)) { |
| 63 | items = {'translateinto': {}, 'uniquetab': ''}; |
| 64 | chrome.storage.sync.set({'translateinto': {}, 'uniquetab': ''}); |
| 65 | } |
| 66 | |
| 67 | // Check the checkbox of the window opening |
| 68 | if (items.uniquetab === "yep") |
| 69 | $("#uniquetab").checked = true; |
| 70 | if (items.uniquetab === "") |
| 71 | $("#varioustabs").checked = true; |
| 72 | if (items.uniquetab === "panel") |
| 73 | $("#panel").checked = true; |
| 74 | |
| 75 | // Add event listeners for certain buttons and links |
| 76 | $("#panelsflag").addEventListener('click', function() { event.preventDefault(); chrome.tabs.create({url: 'chrome://flags/#enable-panels'}); }); |
| 77 | $("#save").addEventListener('click', function() { |
| 78 | save_options(); |
| 79 | }); |
| 80 | |
| 81 | // Print selected language list |
| 82 | var languages = items.translateinto; |
| 83 | |
| 84 | if (languages) { |
| 85 | for (var language_id in languages) { |
| 86 | var language = languages[language_id]; |
| 87 | var el = document.createElement('li'); |
| 88 | el.setAttribute('data-language', language); |
| 89 | el.setAttribute('data-id', language); |
| 90 | el.innerHTML = isoLangs[language]["name"]+" ("+isoLangs[language]["nativeName"]+")"+"<span data-language='"+language+"' class='delete'>x</span>"; |
| 91 | $("#languages").appendChild(el); |
| 92 | $("#languages li[data-language="+language+"] .delete").addEventListener('click', function() { |
| 93 | $("#languages").removeChild($("li[data-language="+this.getAttribute("data-language")+"]")); |
| 94 | print_list_modal(); |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Initiate Sortable |
| 100 | sortable = new Sortable($("#languages"), { |
| 101 | animation: 150 |
| 102 | }); |
| 103 | |
| 104 | // Handling The Dialog |
| 105 | $("#languages_add").addEventListener('click', function() { $("dialog#languages_add_dialog").showModal(); $("#select_language").focus(); }); |
| 106 | $("#languages_add_cancel").addEventListener('click', function() { $("dialog#languages_add_dialog").close(); }); |
| 107 | $("#languages_add_ok").addEventListener('click', function() { |
| 108 | var el = document.createElement('li'); |
| 109 | var language = $("#select_language").value; |
| 110 | if (inArray(language, sortable.toArray())) { |
| 111 | return; |
| 112 | } |
| 113 | el.setAttribute('data-language', language); |
| 114 | el.setAttribute('data-id', language); |
| 115 | el.innerHTML = isoLangs[language]["name"]+" ("+isoLangs[language]["nativeName"]+")"+"<span data-language='"+language+"' class='delete'>x</span>"; |
| 116 | $("#languages").appendChild(el); |
| 117 | var selection = $("#select_language option[value="+language+"]"); |
| 118 | selection.parentNode.removeChild(selection); |
| 119 | $("#languages li[data-id="+language+"] .delete").addEventListener('click', function() { |
| 120 | $("#languages").removeChild($("li[data-language="+this.getAttribute("data-language")+"]")); |
| 121 | print_list_modal(); |
| 122 | }); |
| 123 | $("dialog").close(); |
| 124 | }); |
| 125 | |
| 126 | // About credits... |
| 127 | var xhr = new XMLHttpRequest(); |
| 128 | xhr.onreadystatechange = function() { |
| 129 | if (xhr.readyState == 4 && xhr.status == 200) { |
| 130 | var json = JSON.parse(xhr.responseText), printhtml = ""; |
| 131 | for (var item in json) { |
| 132 | printhtml += "<div>"; |
| 133 | if (json[item].url) { |
| 134 | printhtml += "<a href='"+json[item].url+"' class='homepage' target='_blank'>"+chrome.i18n.getMessage("options_credits_homepage")+"</a>"; |
| 135 | } |
| 136 | printhtml += "<h4>"+json[item].name+"</h4>"; |
| 137 | if (json[item].author) { |
| 138 | printhtml += "<p class='author'>"+chrome.i18n.getMessage("options_credits_by")+" "+json[item].author; |
| 139 | } |
| 140 | if (json[item].license) { |
| 141 | printhtml += " – "+json[item].license+"</p>"; |
| 142 | } else { |
| 143 | printhtml += "</p>"; |
| 144 | } |
| 145 | printhtml += "</div>"; |
| 146 | } |
| 147 | $("dialog#credits_dialog .content_area").innerHTML = printhtml; |
| 148 | window.onhashchange = function() { |
| 149 | if (location.hash == "#credits") { |
| 150 | $("dialog#credits_dialog").showModal(); |
| 151 | $("#credits_ok").focus(); |
| 152 | } |
| 153 | } |
| 154 | if (location.hash == "#credits") { |
| 155 | $("dialog#credits_dialog").showModal(); |
| 156 | $("#credits_ok").focus(); |
| 157 | } |
| 158 | $("#credits_ok").addEventListener('click', function() { $("dialog#credits_dialog").close(); }); |
| 159 | $("dialog#credits_dialog").addEventListener('close', function() { history.pushState("", document.title, window.location.pathname + window.location.search); }); |
| 160 | } |
| 161 | } |
| 162 | xhr.open("GET", "json/credits.json", true); |
| 163 | xhr.send(); |
| 164 | |
| 165 | // Print language list in the modal dialog |
| 166 | print_list_modal(); |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | function save_options() { |
| 171 | var languages = document.getElementById("languages"); |
| 172 | var options = {"uniquetab": "", "translateinto": {}}; |
| 173 | |
| 174 | options.uniquetab = radio_selected("uniquetab"); |
| 175 | |
| 176 | var selected_languages = sortable.toArray(); |
| 177 | |
| 178 | var i = 0; |
| 179 | for (var language_id in selected_languages) { |
| 180 | var language = selected_languages[language_id]; |
| 181 | options.translateinto[i] = language; |
| 182 | i++; |
| 183 | } |
| 184 | |
| 185 | chrome.storage.sync.set(options, function() { |
| 186 | var background = chrome.extension.getBackgroundPage(); |
| 187 | |
| 188 | background.translator_tab = false; |
| 189 | background.translator_window = false; |
| 190 | window.close(); |
| 191 | }); |
| 192 | |
| 193 | // We don't need the following code because the background.js is already listening to changes in chrome.sync ;-) Yeeey! |
| 194 | //chrome.extension.getBackgroundPage().createmenus(JSON.stringify(options)); |
| 195 | } |
| 196 | |
| 197 | function toObject(arr) { |
| 198 | var rv = {}; |
| 199 | for (var i = 0; i < arr.length; ++i) |
| 200 | if (arr[i] !== undefined) rv[i] = arr[i]; |
| 201 | return rv; |
| 202 | } |
| 203 | |
| 204 | function radio_selected(a) { |
| 205 | var elements = document.getElementsByName(a); |
| 206 | |
| 207 | for (var i=0; i<elements.length; i++) |
| 208 | if (elements[i].checked) return elements[i].value; |
| 209 | } |
| 210 | |
| 211 | function inArray(needle, haystack) { |
| 212 | var length = haystack.length; |
| 213 | for(var i = 0; i < length; i++) { |
| 214 | if(haystack[i] == needle) return true; |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | window.addEventListener('load', init); |