Skip to content

Instantly share code, notes, and snippets.

@kzar
Last active December 2, 2019 12:17
Show Gist options
  • Save kzar/91b27aea09801b44bfc9 to your computer and use it in GitHub Desktop.
Save kzar/91b27aea09801b44bfc9 to your computer and use it in GitHub Desktop.
Testing Adblock Plus for Chrome notifications
/**
* 1. Append one of these blocks to adblockpluschrome/lib/devtools.js...
*/
// Desktop notification, relentless type, no links
require("notifications").notifications.addNotification({
id: (new Date() | 0).toString(),
type: "relentless",
title: "Notification title",
message: "Notification message"
});
window.notifications = require("notifications");
// Desktop + popup window notification, critical type, three links
require("notifications").notifications.addNotification({
id: (new Date() | 0).toString(),
type: "critical",
title: "Notification title",
message: "<a>contribute</a> <a>day1.html</a> <a>firefox_support</a>",
links: ["contribute", "abp:day1", "firefox_support"]
});
window.notifications = require("notifications");
// Popup window notification with icon animation, information type, one link
require("notifications").notifications.addNotification({
id: (new Date() | 0).toString(),
type: "information",
title: "Notification title",
message: "<a>Open contribution page</a>",
links: ["contribute"],
});
window.notifications = require("notifications");
/**
* 2. Type this in the background console to trigger the notification.
*/
notifications.notifications.showNext();
/**
* 1. Open the options page, then open the console.
* 2. Paste this block of code:
*/
function getNotificationUrl()
{
browser.runtime.sendMessage({
type: "prefs.get", key: "notificationurl"
}).then(console.log);
}
function setNotificationUrl(url)
{
browser.runtime.sendMessage({
type: "prefs.set", key: "notificationurl",
value: url.toString()
});
}
function getNotificationData()
{
browser.runtime.sendMessage({
type: "prefs.get", key: "notificationdata"
}).then(d => { console.log(JSON.stringify(d.data, null, 2)); });
}
function setNotificationData(data)
{
const millisecondsInOneDay = 1000 * 60 * 60 * 24;
let now = new Date();
browser.runtime.sendMessage({
type: "prefs.set", key: "notificationdata",
value: {
firstVersion: data.version,
lastCheck: now * 1,
softExpiration: now * 1 + millisecondsInOneDay,
hardExpiration: now * 1 + millisecondsInOneDay * 2,
data,
"lastError": 0,
"downloadStatus": "synchronize_ok",
"downloadCount": 1
}
});
}
function getRawNotificationData()
{
browser.runtime.sendMessage({
type: "prefs.get", key: "notificationdata"
}).then(d => { console.log(JSON.stringify(d, null, 2)); });
}
function getNotificationTimes()
{
browser.runtime.sendMessage({
type: "prefs.get", key: "notificationdata"
}).then(notificationdata =>
{
let formatDate = d => new Date(d).toUTCString();
if ("softExpiration" in notificationdata)
console.log("Soft expiration:", formatDate(notificationdata.softExpiration));
if ("hardExpiration" in notificationdata)
console.log("Hard expiration", formatDate(notificationdata.softExpiration));
if (notificationdata.shown)
{
console.log("Recently shown notifications:");
for (let key in notificationdata.shown)
{
if (notificationdata.shown.hasOwnProperty(key))
{
console.log(
" - ", JSON.stringify(key), ": ",
formatDate(notificationdata.shown[key])
);
}
}
}
else
console.log("No notifications have been shown recently.");
});
}
function expireNotificationData()
{
browser.runtime.sendMessage({
type: "prefs.get", key: "notificationdata"
}).then(notificationdata =>
{
if (notificationdata.lastCheck)
{
notificationdata.softExpiration = notificationdata.hardExpiration
= notificationdata.lastCheck - 1;
browser.runtime.sendMessage({
type: "prefs.set", key: "notificationdata",
value: notificationdata
});
}
});
}
/**
* 3. Type these commands to get and set the notification data/URL:
*/
// View or change the URL which is used when fetching the notification data.
getNotificationUrl();
setNotificationUrl("htttps://example.com/notification.json");
// View or change the notification data directly.
// (Changing the notificaiton data also resets expiraton times.)
getNotificationData();
setNotificationData({
"notifications": [{
"id": "critical_notification_id",
"type": "critical",
"links": [
"contribute"
],
"title": {
"en-US": "Test CRITICAL Notification",
"de": "DE Test CRITICAL Notification"
},
"message": {
"en-US": "This is a CRITICAL notification.",
"de": "DE This is a CRITICAL notification."
}
}],
"version": "201810011560"
});
// See also https://gitlab.com/rossg-eyeo/testpages.adblockplus.org/tree/manualtesting/static/notifications
// Display the soft+hard expiration times for the notification data, also the times any notifications were displayed.
getNotificationTimes();
// Set the notification data to expire (set soft+hard expiration times to the past).
expireNotificationData();
// View the raw notification data (including the timing information)
getRawNotificationData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment