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

Unified Diff: ext/content.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Patch Set: Fixed sender API Created Dec. 17, 2014, 7:37 p.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/common.js ('k') | firstRun.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/content.js
===================================================================
--- a/ext/content.js
+++ b/ext/content.js
@@ -15,132 +15,65 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
(function(global)
{
if (!global.ext)
global.ext = {};
- function getURLParameters(data)
+ var backgroundFrame = document.createElement("iframe");
+ backgroundFrame.setAttribute("src", "background.html" + window.location.search);
+ backgroundFrame.style.visibility = "hidden";
+ window.addEventListener("DOMContentLoaded", function()
{
- if (window.location.search)
+ document.body.appendChild(backgroundFrame);
+ }, false);
+
+ var messageQueue = [];
+ var maxMessageId = -1;
+ var loadHandler = function(event)
+ {
+ if (event.data.type == "backgroundPageLoaded")
{
- var params = window.location.search.substr(1).split("&");
- for (var i = 0; i < params.length; i++)
- {
- var parts = params[i].split("=", 2);
- if (parts.length == 2 && parts[0] in data)
- data[parts[0]] = decodeURIComponent(parts[1]);
- }
+ var queue = messageQueue;
+ messageQueue = null;
+ if (queue)
+ for (var i = 0; i < queue.length; i++)
+ backgroundFrame.contentWindow.postMessage(queue[i], "*");
+ window.removeEventListener("message", loadHandler, false);
}
}
-
- var subscriptions =[
- "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
- "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
- "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
- ];
-
- var listenerFilter = null;
+ window.addEventListener("message", loadHandler, false);
global.ext.backgroundPage = {
sendMessage: function(message, responseCallback)
{
- var respond = function(response)
+ var rawMessage = {
+ type: "message",
+ messageId: ++maxMessageId,
+ payload: message
+ };
+ if (messageQueue)
+ messageQueue.push(rawMessage);
+ else
+ backgroundFrame.contentWindow.postMessage(rawMessage, "*");
+
+ if (responseCallback)
{
- setTimeout(responseCallback.bind(responseCallback, response), 0);
- };
-
- var dispatchListenerNotification = function(action)
- {
- var match = /^subscription\.(.*)/.exec(action);
- if (match && listenerFilter && listenerFilter.indexOf(match[1]) >= 0)
+ var callbackWrapper = function(event)
{
- global.ext.onMessage._dispatch({
- type: "subscriptions.listen",
- action: match[1],
- args: Array.prototype.slice.call(arguments, 1)
- });
- }
- };
-
- switch (message.type)
- {
- case "app.doclink":
- respond("https://adblockplus.org/redirect?link=" + encodeURIComponent(message.args[0]));
- break;
- case "app.info":
- var response = {platform: "gecko", platformVersion: "34.0", application: "firefox", applicationVersion: "34.0"};
- getURLParameters(response);
- respond(response);
- break;
- case "app.issues":
- var response = {seenDataCorruption: false, filterlistsReinitialized: false};
- getURLParameters(response);
- respond(response);
- break;
- case "app.options":
- window.open("http://example.com/options.html", "_blank");
- break;
- case "subscriptions.get":
- respond(subscriptions);
- break;
- case "filters.blocked":
- var params = {blockedURLs: ""};
- getURLParameters(params);
- var blocked = params.blockedURLs.split(",");
- respond(blocked.indexOf(message.url) >= 0);
- break;
- case "subscriptions.toggle":
- var index = subscriptions.indexOf(message.url);
- if (index >= 0)
+ if (event.data.type == "response" && event.data.messageId == rawMessage.messageId)
{
- subscriptions.splice(index, 1);
- dispatchListenerNotification("subscription.removed", message.url);
+ window.removeEventListener("message", callbackWrapper, false);
+ responseCallback(event.data.payload);
}
- else
- {
- subscriptions.push(message.url);
- dispatchListenerNotification("subscription.added", message.url);
- }
- break;
- case "subscriptions.listen":
- listenerFilter = message.filter;
- break;
+ };
+ window.addEventListener("message", callbackWrapper, false);
}
}
};
- var EventTarget = function(cancelable)
+ window.addEventListener("unload", function()
{
- this._listeners = [];
- this._cancelable = cancelable;
- };
- EventTarget.prototype = {
- addListener: function(listener)
- {
- if (this._listeners.indexOf(listener) == -1)
- this._listeners.push(listener);
- },
- removeListener: function(listener)
- {
- var idx = this._listeners.indexOf(listener);
- if (idx != -1)
- this._listeners.splice(idx, 1);
- },
- _dispatch: function()
- {
- var result = null;
-
- for (var i = 0; i < this._listeners.length; i++)
- {
- result = this._listeners[i].apply(null, arguments);
-
- if (this._cancelable && result === false)
- break;
- }
-
- return result;
- }
- };
- global.ext.onMessage = new EventTarget();
+ global.ext.backgroundPage.sendMessage({type: "removePage"});
+ }, false);
})(this);
« no previous file with comments | « ext/common.js ('k') | firstRun.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld