Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added desktop notifications to Chrome extension #7

Merged
merged 9 commits into from
Mar 9, 2015
10 changes: 8 additions & 2 deletions MarmoUI-Chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
"content_scripts": [
{
"matches": ["https://marmoset.student.cs.uwaterloo.ca/*"],
"js": ["script.js"]
"js": ["scripts/script.js"]
}
]
],

"background": {
"persistent": false,
"scripts": ["scripts/notifications.js"]
},
"permissions": ["notifications"]
}
82 changes: 82 additions & 0 deletions MarmoUI-Chrome/scripts/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Notifications which have not been closed yet
var activeNotifications = {};

chrome.runtime.onMessage.addListener(function(request, sender) {

if (request.type == "notification") {
// Check if we have permission
chrome.notifications.getPermissionLevel(function(level) {
if(level == "denied") {
console.warn("Notification suppressed; Permission denied.")
return;
}

chrome.windows.get(sender.tab.windowId, function(win) {
// Don't show notification if user is still on marmoset
if (win.focused && sender.tab.active) return;

chrome.notifications.create('', request.notification,
function(notificationId) {
// Add URL to activeNotifications so we can access it
// on button click
request.sender = sender;
activeNotifications[notificationId] = request;
});
});
});
}
});

// Button handler for "View Results"
function openResults(notificationId, buttonIndex) {

// Open a new tab with the results
chrome.tabs.create(activeNotifications[notificationId].options, function(tab) {

// Focus new tab's window
chrome.windows.update(tab.windowId, {focused: true}, function (){ });
});

// Close notification
chrome.notifications.clear(notificationId, function() {});
}

function notificationDeactivated(notificationId) {
delete activeNotifications[notificationId];
}

function focusMarmoUI(notificationId) {
var windowId = activeNotifications[notificationId].sender.tab.windowId;
var tabId = activeNotifications[notificationId].sender.tab.id;
var pageUrl = activeNotifications[notificationId].sender.url;

//check if window still exists
chrome.windows.get(windowId, function() {
if(!chrome.runtime.lastError) {
// focus
chrome.windows.update(windowId, {focused: true}, function (){});
}
});

// check if tab still exists
chrome.tabs.get(tabId, function() {
if(!chrome.runtime.lastError) {
// set active
chrome.tabs.update(tabId, {active: true}, function () {});
} else {
// otherwise open results at previous url
chrome.tabs.create({url: pageUrl}, function(tab) {
// Focus new tab's window
chrome.windows.update(tab.windowId, {focused: true}, function (){ });
});
}
});

// Close notification
chrome.notifications.clear(notificationId, function() {});
}

// Registering listeners
chrome.notifications.onButtonClicked.addListener(openResults)
chrome.notifications.onClosed.addListener(notificationDeactivated);
chrome.notifications.onClicked.addListener(focusMarmoUI);
57 changes: 55 additions & 2 deletions MarmoUI-Chrome/script.js → MarmoUI-Chrome/scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,23 @@ function runMarmoUI()

function applyChangesProblemsList()
{

//After testing, loadSubmission and notify
function loadSubmissionAfterAsyncReload(tableCell, requestResult, requestURL) {
loadSubmission(tableCell, requestResult, requestURL);

// If finished testing, display notification
if(tableCell.find("a:contains('Not tested')").length == 0) {
var $a = tableCell.find("a");

notifyTestingComplete({
test: tableCell.parent().children().first().text().trim(),
result: $a.text().trim(),
url: 'https://marmoset.student.cs.uwaterloo.ca' + $a.attr('href')
});
}
}

//When async callbacks, decrypt the result from the page and integrate into the page
function loadSubmission(tableCell, requestResult, requestURL)
{
Expand All @@ -550,7 +567,7 @@ function runMarmoUI()
if(firstLine.find("td:contains('tested yet')").length > 0)
{
tableCell.find("a").html("Not tested (reload in <span class='update'></span> s)");
queueAsyncReload(tableCell, requestURL, loadSubmission, reload_time);
queueAsyncReload(tableCell, requestURL, loadSubmissionAfterAsyncReload, reload_time);
}
//Check if latest solution failed to compile
//If failed to compile, show it as uncompiled and exits
Expand Down Expand Up @@ -725,6 +742,9 @@ function runMarmoUI()
}
else
{
notifyTestingComplete({
test: $(".nav").children().last().text()
})
//No more untested solutions, reload to see results
document.location.reload(true);
}
Expand Down Expand Up @@ -859,8 +879,13 @@ function runMarmoUI()
});
}

//Start of actual executing code
// Creates desktop notification
function notifyTestingComplete(notification) {
notification.type = "resultsNotification";
window.postMessage(notification, "*");
}

//Start of actual executing code
//Find out which page we're on
var path = $(location).attr("href");
//Check which page we're on
Expand Down Expand Up @@ -940,4 +965,32 @@ function runMarmoUI()
}
}

// Listener to handle desktop notifications
// (notification goes from page context -> content-script -> background page (notifications.js))
window.addEventListener("message", function(event) {
// We only accept messages from ourselves
if (event.source != window)
return;

if (event.data.type && (event.data.type == "resultsNotification")) {
var toSend = {type: "notification",
notification: {
type: "basic",
iconUrl: chrome.extension.getURL("image/icon128.png"),
title: event.data.test + " Testing Complete",
message: (event.data.result ? "Result: " + event.data.result : "")
},
options: {}
};

// Notification has "View results" button
if(event.data.url) {
toSend.notification.buttons = [{title: "View results"}];
toSend.options.url = event.data.url
}
chrome.runtime.sendMessage(toSend);
}
}, false);


loadMarmoUI(runMarmoUI);