Index: polyfill.js |
=================================================================== |
--- a/polyfill.js |
+++ b/polyfill.js |
@@ -12,21 +12,118 @@ |
* GNU General Public License for more details. |
* |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
"use strict"; |
-(function() |
Manish Jethani
2017/10/18 01:41:31
This is not required.
|
{ |
window.browser = {}; |
- /* I18n */ |
+ /* runtime */ |
+ |
+ let messageQueue = []; |
+ let maxMessageId = -1; |
+ let backgroundWindow = null; |
Manish Jethani
2017/10/18 01:41:31
We have to maintain a reference to the background
|
+ |
+ function backgroundPageLoadedHandler(event) |
+ { |
+ if (event.data.type == "backgroundPageLoaded") |
+ { |
+ window.removeEventListener("message", backgroundPageLoadedHandler); |
Manish Jethani
2017/10/18 01:41:31
Removing the listener first, I hope it's OK.
|
+ |
+ backgroundWindow = event.source; |
+ |
+ let queue = messageQueue || []; |
+ messageQueue = null; |
+ for (let message of queue) |
+ backgroundWindow.postMessage(message, "*"); |
+ } |
+ } |
+ |
+ window.addEventListener("message", backgroundPageLoadedHandler); |
+ |
+ function sendRawMessage(message) |
+ { |
+ if (messageQueue) |
+ messageQueue.push(message); |
+ else |
+ backgroundWindow.postMessage(message, "*"); |
+ } |
+ |
+ browser.runtime = { |
+ connect() |
+ { |
+ sendRawMessage({type: "connect"}); |
+ return {onMessage: ext.onMessage}; |
+ }, |
+ |
+ sendMessage(message, responseCallback) |
+ { |
+ let messageId = ++maxMessageId; |
+ |
+ sendRawMessage({type: "message", messageId, payload: message}); |
+ |
+ if (responseCallback) |
+ { |
+ let callbackWrapper = event => |
+ { |
+ if (event.data.type == "response" && |
+ event.data.messageId == messageId) |
+ { |
+ window.removeEventListener("message", callbackWrapper); |
+ responseCallback(event.data.payload); |
+ } |
+ }; |
+ |
+ window.addEventListener("message", callbackWrapper); |
+ } |
+ }, |
+ |
+ onConnect: { |
+ addListener(callback) |
+ { |
+ window.addEventListener("message", event => |
+ { |
+ if (event.data.type == "connect") |
+ { |
+ callback({ |
+ postMessage(message) |
+ { |
+ event.source.postMessage({ |
+ type: "message", |
+ messageId: -1, |
+ payload: message |
+ }, "*"); |
+ } |
+ }); |
+ } |
+ }); |
+ } |
+ } |
+ }; |
+ |
+ /* devtools */ |
+ |
+ if (top.location.pathname == "/devtools-panel.html") |
+ { |
+ browser.devtools = { |
+ panels: { |
+ openResource() {} |
+ }, |
+ |
+ inspectedWindow: { |
+ reload() {} |
+ } |
+ }; |
+ } |
+ |
+ /* i18n */ |
let getLocaleCandidates = function(selectedLocale) |
{ |
let candidates = []; |
let defaultLocale = "en_US"; |
// e.g. "ja-jp-mac" -> "ja_JP", note that the part after the second |
// dash is dropped, since we only support language and region |
@@ -132,9 +229,9 @@ |
return ""; |
let locale = locales.shift(); |
readCatalog(locale, "common.json"); |
readCatalog(locale, catalogFile); |
} |
} |
}; |
-}()); |
+} |