From db5befcf451b26e00b94aeff927099bc63c63e5d Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 11 Jun 2016 18:45:30 -0400 Subject: [PATCH 1/8] Add Gecko as a supported application to manifest --- ext/manifest.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/manifest.json b/ext/manifest.json index 3cb13480..29160d3b 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -26,5 +26,10 @@ "fg/js/frame.js", "fg/ttf/kanji-stroke-orders.ttf", "fg/ttf/vl-gothic-regular.ttf" - ] + ], + "applications": { + "gecko": { + "id": "yomichan-gecko@example.com" + } + } } From f12660af91726bebc39866f30a9c760733478fc8 Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 18 Jun 2016 19:02:07 -0400 Subject: [PATCH 2/8] Use non-deprecated options_ui instead of options_page in manifest --- ext/manifest.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/manifest.json b/ext/manifest.json index 29160d3b..d9367946 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -15,7 +15,9 @@ "css": ["fg/css/client.css"] }], "minimum_chrome_version": "45.0.0.0", - "options_page": "bg/options.html", + "options_ui": { + "page": "bg/options.html" + }, "permissions": ["storage"], "web_accessible_resources": [ "fg/css/frame.css", From b4fe1f1fa6064162adfa7b6eee5f397f190ae8bf Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 18 Jun 2016 19:42:00 -0400 Subject: [PATCH 3/8] Add polyfills for Gecko's WebExtension implementation Gecko currently does not support chrome.storage.sync and chrome.runtime.onInstalled. Use chrome.storage.local instead of sync and ignore calls to onInstalled. The implication of not having runtime.onInstalled is that the options page is not shown on first-run. --- ext/bg/background.html | 1 + ext/bg/js/polyfill-gecko.js | 15 +++++++++++++++ ext/bg/options.html | 1 + 3 files changed, 17 insertions(+) create mode 100644 ext/bg/js/polyfill-gecko.js diff --git a/ext/bg/background.html b/ext/bg/background.html index c35e917d..c6a84636 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -1,6 +1,7 @@ + diff --git a/ext/bg/js/polyfill-gecko.js b/ext/bg/js/polyfill-gecko.js new file mode 100644 index 00000000..8c7cc403 --- /dev/null +++ b/ext/bg/js/polyfill-gecko.js @@ -0,0 +1,15 @@ +// Gecko does not currently support chrome.storage.sync, use storage.local instead +// https://bugzilla.mozilla.org/show_bug.cgi?id=1220494 +if (!chrome.storage.sync) { + chrome.storage.sync = chrome.storage.local; +} + +// Gecko does not currently support chrome.runtime.onInstalled, just ignore calls to it +// (https://bugzilla.mozilla.org/show_bug.cgi?id=1252871) +if (!chrome.runtime.onInstalled) { + chrome.runtime.onInstalled = { + 'addListener' : function(){}, + 'hasListener' : function(){}, + 'removeListener' : function(){} + }; +} \ No newline at end of file diff --git a/ext/bg/options.html b/ext/bg/options.html index 289a0f60..a2b4a56b 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -163,6 +163,7 @@ + From 5e0ac4e8ea32c3e33de38db02d7dcbebfdd05cda Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 18 Jun 2016 18:33:51 -0400 Subject: [PATCH 4/8] Workaround spidermonkey bug so dictionary data loads We need to make a copy of the iteration variable in the for-of loop so that the distinct values are available in the callback. --- ext/bg/js/translator.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index bf1538e2..261196d2 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -46,8 +46,17 @@ class Translator { const pendingLoads = []; for (let key of files) { + /* + Spidermonkey does not implement lexical bindings for for-of loop + (see https://bugzilla.mozilla.org/show_bug.cgi?id=449811) + so we need to manually make a new declaration for key. + Otherwise key will always remain the same in the callback to loadData + and the dictionary data will not be set correctly + */ + let key_ = key; pendingLoads.push(key); Translator.loadData(this.paths[key], (response) => { + let key = key_ switch (key) { case 'rules': this.deinflector.setRules(JSON.parse(response)); From 28de6a4d6ecac9dade41f511c426fc16a443c537 Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 18 Jun 2016 18:29:49 -0400 Subject: [PATCH 5/8] Add mimetype to XHR call This silences warnings about the JSON files being ill-formed --- ext/bg/js/translator.js | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 261196d2..d79ec6d1 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -237,6 +237,7 @@ class Translator { static loadData(url, callback) { const xhr = new XMLHttpRequest(); + xhr.overrideMimeType("application/json"); xhr.addEventListener('load', () => callback(xhr.responseText)); xhr.open('GET', chrome.extension.getURL(url), true); xhr.send(); From 651231fbc136ad2574c18d837151aa4dd6c2306e Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 18 Jun 2016 18:35:41 -0400 Subject: [PATCH 6/8] Use iFrame srcdoc attribute to set the content of the popup Previously the contentdocument of the iFrame was opened and document.write() was being used set the contents of the popup. In Gecko, content scripts do not have the same security context as of the embedded page, so the call to document.open() results in a SecurityError. --- ext/fg/js/popup.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 78106319..4c2b18f7 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -62,10 +62,8 @@ class Popup { return; } - const doc = this.popup.contentDocument; - doc.open(); - doc.write(content); - doc.close(); + const doc = this.popup; + doc.srcdoc=content; } sendMessage(action, params, callback) { From 099d48ef48a065001a6b4a0ab29c567935af1f25 Mon Sep 17 00:00:00 2001 From: ispedals Date: Sat, 18 Jun 2016 18:52:02 -0400 Subject: [PATCH 7/8] Polyfill caretRangeFromPoint() Gecko did not implment the older caretRangeFromPoint() and instead implemented the newer caretPositionFromPoint() --- ext/fg/js/range.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ext/fg/js/range.js b/ext/fg/js/range.js index 182e242c..0befe279 100644 --- a/ext/fg/js/range.js +++ b/ext/fg/js/range.js @@ -16,6 +16,19 @@ * along with this program. If not, see . */ +// Polyfill caretRangeFromPoint() using the newer caretPositionFromPoint() +if (!document.caretRangeFromPoint){ + document.caretRangeFromPoint = function polyfillcaretRangeFromPoint(x,y){ + let range = document.createRange(); + let position = document.caretPositionFromPoint(x,y); + if (!position) { + return null; + } + range.setStart(position.offsetNode, position.offset); + range.setEnd(position.offsetNode, position.offset); + return range; + }; +} class Range { constructor(range) { From a9863de95e201c8e814ad7d6dacedae98b13748a Mon Sep 17 00:00:00 2001 From: ispedals Date: Sun, 19 Jun 2016 12:46:39 -0400 Subject: [PATCH 8/8] Request permission to access localhost Permission is required to do XHR request to the domain. Otherwise Gecko attempts to use CORS to negotiate the request, which AnkiConnect does not expect. --- ext/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/manifest.json b/ext/manifest.json index d9367946..70ce4a9a 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -18,7 +18,7 @@ "options_ui": { "page": "bg/options.html" }, - "permissions": ["storage"], + "permissions": ["*://127.0.0.1/*", "storage"], "web_accessible_resources": [ "fg/css/frame.css", "fg/img/add_kanji.png",