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 function sendRawMessage(message) | |
58 { | |
59 if (messageQueue) | |
60 messageQueue.push(message); | |
61 else | |
62 backgroundFrame.contentWindow.postMessage(message, "*"); | |
63 } | |
64 | |
65 chrome.runtime.connect = () => | |
66 { | |
67 sendRawMessage({type: "connect"}); | |
68 return {onMessage: ext.onMessage}; | |
69 }; | |
70 | |
71 chrome.runtime.sendMessage = (message, responseCallback) => | |
72 { | |
73 let messageId = ++maxMessageId; | |
74 | |
75 sendRawMessage({type: "message", messageId, payload: message}); | |
76 | |
77 if (responseCallback) | |
78 { | |
79 let callbackWrapper = event => | |
80 { | |
81 if (event.data.type == "response" && event.data.messageId == messageId) | |
82 { | |
83 window.removeEventListener("message", callbackWrapper); | |
84 responseCallback(event.data.payload); | |
85 } | |
86 }; | |
87 | |
88 window.addEventListener("message", callbackWrapper); | |
89 } | |
90 }; | |
91 }()); | 30 }()); |
LEFT | RIGHT |