|
@@ -3,24 +3,32 @@ var VMDB = require('./db');
|
|
|
var sync = require('./sync');
|
|
var sync = require('./sync');
|
|
|
var requests = require('./requests');
|
|
var requests = require('./requests');
|
|
|
var cache = require('./utils/cache');
|
|
var cache = require('./utils/cache');
|
|
|
-var tabsUtils = require('./utils/tabs');
|
|
|
|
|
var scriptUtils = require('./utils/script');
|
|
var scriptUtils = require('./utils/script');
|
|
|
var clipboard = require('./utils/clipboard');
|
|
var clipboard = require('./utils/clipboard');
|
|
|
var options = require('./options');
|
|
var options = require('./options');
|
|
|
|
|
|
|
|
var vmdb = exports.vmdb = new VMDB;
|
|
var vmdb = exports.vmdb = new VMDB;
|
|
|
-var VM_VER = chrome.runtime.getManifest().version;
|
|
|
|
|
|
|
+var VM_VER = browser.runtime.getManifest().version;
|
|
|
|
|
|
|
|
options.hook(function (changes) {
|
|
options.hook(function (changes) {
|
|
|
if ('isApplied' in changes) {
|
|
if ('isApplied' in changes) {
|
|
|
setIcon(changes.isApplied);
|
|
setIcon(changes.isApplied);
|
|
|
}
|
|
}
|
|
|
- _.messenger.post({
|
|
|
|
|
|
|
+ browser.runtime.sendMessage({
|
|
|
cmd: 'UpdateOptions',
|
|
cmd: 'UpdateOptions',
|
|
|
data: changes,
|
|
data: changes,
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+function broadcast(data) {
|
|
|
|
|
+ browser.tabs.query({})
|
|
|
|
|
+ .then(function (tabs) {
|
|
|
|
|
+ tabs.forEach(function (tab) {
|
|
|
|
|
+ browser.tabs.sendMessage(tab.id, data);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
var autoUpdate = function () {
|
|
var autoUpdate = function () {
|
|
|
function check() {
|
|
function check() {
|
|
|
checking = true;
|
|
checking = true;
|
|
@@ -59,12 +67,14 @@ var commands = {
|
|
|
GetInjected: function (url, src) {
|
|
GetInjected: function (url, src) {
|
|
|
var data = {
|
|
var data = {
|
|
|
isApplied: options.get('isApplied'),
|
|
isApplied: options.get('isApplied'),
|
|
|
- injectMode: options.get('injectMode'),
|
|
|
|
|
version: VM_VER,
|
|
version: VM_VER,
|
|
|
};
|
|
};
|
|
|
- if (src.tab && src.url === src.tab.url) {
|
|
|
|
|
- chrome.tabs.sendMessage(src.tab.id, {cmd: 'GetBadge'});
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ setTimeout(function () {
|
|
|
|
|
+ // delayed to wait for the tab URL updated
|
|
|
|
|
+ if (src.tab && url === src.tab.url) {
|
|
|
|
|
+ browser.tabs.sendMessage(src.tab.id, {cmd: 'GetBadge'});
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
return data.isApplied
|
|
return data.isApplied
|
|
|
? vmdb.getScriptsByURL(url).then(function (res) {
|
|
? vmdb.getScriptsByURL(url).then(function (res) {
|
|
|
return Object.assign(data, res);
|
|
return Object.assign(data, res);
|
|
@@ -76,7 +86,7 @@ var commands = {
|
|
|
})
|
|
})
|
|
|
.then(function (script) {
|
|
.then(function (script) {
|
|
|
sync.sync();
|
|
sync.sync();
|
|
|
- _.messenger.post({
|
|
|
|
|
|
|
+ browser.runtime.sendMessage({
|
|
|
cmd: 'UpdateScript',
|
|
cmd: 'UpdateScript',
|
|
|
data: script,
|
|
data: script,
|
|
|
});
|
|
});
|
|
@@ -85,7 +95,7 @@ var commands = {
|
|
|
SetValue: function (data, _src) {
|
|
SetValue: function (data, _src) {
|
|
|
return vmdb.setValue(data.uri, data.values)
|
|
return vmdb.setValue(data.uri, data.values)
|
|
|
.then(function () {
|
|
.then(function () {
|
|
|
- tabsUtils.broadcast({
|
|
|
|
|
|
|
+ broadcast({
|
|
|
cmd: 'UpdateValues',
|
|
cmd: 'UpdateValues',
|
|
|
data: {
|
|
data: {
|
|
|
uri: data.uri,
|
|
uri: data.uri,
|
|
@@ -112,28 +122,27 @@ var commands = {
|
|
|
ParseScript: function (data, _src) {
|
|
ParseScript: function (data, _src) {
|
|
|
return vmdb.parseScript(data).then(function (res) {
|
|
return vmdb.parseScript(data).then(function (res) {
|
|
|
var meta = res.data.meta;
|
|
var meta = res.data.meta;
|
|
|
- if (!meta.grant.length && !options.get('ignoreGrant'))
|
|
|
|
|
|
|
+ if (!meta.grant.length && !options.get('ignoreGrant')) {
|
|
|
notify({
|
|
notify({
|
|
|
id: 'VM-NoGrantWarning',
|
|
id: 'VM-NoGrantWarning',
|
|
|
title: _.i18n('Warning'),
|
|
title: _.i18n('Warning'),
|
|
|
- body: _.i18n('msgWarnGrant', [meta.name||_.i18n('labelNoName')]),
|
|
|
|
|
|
|
+ body: _.i18n('msgWarnGrant', [meta.name || _.i18n('labelNoName')]),
|
|
|
isClickable: true,
|
|
isClickable: true,
|
|
|
});
|
|
});
|
|
|
- _.messenger.post(res);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ browser.runtime.sendMessage(res);
|
|
|
sync.sync();
|
|
sync.sync();
|
|
|
return res.data;
|
|
return res.data;
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
CheckUpdate: function (id, _src) {
|
|
CheckUpdate: function (id, _src) {
|
|
|
vmdb.getScript(id).then(vmdb.checkUpdate);
|
|
vmdb.getScript(id).then(vmdb.checkUpdate);
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
CheckUpdateAll: function (_data, _src) {
|
|
CheckUpdateAll: function (_data, _src) {
|
|
|
options.set('lastUpdate', Date.now());
|
|
options.set('lastUpdate', Date.now());
|
|
|
vmdb.getScriptsByIndex('update', 1).then(function (scripts) {
|
|
vmdb.getScriptsByIndex('update', 1).then(function (scripts) {
|
|
|
return Promise.all(scripts.map(vmdb.checkUpdate));
|
|
return Promise.all(scripts.map(vmdb.checkUpdate));
|
|
|
});
|
|
});
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
ParseMeta: function (code, _src) {
|
|
ParseMeta: function (code, _src) {
|
|
|
return scriptUtils.parseMeta(code);
|
|
return scriptUtils.parseMeta(code);
|
|
@@ -144,57 +153,46 @@ var commands = {
|
|
|
},
|
|
},
|
|
|
HttpRequest: function (details, src) {
|
|
HttpRequest: function (details, src) {
|
|
|
requests.httpRequest(details, function (res) {
|
|
requests.httpRequest(details, function (res) {
|
|
|
- _.messenger.send(src.tab.id, {
|
|
|
|
|
|
|
+ browser.tabs.sendMessage(src.tab.id, {
|
|
|
cmd: 'HttpRequested',
|
|
cmd: 'HttpRequested',
|
|
|
data: res,
|
|
data: res,
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
AbortRequest: function (id, _src) {
|
|
AbortRequest: function (id, _src) {
|
|
|
return requests.abortRequest(id);
|
|
return requests.abortRequest(id);
|
|
|
},
|
|
},
|
|
|
SetBadge: function (num, src) {
|
|
SetBadge: function (num, src) {
|
|
|
setBadge(num, src);
|
|
setBadge(num, src);
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
SyncAuthorize: function (_data, _src) {
|
|
SyncAuthorize: function (_data, _src) {
|
|
|
sync.authorize();
|
|
sync.authorize();
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
SyncRevoke: function (_data, _src) {
|
|
SyncRevoke: function (_data, _src) {
|
|
|
sync.revoke();
|
|
sync.revoke();
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
SyncStart: function (_data, _src) {
|
|
SyncStart: function (_data, _src) {
|
|
|
sync.sync();
|
|
sync.sync();
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
GetFromCache: function (data, _src) {
|
|
GetFromCache: function (data, _src) {
|
|
|
return cache.get(data) || null;
|
|
return cache.get(data) || null;
|
|
|
},
|
|
},
|
|
|
Notification: function (data, _src) {
|
|
Notification: function (data, _src) {
|
|
|
- return new Promise(function (resolve) {
|
|
|
|
|
- chrome.notifications.create({
|
|
|
|
|
- type: 'basic',
|
|
|
|
|
- title: data.title || _.i18n('extName'),
|
|
|
|
|
- message: data.text,
|
|
|
|
|
- iconUrl: data.image || _.defaultImage,
|
|
|
|
|
- }, function (id) {
|
|
|
|
|
- resolve(id);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ return browser.notifications.create({
|
|
|
|
|
+ type: 'basic',
|
|
|
|
|
+ title: data.title || _.i18n('extName'),
|
|
|
|
|
+ message: data.text,
|
|
|
|
|
+ iconUrl: data.image || _.defaultImage,
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
SetClipboard: function (data, _src) {
|
|
SetClipboard: function (data, _src) {
|
|
|
clipboard.set(data);
|
|
clipboard.set(data);
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
OpenTab: function (data, _src) {
|
|
OpenTab: function (data, _src) {
|
|
|
- chrome.tabs.create({
|
|
|
|
|
|
|
+ browser.tabs.create({
|
|
|
url: data.url,
|
|
url: data.url,
|
|
|
active: data.active,
|
|
active: data.active,
|
|
|
});
|
|
});
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
GetAllOptions: function (_data, _src) {
|
|
GetAllOptions: function (_data, _src) {
|
|
|
return options.getAll();
|
|
return options.getAll();
|
|
@@ -210,39 +208,21 @@ var commands = {
|
|
|
data.forEach(function (item) {
|
|
data.forEach(function (item) {
|
|
|
options.set(item.key, item.value);
|
|
options.set(item.key, item.value);
|
|
|
});
|
|
});
|
|
|
- return false;
|
|
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
vmdb.initialized.then(function () {
|
|
vmdb.initialized.then(function () {
|
|
|
- chrome.runtime.onMessage.addListener(function (req, src, callback) {
|
|
|
|
|
|
|
+ browser.runtime.onMessage.addListener(function (req, src) {
|
|
|
var func = commands[req.cmd];
|
|
var func = commands[req.cmd];
|
|
|
|
|
+ var res;
|
|
|
if (func) {
|
|
if (func) {
|
|
|
- var res = func(req.data, src);
|
|
|
|
|
- if (res === false) return;
|
|
|
|
|
- var finish = function (data) {
|
|
|
|
|
- try {
|
|
|
|
|
- callback(data);
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- // callback fails if not given in content page
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
- Promise.resolve(res).then(function (data) {
|
|
|
|
|
- finish({
|
|
|
|
|
- data: data,
|
|
|
|
|
- error: null,
|
|
|
|
|
- });
|
|
|
|
|
- }, function (data) {
|
|
|
|
|
- if (data instanceof Error) {
|
|
|
|
|
- console.error(data);
|
|
|
|
|
- data = data.toString();
|
|
|
|
|
- }
|
|
|
|
|
- finish({
|
|
|
|
|
- error: data,
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ res = func(req.data, src);
|
|
|
|
|
+ if (typeof res !== 'undefined') {
|
|
|
|
|
+ // If res is not instance of native Promise, browser APIs will not wait for it.
|
|
|
|
|
+ res = Promise.resolve(res);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ return res;
|
|
|
});
|
|
});
|
|
|
setTimeout(autoUpdate, 2e4);
|
|
setTimeout(autoUpdate, 2e4);
|
|
|
sync.initialize();
|
|
sync.initialize();
|
|
@@ -251,7 +231,7 @@ vmdb.initialized.then(function () {
|
|
|
// Common functions
|
|
// Common functions
|
|
|
|
|
|
|
|
function notify(options) {
|
|
function notify(options) {
|
|
|
- chrome.notifications.create(options.id || 'ViolentMonkey', {
|
|
|
|
|
|
|
+ browser.notifications.create(options.id || 'ViolentMonkey', {
|
|
|
type: 'basic',
|
|
type: 'basic',
|
|
|
iconUrl: _.defaultImage,
|
|
iconUrl: _.defaultImage,
|
|
|
title: options.title + ' - ' + _.i18n('extName'),
|
|
title: options.title + ' - ' + _.i18n('extName'),
|
|
@@ -266,12 +246,12 @@ var setBadge = function () {
|
|
|
var o = badges[src.id];
|
|
var o = badges[src.id];
|
|
|
if (!o) o = badges[src.id] = {num: 0};
|
|
if (!o) o = badges[src.id] = {num: 0};
|
|
|
o.num += num;
|
|
o.num += num;
|
|
|
- chrome.browserAction.setBadgeBackgroundColor({
|
|
|
|
|
|
|
+ browser.browserAction.setBadgeBackgroundColor({
|
|
|
color: '#808',
|
|
color: '#808',
|
|
|
tabId: src.tab.id,
|
|
tabId: src.tab.id,
|
|
|
});
|
|
});
|
|
|
var text = (options.get('showBadge') && o.num || '').toString();
|
|
var text = (options.get('showBadge') && o.num || '').toString();
|
|
|
- chrome.browserAction.setBadgeText({
|
|
|
|
|
|
|
+ browser.browserAction.setBadgeText({
|
|
|
text: text,
|
|
text: text,
|
|
|
tabId: src.tab.id,
|
|
tabId: src.tab.id,
|
|
|
});
|
|
});
|
|
@@ -282,40 +262,31 @@ var setBadge = function () {
|
|
|
};
|
|
};
|
|
|
}();
|
|
}();
|
|
|
|
|
|
|
|
-_.messenger = function () {
|
|
|
|
|
- return {
|
|
|
|
|
- post: function (data) {
|
|
|
|
|
- chrome.runtime.sendMessage(data);
|
|
|
|
|
- },
|
|
|
|
|
- send: function (tabId, data) {
|
|
|
|
|
- chrome.tabs.sendMessage(tabId, data);
|
|
|
|
|
- },
|
|
|
|
|
- };
|
|
|
|
|
-}();
|
|
|
|
|
-
|
|
|
|
|
function setIcon(isApplied) {
|
|
function setIcon(isApplied) {
|
|
|
- chrome.browserAction.setIcon({
|
|
|
|
|
|
|
+ browser.browserAction.setIcon({
|
|
|
path: {
|
|
path: {
|
|
|
- 19: '/images/icon19' + (isApplied ? '' : 'w') + '.png',
|
|
|
|
|
- 38: '/images/icon38' + (isApplied ? '' : 'w') + '.png'
|
|
|
|
|
|
|
+ 19: '/public/images/icon19' + (isApplied ? '' : 'w') + '.png',
|
|
|
|
|
+ 38: '/public/images/icon38' + (isApplied ? '' : 'w') + '.png'
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
setIcon(options.get('isApplied'));
|
|
setIcon(options.get('isApplied'));
|
|
|
|
|
|
|
|
-chrome.notifications.onClicked.addListener(function (id) {
|
|
|
|
|
- if (id == 'VM-NoGrantWarning') {
|
|
|
|
|
- tabsUtils.create('http://wiki.greasespot.net/@grant');
|
|
|
|
|
|
|
+browser.notifications.onClicked.addListener(function (id) {
|
|
|
|
|
+ if (id === 'VM-NoGrantWarning') {
|
|
|
|
|
+ browser.tabs.create({
|
|
|
|
|
+ url: 'http://wiki.greasespot.net/@grant',
|
|
|
|
|
+ });
|
|
|
} else {
|
|
} else {
|
|
|
- tabsUtils.broadcast({
|
|
|
|
|
|
|
+ broadcast({
|
|
|
cmd: 'NotificationClick',
|
|
cmd: 'NotificationClick',
|
|
|
data: id,
|
|
data: id,
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-chrome.notifications.onClosed.addListener(function (id) {
|
|
|
|
|
- tabsUtils.broadcast({
|
|
|
|
|
|
|
+browser.notifications.onClosed.addListener(function (id) {
|
|
|
|
|
+ broadcast({
|
|
|
cmd: 'NotificationClose',
|
|
cmd: 'NotificationClose',
|
|
|
data: id,
|
|
data: id,
|
|
|
});
|
|
});
|