2014-11-26 18:25:56 +00:00
|
|
|
// Maintaining options version
|
|
|
|
chrome.storage.sync.get('options_version', function(data) {
|
|
|
|
var pp_version = getVersion();
|
|
|
|
console.info('Point+ %s. Options are for %s.', pp_version, data.options_version);
|
|
|
|
|
|
|
|
if (data.options_version != pp_version) {
|
|
|
|
chrome.tabs.create({url: 'options.html'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-07 02:36:06 +00:00
|
|
|
// Crutches and bikes
|
|
|
|
/**
|
|
|
|
* Inject several JS files
|
|
|
|
* @param {number} tabId Unique ID of tab which requested injection
|
|
|
|
* @param {Object[]} files Array of objects of files to inject
|
2015-01-07 05:51:51 +00:00
|
|
|
* @param {function} onAllInjected allback function running when injection ends
|
2015-01-07 02:36:06 +00:00
|
|
|
*/
|
|
|
|
function injectJS(tabId, files, onAllInjected) {
|
|
|
|
var item = files.shift();
|
|
|
|
if (item) {
|
|
|
|
console.log('Injecting JS "%s" to the tab #%s', item.file, tabId);
|
|
|
|
|
|
|
|
if ('file' in item) {
|
|
|
|
chrome.tabs.executeScript(tabId ? tabId : null, {
|
|
|
|
file: item.file,
|
|
|
|
runAt: item.runAt || 'document_start'
|
|
|
|
}, function(result) {
|
|
|
|
console.info('"%s" injected to the tab #%s', item.file, tabId);
|
|
|
|
|
|
|
|
injectJS(tabId, files, onAllInjected);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onAllInjected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Implement injectCSS (because JS execution working always after CSS injection)
|
|
|
|
|
2014-04-21 20:42:53 +00:00
|
|
|
// Message listener
|
2015-01-07 02:36:06 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
|
|
|
|
// @todo Check if sender.tab may be undefined in some cases
|
2015-01-07 19:11:10 +00:00
|
|
|
console.log('Received message from tab #%s: %O', sender.tab ? sender.tab.id : 'undefined', message);
|
2014-04-04 23:57:26 +00:00
|
|
|
|
2014-04-21 20:42:53 +00:00
|
|
|
if (message) {
|
|
|
|
switch (message.type) {
|
|
|
|
case 'showPageAction':
|
2015-01-07 02:36:06 +00:00
|
|
|
chrome.pageAction.show(sender.tab.id);
|
|
|
|
sendResponse(true);
|
|
|
|
|
|
|
|
console.log('Showed pageAction for tab #%s', sender.tab.id);
|
2015-01-07 04:19:02 +00:00
|
|
|
|
|
|
|
// Fuck You, Chrome API documentation!!11
|
|
|
|
return true;
|
2014-04-21 20:42:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'showNotification':
|
2014-11-24 14:57:34 +00:00
|
|
|
chrome.notifications.create(
|
2014-04-21 20:42:53 +00:00
|
|
|
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
|
2015-01-07 02:36:06 +00:00
|
|
|
}, function(notificationId) {
|
|
|
|
console.info('Notification "%s" created', notificationId);
|
|
|
|
|
|
|
|
sendResponse(true);
|
|
|
|
}
|
2014-11-24 14:57:34 +00:00
|
|
|
);
|
|
|
|
|
2015-01-07 04:19:02 +00:00
|
|
|
// Fuck You, Chrome API documentation!!11
|
|
|
|
return true;
|
2014-11-24 14:57:34 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-07 19:11:10 +00:00
|
|
|
case 'getManifestVersion':
|
|
|
|
sendResponse({version: getVersion()});
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
2014-11-24 14:57:34 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-04-21 20:42:53 +00:00
|
|
|
|
2015-01-07 02:36:06 +00:00
|
|
|
sendResponse(true);
|
2015-01-07 04:19:02 +00:00
|
|
|
|
2015-01-07 04:43:17 +00:00
|
|
|
// Fuck You, Chrome API documentation!
|
2015-01-07 04:19:02 +00:00
|
|
|
return true;
|
2015-01-07 02:36:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated since 1.19.1
|
|
|
|
*/
|
2014-11-16 02:01:31 +00:00
|
|
|
case 'injectJSFile':
|
2015-01-07 02:36:06 +00:00
|
|
|
console.log('Executing JS: %s', message.file);
|
|
|
|
chrome.tabs.executeScript(sender.tab.id ? sender.tab.id : null, {
|
|
|
|
file: message.file,
|
|
|
|
runAt: message.runAt || 'document_start'
|
|
|
|
}, function() {
|
|
|
|
sendResponse(true);
|
|
|
|
|
|
|
|
console.info('JS file executed: "%s"', message.file);
|
2015-01-07 04:43:17 +00:00
|
|
|
return true;
|
2014-11-16 02:01:31 +00:00
|
|
|
});
|
2015-01-07 04:19:02 +00:00
|
|
|
|
2015-01-07 04:43:17 +00:00
|
|
|
// Fuck You, Chrome API documentation!
|
2015-01-07 04:19:02 +00:00
|
|
|
return true;
|
2014-04-21 20:42:53 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-07 02:36:06 +00:00
|
|
|
// Inject several files
|
|
|
|
case 'executeJSFiles':
|
|
|
|
//console.debug('Received JS file list: %O', message.files);
|
|
|
|
|
|
|
|
if (message.files.length) {
|
|
|
|
injectJS(sender.tab.id ? sender.tab.id : null, message.files, function() {
|
|
|
|
// @fixme does not sending response now!
|
2015-01-07 04:19:02 +00:00
|
|
|
console.info('All scripts executed');
|
|
|
|
|
2015-01-07 02:36:06 +00:00
|
|
|
sendResponse(true);
|
2015-01-07 04:19:02 +00:00
|
|
|
return true;
|
2015-01-07 02:36:06 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* May be not?
|
|
|
|
* But I don't want to block some shit-code execution
|
|
|
|
*/
|
|
|
|
sendResponse(false);
|
|
|
|
|
|
|
|
console.warn('No scripts executed (empty script array)');
|
|
|
|
}
|
2015-01-07 04:19:02 +00:00
|
|
|
|
2015-01-07 04:43:17 +00:00
|
|
|
// Fuck You, Chrome API documentation!
|
2015-01-07 04:19:02 +00:00
|
|
|
return true;
|
2015-01-07 02:36:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated since 1.19.1
|
|
|
|
*/
|
2014-04-21 20:42:53 +00:00
|
|
|
case 'injectCSSFile':
|
2015-01-07 02:36:06 +00:00
|
|
|
console.log('Injecting CSS: "%s"', message.file);
|
|
|
|
chrome.tabs.insertCSS(sender.tab.id ? sender.tab.id : null, {
|
2014-04-21 20:42:53 +00:00
|
|
|
file: message.file
|
2015-01-07 02:36:06 +00:00
|
|
|
}, function() {
|
2015-01-07 04:43:17 +00:00
|
|
|
// @todo message response callback processing
|
|
|
|
//sendResponse(true);
|
2015-01-07 02:36:06 +00:00
|
|
|
|
|
|
|
console.info('CSS file "%s" injected', message.file);
|
2014-04-21 20:42:53 +00:00
|
|
|
});
|
2015-01-07 04:19:02 +00:00
|
|
|
|
2015-01-07 04:43:17 +00:00
|
|
|
// Fuck You, Chrome API documentation!
|
2015-01-07 04:19:02 +00:00
|
|
|
return true;
|
2014-04-21 20:42:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'injectCSSCode':
|
|
|
|
if (message.code !== undefined) {
|
2015-01-07 02:36:06 +00:00
|
|
|
chrome.tabs.insertCSS(sender.tab.id ? sender.tab.id : null, {
|
2014-04-21 20:42:53 +00:00
|
|
|
code: message.code
|
2015-01-07 02:36:06 +00:00
|
|
|
}, function() {
|
2015-01-07 04:43:17 +00:00
|
|
|
// @todo message response callback processing
|
|
|
|
//sendResponse(true);
|
2015-01-07 02:36:06 +00:00
|
|
|
|
|
|
|
console.info('CSS code injected: \n%s', message.file);
|
2014-04-21 20:42:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-07 04:43:17 +00:00
|
|
|
// Fuck You, Chrome API documentation!
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
sendResponse(false);
|
2015-01-07 04:19:02 +00:00
|
|
|
return true;
|
2014-04-21 20:42:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-26 18:25:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Getting version from manifest.json
|
2015-01-07 19:11:10 +00:00
|
|
|
function getVersion() {
|
|
|
|
/**
|
|
|
|
* @deprecated XMLHttpRequest in the background worker is deprecated
|
|
|
|
* according to the Chrome warning. But we definitely need synchronous
|
|
|
|
* AJAX here
|
|
|
|
*/
|
2014-11-26 18:25:56 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', chrome.extension.getURL('manifest.json'), false);
|
|
|
|
xhr.send(null);
|
|
|
|
var manifest = JSON.parse(xhr.responseText);
|
|
|
|
return manifest.version;
|
2015-01-08 14:41:12 +00:00
|
|
|
}
|