LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 (function() | 20 (function() |
21 { | 21 { |
22 if (typeof ext == "undefined") | |
23 window.ext = {}; | |
24 | |
25 let backgroundFrame = document.createElement("iframe"); | 22 let backgroundFrame = document.createElement("iframe"); |
26 backgroundFrame.setAttribute("src", | 23 backgroundFrame.setAttribute("src", |
27 "background.html" + window.location.search); | 24 "background.html" + window.location.search); |
28 backgroundFrame.style.display = "none"; | 25 backgroundFrame.style.display = "none"; |
29 window.addEventListener("DOMContentLoaded", () => | 26 window.addEventListener("DOMContentLoaded", () => |
30 { | 27 { |
31 document.body.appendChild(backgroundFrame); | 28 document.body.appendChild(backgroundFrame); |
32 }); | 29 }); |
33 | |
34 let messageQueue = []; | |
35 let maxMessageId = -1; | |
36 let loadHandler = (event) => | |
37 { | |
38 if (event.data.type == "backgroundPageLoaded") | |
39 { | |
40 let queue = messageQueue; | |
41 messageQueue = null; | |
42 if (queue) | |
43 { | |
44 for (let message of queue) | |
45 backgroundFrame.contentWindow.postMessage(message, "*"); | |
46 } | |
47 window.removeEventListener("message", loadHandler); | |
48 } | |
49 }; | |
50 window.addEventListener("message", loadHandler); | |
51 | |
52 /* Polyfills */ | |
53 | |
54 if (!("runtime" in chrome)) | |
55 chrome.runtime = {}; | |
56 | |
57 chrome.runtime.sendMessage = (message, responseCallback) => | |
58 { | |
59 let messageId = ++maxMessageId; | |
60 | |
61 let rawMessage = {type: "message", messageId, payload: message}; | |
62 | |
63 if (messageQueue) | |
64 messageQueue.push(rawMessage); | |
65 else | |
66 backgroundFrame.contentWindow.postMessage(rawMessage, "*"); | |
67 | |
68 if (responseCallback) | |
69 { | |
70 let callbackWrapper = event => | |
71 { | |
72 if (event.data.type == "response" && event.data.messageId == messageId) | |
73 { | |
74 window.removeEventListener("message", callbackWrapper); | |
75 responseCallback(event.data.payload); | |
76 } | |
77 }; | |
78 | |
79 window.addEventListener("message", callbackWrapper); | |
80 } | |
81 }; | |
82 }()); | 30 }()); |
LEFT | RIGHT |