Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
psawaya committed Jun 24, 2011
0 parents commit 8f6302e
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is Chrome Notifications in Firefox via Jetpack. Learn more about what's being implemented at http://www.html5rocks.com/en/tutorials/notifications/quick/
20 changes: 20 additions & 0 deletions data/auth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<style>
body {
background-color:#ffffff;
color:#000000;
}
</style>
</head>
<body>
<div>
This site would like to show you desktop notifications. How do you feel about that?
</div>

<div>
<input type="submit" value="Yes" id="yesButton"/>
<input type="submit" value="No" id="noButton"/>
</div>
</body>
</html>
12 changes: 12 additions & 0 deletions data/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function userChoice(authed) {
self.postMessage({
authDecision: authed,
hostname: window.location.host
});
}

function init() {
document.getElementById('yesButton').addEventListener('click', function(e) { userChoice(true); },false);
document.getElementById('noButton').addEventListener('click', function(e) { userChoice(false); },false);
}
init();
57 changes: 57 additions & 0 deletions data/notifsPageMod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
window.notifications = {
createNotification: function createNotification(iconURL,title,text) {
var notifObj = {
show: function show() {
self.postMessage({ type: 'regular', args: {
iconURL: iconURL,
title: title,
text: text
}
});
}
};
return notifObj;
},
createHTMLNotification: function createHTMLNotification(url) {
self.postMessage({ type: 'html', args: arguments });
},
checkPermission: function checkPermission() {
// TODO: ask permission.

// self.postMessage({ type: 'permission', args: {
//
// } });

return window.notifications.permissionAllowed;
// return 0; // 0 == PERMISSION_ALLOWED
},
permissionAllowed: 1, // Real copy of permissions is kept in main.js, this is just used for checkPermission()
permissionCallback: function emptyPermissionCallback() {},
requestPermission: function requestPermission(callback) {
// TODO: ask permission.
self.postMessage({
type:'permission',
args: {
}
});

this.permissionCallback = callback;

return true;
}
};

self.port.on('authDecision', function (msg) {
if (msg['type'] != 'authresult') return;

window.notifications.permissionAllowed = msg['value'] ? 0 : 1;

// TODO: PERMISSION_DENIED
if (msg['value'])
window.notifications.permissionCallback(0); // PERMISSION_ALLOWED
else
window.notifications.permissionCallback(1); // PERMISSION_NOT_ALLOWED
});

window.webkitNotifications = window.notifications;
window.mozillaNotifications = window.notifications;
2 changes: 2 additions & 0 deletions doc/main.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The main module is a program that creates a widget. When a user clicks on
the widget, the program loads the mozilla.org website in a new tab.
92 changes: 92 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// HTML5 Desktop Notifications

const dataDir = require("self").data;
const notifications = require("notifications");
const pageMod = require("page-mod");
const panel = require("panel");
const tabBrowser = require("tab-browser");
const url = require("url");


// Dictionary of hostnames that are allowed to show notifications.
var SITES_ALLOWED = {};

// Remaining to be done:
// * HTML Notifications
// * Persist SITES_ALLOWED between sessions (simple storage?) so that users don't have to auth every time
// * After notification callback
// * Ability to remove site's notification privileges

function createNotificationsHook() {
pageMod.PageMod({
include: "*",
contentScriptFile: dataDir.url('notifsPageMod.js'),
onAttach: function(worker,mod) {
worker.on('message',function(msg) {
if (msg['type'] == 'regular') {
console.log("Can auth: " + this.canAuth());
if (!this.canAuth) return false;
var msgArgs = msg.args;
var notifObj = {
title: msgArgs['title'] ? msgArgs['title'] : 'Notification',
text: msgArgs['text'] ? msgArgs['text'] : '',
iconURL: msgArgs['iconURL'] ? msgArgs['iconURL'] : undefined,
data: 'data string.',
onClick: function (data) {
console.log(data);
}
};
notifications.notify(notifObj);
}
else if (msg['type'] == 'permission') {
console.log(msg['args'].win);
var currentWorker = worker;
askNotificationsPermission(function callback(authResult) {
worker.port.emit('authDecision', {
type: 'authresult',
value: authResult
});

if (authResult) {
// If the user accepts notifications, authorize entire hostname.
var workerHostname = getHostnameFromURL(worker.tab.url);
SITES_ALLOWED[workerHostname] = true;
}
});
}
});
worker.canAuth = function canAuth() {
return SITES_ALLOWED[getHostnameFromURL(this.tab.url)];
}
}
});
}
createNotificationsHook();

function getHostnameFromURL(_url) {
return url.URL(_url).host;
}

function askNotificationsPermission(callback) {
var canHidePanel = true;

var authPanel = panel.Panel({
contentURL: dataDir.url('auth.html'),
contentScriptFile: dataDir.url('auth.js'),
contentScriptWhen: 'ready',
onMessage: function(msg) {
canHidePanel = true;
this.hide();
callback(msg['authDecision']);
},
onHide: function() {
// Cheesy way of making sure the panel doesn't disappear when you click off.
if (!canHidePanel)
this.show();
},
});

authPanel.show();
canHidePanel = false;

}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "chrome-notifications-jetpack",
"license": "MPL 1.1/GPL 2.0/LGPL 2.1",
"author": "",
"version": "0.1",
"fullName": "Chrome Notifications Jetpack",
"id": "jid1-KQQ9iBe5nw7jdA",
"description": "Chrome (Gmail/GTalk) desktop notifications in Firefox."
}
32 changes: 32 additions & 0 deletions test/test-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const main = require("main");

exports.test_test_run = function(test) {
test.pass("Unit test running!");
};

exports.test_id = function(test) {
test.assert(require("self").id.length > 0);
};

exports.test_url = function(test) {
require("request").Request({
url: "http://www.mozilla.org/",
onComplete: function(response) {
test.assertEqual(response.statusText, "OK");
test.done();
}
}).get();
test.waitUntilDone(20000);
};

exports.test_open_tab = function(test) {
const tabs = require("tabs");
tabs.open({
url: "http://www.mozilla.org/",
onReady: function(tab) {
test.assertEqual(tab.url, "http://www.mozilla.org/");
test.done();
}
});
test.waitUntilDone(20000);
};

0 comments on commit 8f6302e

Please sign in to comment.