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

Unified Diff: notification.js

Issue 29567749: Issue 5593 - Use messaging for the popup's notification code (Closed)
Patch Set: Rebased Created Oct. 9, 2017, 4:47 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 | « dependencies ('k') | popup.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: notification.js
diff --git a/notification.js b/notification.js
index ae189d03e2b4530eaa994778932b961b9e1a88e8..1015e00f7ff9ddd6c5b2a8492f54ebfc43fc7be2 100644
--- a/notification.js
+++ b/notification.js
@@ -15,25 +15,28 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-"use strict";
-
-const {require} = ext.backgroundPage.getWindow();
+/* global setPref */
-const {Utils} = require("utils");
-const {Notification} = require("notification");
-const {getActiveNotification, shouldDisplay} = require("notificationHelper");
+"use strict";
function getDocLinks(notification)
{
if (!notification.links)
- return [];
+ return Promise.resolve([]);
- let docLinks = [];
- notification.links.forEach(link =>
- {
- docLinks.push(Utils.getDocLink(link));
- });
- return docLinks;
+ return Promise.all(
+ notification.links.map(link =>
+ {
+ return new Promise((resolve, reject) =>
+ {
+ chrome.runtime.sendMessage({
+ type: "app.get",
+ what: "doclink",
+ link
+ }, resolve);
+ });
+ })
+ );
}
function insertMessage(element, text, links)
@@ -53,7 +56,7 @@ function insertMessage(element, text, links)
insertMessage(element, before, links);
let newElement = document.createElement(tagName);
- if (tagName === "a" && links && links.length)
+ if (tagName == "a" && links && links.length)
newElement.href = links.shift();
insertMessage(newElement, value, links);
element.appendChild(newElement);
@@ -63,45 +66,52 @@ function insertMessage(element, text, links)
window.addEventListener("load", () =>
{
- let notification = getActiveNotification();
- if (!notification || !shouldDisplay("popup", notification.type))
- return;
-
- let texts = Notification.getLocalizedTexts(notification);
- let titleElement = document.getElementById("notification-title");
- titleElement.textContent = texts.title;
-
- let docLinks = getDocLinks(notification);
- let messageElement = document.getElementById("notification-message");
- insertMessage(messageElement, texts.message, docLinks);
-
- messageElement.addEventListener("click", event =>
+ chrome.runtime.sendMessage({
+ type: "notifications.get",
+ displayMethod: "popup"
+ }, notification =>
{
- let link = event.target;
- while (link && link !== messageElement && link.localName !== "a")
- link = link.parentNode;
- if (!link)
+ if (!notification)
return;
- event.preventDefault();
- event.stopPropagation();
- chrome.tabs.create({url: link.href});
- });
- let notificationElement = document.getElementById("notification");
- notificationElement.className = notification.type;
- notificationElement.hidden = false;
- notificationElement.addEventListener("click", event =>
- {
- if (event.target.id == "notification-close")
- notificationElement.classList.add("closing");
- else if (event.target.id == "notification-optout" ||
- event.target.id == "notification-hide")
- {
- if (event.target.id == "notification-optout")
- Notification.toggleIgnoreCategory("*", true);
+ let titleElement = document.getElementById("notification-title");
+ let messageElement = document.getElementById("notification-message");
- notificationElement.hidden = true;
- notification.onClicked();
- }
- }, true);
+ titleElement.textContent = notification.texts.title;
+
+ getDocLinks(notification).then(docLinks =>
+ {
+ insertMessage(messageElement, notification.texts.message, docLinks);
+
+ messageElement.addEventListener("click", event =>
+ {
+ let link = event.target;
+ while (link && link != messageElement && link.localName != "a")
+ link = link.parentNode;
+ if (!link)
+ return;
+ event.preventDefault();
+ event.stopPropagation();
+ chrome.tabs.create({url: link.href});
+ });
+ });
+
+ let notificationElement = document.getElementById("notification");
+ notificationElement.className = notification.type;
+ notificationElement.hidden = false;
+ notificationElement.addEventListener("click", event =>
+ {
+ if (event.target.id == "notification-close")
+ notificationElement.classList.add("closing");
+ else if (event.target.id == "notification-optout" ||
+ event.target.id == "notification-hide")
+ {
+ if (event.target.id == "notification-optout")
+ setPref("notifications_ignoredcategories", true);
+
+ notificationElement.hidden = true;
+ notification.onClicked();
+ }
+ }, true);
+ });
}, false);
« no previous file with comments | « dependencies ('k') | popup.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld