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

Delta Between Two Patch Sets: notification.js

Issue 29567749: Issue 5593 - Use messaging for the popup's notification code (Closed)
Left Patch Set: Created Oct. 6, 2017, 1:07 p.m.
Right Patch Set: Rebased Created Oct. 9, 2017, 4:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « dependencies ('k') | popup.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 /* global setPref */
19
18 "use strict"; 20 "use strict";
19 21
20 function getDocLinks(notification) 22 function getDocLinks(notification)
21 { 23 {
22 let docLinks = [];
23
24 if (!notification.links) 24 if (!notification.links)
25 return Promise.resolve(docLinks); 25 return Promise.resolve([]);
26 26
27 return Promise.all( 27 return Promise.all(
28 notification.links.map(link => 28 notification.links.map(link =>
29 { 29 {
30 return new Promise((resolve, reject) => 30 return new Promise((resolve, reject) =>
31 { 31 {
32 ext.backgroundPage.sendMessage({ 32 chrome.runtime.sendMessage({
Manish Jethani 2017/10/06 14:26:04 We should use runtime.sendMessage here so we lose
kzar 2017/10/06 20:05:34 Done.
33 type: "app.get", 33 type: "app.get",
34 what: "doclink", 34 what: "doclink",
35 link 35 link
36 }, resolve); 36 }, resolve);
37 }); 37 });
38 }) 38 })
39 ); 39 );
40 }
41
42 function getActiveDisplayedNotification()
Manish Jethani 2017/10/06 14:26:04 Nit: I feel like "displayed" implies it's already
kzar 2017/10/06 20:05:34 Acknowledged.
43 {
44 return new Promise((resolve, reject) =>
45 {
46 ext.backgroundPage.sendMessage({
47 type: "notifications.getActive"
48 }, notification =>
Manish Jethani 2017/10/06 14:26:05 I may be wrong about this, but "notification =>" h
kzar 2017/10/06 20:05:34 I think this looks OK like it is. If we decide to
49 {
50 if (!notification)
Manish Jethani 2017/10/06 14:26:05 We would normally add braces here around the if bl
kzar 2017/10/06 20:05:34 IIRC we considered enforcing that braces for if...
51 resolve(notification);
52 else
53 {
54 ext.backgroundPage.sendMessage({
55 type: "notifications.shouldDisplay",
56 method: "popup",
57 notificationType: notification.notificationType
Manish Jethani 2017/10/06 14:26:05 It should be notification.type, not notification.n
kzar 2017/10/06 20:05:34 Acknowledged.
58 }, shouldDisplay => { resolve(shouldDisplay && notification); });
59 }
60 });
61 });
62 } 40 }
63 41
64 function insertMessage(element, text, links) 42 function insertMessage(element, text, links)
65 { 43 {
66 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text); 44 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text);
67 if (!match) 45 if (!match)
68 { 46 {
69 element.appendChild(document.createTextNode(text)); 47 element.appendChild(document.createTextNode(text));
70 return; 48 return;
71 } 49 }
72 50
73 let before = match[1]; 51 let before = match[1];
74 let tagName = match[2]; 52 let tagName = match[2];
75 let value = match[3]; 53 let value = match[3];
76 let after = match[4]; 54 let after = match[4];
77 55
78 insertMessage(element, before, links); 56 insertMessage(element, before, links);
79 57
80 let newElement = document.createElement(tagName); 58 let newElement = document.createElement(tagName);
81 if (tagName === "a" && links && links.length) 59 if (tagName == "a" && links && links.length)
82 newElement.href = links.shift(); 60 newElement.href = links.shift();
83 insertMessage(newElement, value, links); 61 insertMessage(newElement, value, links);
84 element.appendChild(newElement); 62 element.appendChild(newElement);
85 63
86 insertMessage(element, after, links); 64 insertMessage(element, after, links);
87 } 65 }
88 66
89 window.addEventListener("load", () => 67 window.addEventListener("load", () =>
90 { 68 {
91 getActiveDisplayedNotification().then(notification => 69 chrome.runtime.sendMessage({
70 type: "notifications.get",
71 displayMethod: "popup"
72 }, notification =>
92 { 73 {
93 if (!notification) 74 if (!notification)
94 return; 75 return;
95 76
96 let titleElement = document.getElementById("notification-title"); 77 let titleElement = document.getElementById("notification-title");
97 let messageElement = document.getElementById("notification-message"); 78 let messageElement = document.getElementById("notification-message");
98 79
99 ext.backgroundPage.sendMessage({ 80 titleElement.textContent = notification.texts.title;
100 type: "notifications.getLocalizedTexts", 81
101 notification 82 getDocLinks(notification).then(docLinks =>
Manish Jethani 2017/10/06 14:26:04 I'm not sure how message.locale gets in there. Is
kzar 2017/10/06 20:05:34 If not specified we go with the default I think.
102 }, texts =>
103 { 83 {
104 titleElement.textContent = texts.title; 84 insertMessage(messageElement, notification.texts.message, docLinks);
105 85
106 getDocLinks(notification).then(docLinks => 86 messageElement.addEventListener("click", event =>
107 { 87 {
108 insertMessage(messageElement, texts.message, docLinks); 88 let link = event.target;
89 while (link && link != messageElement && link.localName != "a")
90 link = link.parentNode;
91 if (!link)
92 return;
93 event.preventDefault();
94 event.stopPropagation();
95 chrome.tabs.create({url: link.href});
109 }); 96 });
110 });
111
112 messageElement.addEventListener("click", event =>
Manish Jethani 2017/10/06 14:26:04 I feel like this should be added in the getDocLink
kzar 2017/10/06 20:05:34 Done.
113 {
114 let link = event.target;
115 while (link && link !== messageElement && link.localName !== "a")
116 link = link.parentNode;
117 if (!link)
118 return;
119 event.preventDefault();
120 event.stopPropagation();
121 ext.pages.open(link.href);
122 }); 97 });
123 98
124 let notificationElement = document.getElementById("notification"); 99 let notificationElement = document.getElementById("notification");
125 notificationElement.className = notification.type; 100 notificationElement.className = notification.type;
126 notificationElement.hidden = false; 101 notificationElement.hidden = false;
127 notificationElement.addEventListener("click", event => 102 notificationElement.addEventListener("click", event =>
128 { 103 {
129 if (event.target.id == "notification-close") 104 if (event.target.id == "notification-close")
130 notificationElement.classList.add("closing"); 105 notificationElement.classList.add("closing");
131 else if (event.target.id == "notification-optout" || 106 else if (event.target.id == "notification-optout" ||
132 event.target.id == "notification-hide") 107 event.target.id == "notification-hide")
133 { 108 {
134 if (event.target.id == "notification-optout") 109 if (event.target.id == "notification-optout")
135 { 110 setPref("notifications_ignoredcategories", true);
136 ext.backgroundPage.sendMessage({
Manish Jethani 2017/10/06 14:26:04 You meant to use the togglePref function here, def
kzar 2017/10/06 20:05:34 Done.
137 type: "notifications_ignoredcategories"
138 });
139 }
140 111
141 notificationElement.hidden = true; 112 notificationElement.hidden = true;
142 notification.onClicked(); 113 notification.onClicked();
143 } 114 }
144 }, true); 115 }, true);
145 }); 116 });
146 }, false); 117 }, false);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld