Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: polyfill.js

Issue 29573905: Issue 4580 - Replace ext.devtools with devtools Base URL: https://hg.adblockplus.org/adblockplusui/
Patch Set: Rebase Created Oct. 18, 2017, 1:37 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ext/devtools.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
}
}
};
-}());
+}
« no previous file with comments | « ext/devtools.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld