2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-21 02:21:07 +00:00
|
|
|
*/
|
2014-11-09 10:17:26 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2014-11-16 21:19:51 +00:00
|
|
|
// Browser specific functions for Chrom*
|
2014-11-09 10:17:26 +00:00
|
|
|
window.extension = window.extension || {};
|
2014-05-21 02:21:07 +00:00
|
|
|
|
2014-11-09 10:17:26 +00:00
|
|
|
window.extension.navigator = (function () {
|
|
|
|
var self = {},
|
|
|
|
tabs = {};
|
|
|
|
tabs.create = function (url) {
|
2015-05-12 21:01:45 +00:00
|
|
|
if (chrome.tabs) {
|
|
|
|
chrome.tabs.create({url: url});
|
|
|
|
} else {
|
|
|
|
extension.windows.open({url: url});
|
|
|
|
}
|
2014-11-09 10:17:26 +00:00
|
|
|
};
|
|
|
|
self.tabs = tabs;
|
2014-05-27 21:12:14 +00:00
|
|
|
|
2014-11-09 10:17:26 +00:00
|
|
|
self.setBadgeText = function (text) {
|
2015-05-15 19:09:50 +00:00
|
|
|
if (chrome.browserAction && chrome.browserAction.setBadgeText) {
|
2015-05-12 21:01:45 +00:00
|
|
|
chrome.browserAction.setBadgeText({text: String(text)});
|
|
|
|
}
|
2014-11-09 10:17:26 +00:00
|
|
|
};
|
2014-05-27 21:12:14 +00:00
|
|
|
|
2014-11-09 10:17:26 +00:00
|
|
|
return self;
|
|
|
|
}());
|
2014-05-21 02:21:07 +00:00
|
|
|
|
2014-11-16 21:19:51 +00:00
|
|
|
window.extension.trigger = function (name, object) {
|
|
|
|
chrome.runtime.sendMessage(null, { name: name, data: object });
|
|
|
|
};
|
|
|
|
|
2014-12-19 21:59:18 +00:00
|
|
|
window.extension.on = function (name, callback) {
|
|
|
|
// this causes every listener to fire on every message.
|
|
|
|
// if we eventually end up with lots of listeners (lol)
|
|
|
|
// might be worth making a map of 'name' -> [callbacks, ...]
|
|
|
|
// so we can fire a single listener that calls only the necessary
|
|
|
|
// calllbacks for that message name
|
2014-11-16 21:19:51 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function(e) {
|
|
|
|
if (e.name === name) {
|
|
|
|
callback(e.data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-05-21 02:21:07 +00:00
|
|
|
|
2015-01-22 08:21:37 +00:00
|
|
|
extension.windows = {
|
|
|
|
open: function(options, callback) {
|
2015-05-12 21:01:45 +00:00
|
|
|
if (chrome.windows) {
|
|
|
|
chrome.windows.create(options, callback);
|
|
|
|
} else if (chrome.app.window) {
|
|
|
|
var url = options.url;
|
|
|
|
delete options.url;
|
|
|
|
chrome.app.window.create(url, options, callback);
|
|
|
|
}
|
2015-01-22 08:21:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
focus: function(id, callback) {
|
2015-05-12 21:01:45 +00:00
|
|
|
if (chrome.windows) {
|
|
|
|
chrome.windows.update(id, { focused: true }, function() {
|
|
|
|
callback(chrome.runtime.lastError);
|
|
|
|
});
|
|
|
|
} else if (chrome.app.window) {
|
|
|
|
var appWindow = chrome.app.window.get(id);
|
|
|
|
if (appWindow) {
|
2015-05-22 19:36:14 +00:00
|
|
|
appWindow.show();
|
2015-05-12 21:01:45 +00:00
|
|
|
appWindow.focus();
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
callback('No window found for id ' + id);
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 08:21:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getCurrent: function(callback) {
|
2015-05-12 21:01:45 +00:00
|
|
|
if (chrome.windows) {
|
|
|
|
chrome.windows.getCurrent(callback);
|
|
|
|
} else if (chrome.app.window) {
|
|
|
|
callback(chrome.app.window.current());
|
|
|
|
}
|
2015-01-22 08:21:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: function(windowId) {
|
2015-05-12 21:01:45 +00:00
|
|
|
if (chrome.windows) {
|
|
|
|
chrome.windows.remove(windowId);
|
|
|
|
} else if (chrome.app.window) {
|
|
|
|
chrome.app.window.get(windowId).close();
|
|
|
|
}
|
2015-01-22 08:21:37 +00:00
|
|
|
},
|
|
|
|
|
2015-05-11 21:22:15 +00:00
|
|
|
getBackground: function(callback) {
|
2015-05-14 20:44:46 +00:00
|
|
|
var getBackground;
|
2015-05-11 21:22:15 +00:00
|
|
|
if (chrome.extension) {
|
2015-05-14 20:44:46 +00:00
|
|
|
var bg = chrome.extension.getBackgroundPage();
|
|
|
|
bg.storage.onready(function() {
|
|
|
|
callback(bg);
|
2015-05-11 21:22:15 +00:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} else if (chrome.runtime) {
|
2015-05-14 20:44:46 +00:00
|
|
|
chrome.runtime.getBackgroundPage(function(bg) {
|
|
|
|
bg.storage.onready(function() {
|
|
|
|
callback(bg);
|
|
|
|
});
|
2015-05-11 21:22:15 +00:00
|
|
|
});
|
|
|
|
}
|
2015-03-09 20:20:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getViews: function() {
|
2015-05-12 21:01:45 +00:00
|
|
|
if (chrome.extension) {
|
|
|
|
return chrome.extension.getViews();
|
|
|
|
} else if (chrome.app.window) {
|
|
|
|
return chrome.app.window.getAll().map(function(appWindow) {
|
|
|
|
return appWindow.contentWindow;
|
|
|
|
});
|
|
|
|
}
|
2015-05-13 18:23:59 +00:00
|
|
|
},
|
|
|
|
|
2015-11-08 18:40:37 +00:00
|
|
|
onSuspend: function(callback) {
|
2015-05-13 18:23:59 +00:00
|
|
|
if (chrome.runtime) {
|
|
|
|
chrome.runtime.onSuspend.addListener(callback);
|
|
|
|
} else {
|
|
|
|
window.addEventListener('beforeunload', callback);
|
|
|
|
}
|
2015-05-20 22:50:03 +00:00
|
|
|
},
|
2015-11-07 00:47:30 +00:00
|
|
|
onClosed: function(callback) {
|
|
|
|
// assumes only one front end window
|
2015-11-08 08:15:14 +00:00
|
|
|
if (window.chrome && chrome.app && chrome.app.window) {
|
|
|
|
return chrome.app.window.getAll()[0].onClosed.addListener(callback);
|
|
|
|
} else {
|
|
|
|
window.addEventListener('beforeunload', callback);
|
|
|
|
}
|
2015-11-07 00:47:30 +00:00
|
|
|
},
|
2015-05-20 22:50:03 +00:00
|
|
|
|
|
|
|
drawAttention: function(window_id) {
|
2016-02-23 20:41:21 +00:00
|
|
|
console.log('draw attention');
|
2015-05-20 22:50:03 +00:00
|
|
|
if (chrome.app.window) {
|
2015-05-24 23:19:33 +00:00
|
|
|
var w = chrome.app.window.get(window_id);
|
2016-03-10 19:00:53 +00:00
|
|
|
if (w) {
|
|
|
|
w.clearAttention();
|
|
|
|
w.drawAttention();
|
|
|
|
}
|
2015-05-20 22:50:03 +00:00
|
|
|
}
|
2015-12-04 01:13:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
clearAttention: function(window_id) {
|
2016-02-23 20:41:21 +00:00
|
|
|
console.log('clear attention');
|
2015-12-04 01:13:38 +00:00
|
|
|
if (chrome.app.window) {
|
|
|
|
var w = chrome.app.window.get(window_id);
|
2016-03-10 19:00:53 +00:00
|
|
|
if (w) {
|
|
|
|
w.clearAttention();
|
|
|
|
}
|
2015-12-04 01:13:38 +00:00
|
|
|
}
|
2015-01-22 08:21:37 +00:00
|
|
|
}
|
2015-05-12 21:01:45 +00:00
|
|
|
|
2015-01-22 08:21:37 +00:00
|
|
|
};
|
|
|
|
|
2015-05-12 21:01:45 +00:00
|
|
|
extension.onLaunched = function(callback) {
|
2015-05-15 19:09:50 +00:00
|
|
|
if (chrome.browserAction && chrome.browserAction.onClicked) {
|
2015-05-12 21:01:45 +00:00
|
|
|
chrome.browserAction.onClicked.addListener(callback);
|
|
|
|
}
|
|
|
|
if (chrome.app && chrome.app.runtime) {
|
|
|
|
chrome.app.runtime.onLaunched.addListener(callback);
|
|
|
|
}
|
2015-01-22 08:21:37 +00:00
|
|
|
};
|
|
|
|
|
2015-12-14 23:10:49 +00:00
|
|
|
// Translate
|
2016-01-17 10:54:34 +00:00
|
|
|
window.i18n = function(message, substitutions) {
|
2016-03-23 19:57:29 +00:00
|
|
|
if (window.chrome && chrome.i18n) {
|
|
|
|
return chrome.i18n.getMessage(message, substitutions);
|
|
|
|
}
|
2015-12-14 23:10:49 +00:00
|
|
|
};
|
|
|
|
|
2014-11-13 23:38:09 +00:00
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.registration = {
|
|
|
|
done: function () {
|
2016-01-12 00:08:54 +00:00
|
|
|
storage.put("chromiumRegistrationDoneEver", "");
|
2015-05-12 22:14:20 +00:00
|
|
|
storage.put("chromiumRegistrationDone", "");
|
2014-12-19 21:59:18 +00:00
|
|
|
extension.trigger('registration_done');
|
2014-11-13 23:38:09 +00:00
|
|
|
},
|
|
|
|
isDone: function () {
|
2015-05-12 22:14:20 +00:00
|
|
|
return storage.get("chromiumRegistrationDone") === "";
|
2014-11-13 23:38:09 +00:00
|
|
|
},
|
2016-01-12 00:08:54 +00:00
|
|
|
everDone: function() {
|
2016-01-16 16:53:02 +00:00
|
|
|
return storage.get("chromiumRegistrationDoneEver") === "" || storage.get("chromiumRegistrationDone") === "";
|
2016-01-12 00:08:54 +00:00
|
|
|
},
|
|
|
|
remove: function() {
|
|
|
|
storage.remove("chromiumRegistrationDone");
|
|
|
|
},
|
2014-11-13 23:38:09 +00:00
|
|
|
};
|
2015-05-11 21:22:15 +00:00
|
|
|
|
2015-05-14 21:50:52 +00:00
|
|
|
extension.install = function(mode) {
|
|
|
|
var id = 'installer';
|
|
|
|
var url = 'options.html';
|
|
|
|
if (mode === 'standalone') {
|
|
|
|
id = 'standalone-installer';
|
|
|
|
url = 'register.html';
|
|
|
|
}
|
2015-08-24 16:10:48 +00:00
|
|
|
if (!chrome.app.window.get(id)) {
|
|
|
|
extension.windows.open({
|
|
|
|
id: id,
|
|
|
|
url: url,
|
2015-11-03 03:19:30 +00:00
|
|
|
bounds: { width: 800, height: 666, },
|
|
|
|
minWidth: 800,
|
|
|
|
minHeight: 666
|
2015-08-24 16:10:48 +00:00
|
|
|
});
|
|
|
|
}
|
2015-05-12 21:01:45 +00:00
|
|
|
};
|
|
|
|
|
2015-12-01 21:52:38 +00:00
|
|
|
var notification_pending = Promise.resolve();
|
2015-11-25 23:11:01 +00:00
|
|
|
extension.notification = {
|
|
|
|
clear: function() {
|
2015-12-01 21:52:38 +00:00
|
|
|
notification_pending = notification_pending.then(function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
chrome.notifications.clear('signal', resolve);
|
|
|
|
});
|
|
|
|
});
|
2015-11-25 23:11:01 +00:00
|
|
|
},
|
|
|
|
update: function(options) {
|
|
|
|
if (chrome) {
|
|
|
|
var chromeOpts = {
|
|
|
|
type : options.type,
|
|
|
|
title : options.title,
|
|
|
|
message : options.message || '', // required
|
|
|
|
iconUrl : options.iconUrl,
|
|
|
|
imageUrl : options.imageUrl,
|
|
|
|
items : options.items,
|
|
|
|
buttons : options.buttons
|
|
|
|
};
|
2015-12-01 21:52:38 +00:00
|
|
|
notification_pending = notification_pending.then(function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
chrome.notifications.update('signal', chromeOpts, function(wasUpdated) {
|
|
|
|
if (!wasUpdated) {
|
|
|
|
chrome.notifications.create('signal', chromeOpts, resolve);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-11-25 23:11:01 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var notification = new Notification(options.title, {
|
|
|
|
body : options.message,
|
|
|
|
icon : options.iconUrl,
|
|
|
|
tag : 'signal'
|
|
|
|
});
|
|
|
|
notification.onclick = function() {
|
|
|
|
Whisper.Notifications.onclick();
|
|
|
|
};
|
|
|
|
}
|
2015-09-14 03:25:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-16 22:59:41 +00:00
|
|
|
extension.keepAwake = function() {
|
|
|
|
if (chrome && chrome.alarms) {
|
|
|
|
chrome.alarms.onAlarm.addListener(function() {
|
|
|
|
// nothing to do.
|
|
|
|
});
|
|
|
|
chrome.alarms.create('awake', {periodInMinutes: 1});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-14 22:44:43 +00:00
|
|
|
if (chrome.runtime.onInstalled) {
|
2015-05-12 21:01:45 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(function(options) {
|
|
|
|
if (options.reason === 'install') {
|
|
|
|
extension.install();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-11-09 10:17:26 +00:00
|
|
|
}());
|