chrome_point_plus/chrome_point_plus/js/background.js

72 lines
2.7 KiB
JavaScript
Raw Normal View History

// Message listener
chrome.extension.onMessage.addListener(function(message, sender) {
console.log('Received message: %O', message);
if (message) {
switch (message.type) {
case 'showPageAction':
var tab = sender.tab;
chrome.pageAction.show(tab.id);
break;
case 'showNotification':
2014-11-24 14:57:34 +00:00
chrome.notifications.create(
message.notificationId, {
type: 'basic',
iconUrl: message.avatarUrl,
title: message.title,
message: message.text,
2014-11-24 14:57:34 +00:00
priority: 0,
isClickable: true
},
function() { /* Error checking goes here */}
2014-11-24 14:57:34 +00:00
);
console.log('Showing notification %s', message.notificationId);
break;
case 'listenNotificationClicks':
// Adding notification click event listener
chrome.notifications.onClicked.addListener(function(notificationId) {
// Detecting notification type
if (notificationId.indexOf('comment_') === 0) {
tab_url = message.protocol + '//' + 'point.im/' + notificationId.replace(/comment_/g, '');
} else if (notificationId.indexOf('post_') === 0) {
tab_url = message.protocol + '//' + 'point.im/' + notificationId.replace(/post_/g, '');
}
console.log('Notification %s clicked! Opening new tab: %s', notificationId, tab_url);
if (tab_url !== undefined) {
chrome.tabs.create({
url: tab_url
});
}
});
break;
case 'injectJSFile':
console.log('Injecting JS: %s', message.file);
chrome.tabs.executeScript(null, {
file: message.file
//,runAt: 'document_end'
});
break;
case 'injectCSSFile':
console.log('Injecting CSS: %s', message.file);
chrome.tabs.insertCSS(null, {
file: message.file
});
break;
case 'injectCSSCode':
if (message.code !== undefined) {
chrome.tabs.insertCSS(null, {
code: message.code
});
}
break;
}
}
});