main.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. var vmdb = new VMDB;
  2. var VM_VER = chrome.app.getDetails().version;
  3. var commands = {
  4. NewScript: function (data, src) {
  5. return scriptUtils.newScript();
  6. },
  7. RemoveScript: function (id, src) {
  8. return vmdb.removeScript(id)
  9. .then(function () {
  10. sync.sync();
  11. _.messenger.post({
  12. cmd: 'del',
  13. data: id,
  14. });
  15. });
  16. },
  17. GetData: function (data, src) {
  18. return vmdb.getData().then(function (data) {
  19. data.sync = sync.states();
  20. data.version = VM_VER;
  21. return data;
  22. });
  23. },
  24. GetInjected: function (url, src) {
  25. var data = {
  26. isApplied: _.options.get('isApplied'),
  27. injectMode: _.options.get('injectMode'),
  28. version: VM_VER,
  29. };
  30. if (src.url == src.tab.url)
  31. chrome.tabs.sendMessage(src.tab.id, {cmd: 'GetBadge'});
  32. return data.isApplied
  33. ? vmdb.getScriptsByURL(url).then(function (res) {
  34. return _.assign(data, res);
  35. }) : data;
  36. },
  37. UpdateScriptInfo: function (data, src) {
  38. return vmdb.updateScriptInfo(data.id, data, {
  39. modified: Date.now(),
  40. })
  41. .then(function (script) {
  42. sync.sync();
  43. _.messenger.post({
  44. cmd: 'update',
  45. data: script,
  46. });
  47. });
  48. },
  49. SetValue: function (data, src) {
  50. return vmdb.setValue(data.uri, data.values)
  51. .then(function () {
  52. _.broadcast({
  53. cmd: 'UpdateValues',
  54. data: {
  55. uri: data.uri,
  56. values: data.values,
  57. },
  58. });
  59. });
  60. },
  61. ExportZip: function (data, src) {
  62. return vmdb.getExportData(data.ids, data.values);
  63. },
  64. GetScript: function (id, src) {
  65. return vmdb.getScriptData(id);
  66. },
  67. GetMetas: function (ids, src) {
  68. return vmdb.getScriptInfos(ids);
  69. },
  70. Move: function (data, src) {
  71. return vmdb.moveScript(data.id, data.offset);
  72. },
  73. Vacuum: function (data, src) {
  74. return vmdb.vacuum();
  75. },
  76. ParseScript: function (data, src) {
  77. return vmdb.parseScript(data).then(function (res) {
  78. var meta = res.data.meta;
  79. if (!meta.grant.length && !_.options.get('ignoreGrant'))
  80. notify({
  81. id: 'VM-NoGrantWarning',
  82. title: _.i18n('Warning'),
  83. body: _.i18n('msgWarnGrant', [meta.name||_.i18n('labelNoName')]),
  84. isClickable: true,
  85. });
  86. _.messenger.post(res);
  87. sync.sync();
  88. return res.data;
  89. });
  90. },
  91. CheckUpdate: function (id, src) {
  92. vmdb.getScript(id).then(vmdb.checkUpdate);
  93. return false;
  94. },
  95. CheckUpdateAll: function (data, src) {
  96. _.options.set('lastUpdate', Date.now());
  97. vmdb.getScriptsByIndex('update', 1).then(function (scripts) {
  98. return Promise.all(scripts.map(vmdb.checkUpdate));
  99. });
  100. return false;
  101. },
  102. ParseMeta: function (code, src) {
  103. return scriptUtils.parseMeta(code);
  104. },
  105. AutoUpdate: autoUpdate,
  106. GetRequestId: function (data, src) {
  107. return requests.getRequestId();
  108. },
  109. HttpRequest: function (details, src) {
  110. requests.httpRequest(details, function (res) {
  111. _.messenger.send(src.tab.id, {
  112. cmd: 'HttpRequested',
  113. data: res,
  114. });
  115. });
  116. return false;
  117. },
  118. AbortRequest: function (id, src) {
  119. return requests.abortRequest(id);
  120. },
  121. SetBadge: function (num, src) {
  122. setBadge(num, src);
  123. return false;
  124. },
  125. Authenticate: function (data, src) {
  126. var service = sync.service(data);
  127. service && service.authenticate && service.authenticate();
  128. return false;
  129. },
  130. SyncStart: function (data, src) {
  131. sync.sync(data && sync.service(data));
  132. return false;
  133. },
  134. };
  135. vmdb.initialized.then(function () {
  136. chrome.runtime.onMessage.addListener(function (req, src, callback) {
  137. var func = commands[req.cmd];
  138. if (func) {
  139. var res = func(req.data, src);
  140. if (res === false) return;
  141. var finish = function (data) {
  142. try {
  143. callback(data);
  144. } catch (e) {
  145. // callback fails if not given in content page
  146. }
  147. };
  148. Promise.resolve(res).then(function (data) {
  149. finish({
  150. data: data,
  151. error: null,
  152. });
  153. }, function (data) {
  154. finish({
  155. error: data,
  156. });
  157. });
  158. return true;
  159. }
  160. });
  161. setTimeout(autoUpdate, 2e4);
  162. sync.init();
  163. });
  164. // Common functions
  165. function notify(options) {
  166. chrome.notifications.create(options.id || 'ViolentMonkey', {
  167. type: 'basic',
  168. iconUrl: '/images/icon128.png',
  169. title: options.title + ' - ' + _.i18n('extName'),
  170. message: options.body,
  171. isClickable: options.isClickable,
  172. });
  173. }
  174. var setBadge = function () {
  175. var badges = {};
  176. return function (num, src) {
  177. var o = badges[src.id];
  178. if (!o) o = badges[src.id] = {num: 0};
  179. o.num += num;
  180. chrome.browserAction.setBadgeBackgroundColor({
  181. color: '#808',
  182. tabId: src.tab.id,
  183. });
  184. chrome.browserAction.setBadgeText({
  185. text: (o.num || '').toString(),
  186. tabId: src.tab.id,
  187. });
  188. if (o.timer) clearTimeout(o.timer);
  189. o.timer = setTimeout(function () {
  190. delete badges[src.id];
  191. }, 300);
  192. };
  193. }();
  194. var autoUpdate = function () {
  195. function check() {
  196. checking = true;
  197. return new Promise(function (resolve, reject) {
  198. if (!_.options.get('autoUpdate')) return reject();
  199. if (Date.now() - _.options.get('lastUpdate') >= 864e5)
  200. return commands.CheckUpdateAll();
  201. }).then(function () {
  202. setTimeout(check, 36e5);
  203. }, function () {
  204. checking = false;
  205. });
  206. }
  207. var checking;
  208. return function () {
  209. checking || check();
  210. };
  211. }();
  212. _.messenger = function () {
  213. var port;
  214. chrome.runtime.onConnect.addListener(function (_port) {
  215. port = _port;
  216. _port.onDisconnect.addListener(function () {
  217. if (port === _port) port = null;
  218. });
  219. });
  220. return {
  221. post: function (data) {
  222. try {
  223. port && port.postMessage(data);
  224. } catch (e) {
  225. console.log(e);
  226. port = null;
  227. }
  228. },
  229. send: function (tabId, data) {
  230. chrome.tabs.sendMessage(tabId, data);
  231. },
  232. };
  233. }();
  234. !function (isApplied) {
  235. chrome.browserAction.setIcon({
  236. path: {
  237. 19: '/images/icon19' + (isApplied ? '' : 'w') + '.png',
  238. 38: '/images/icon38' + (isApplied ? '' : 'w') + '.png'
  239. },
  240. });
  241. }(_.options.get('isApplied'));
  242. chrome.notifications.onClicked.addListener(function(id) {
  243. if(id == 'VM-NoGrantWarning')
  244. chrome.tabs.create({url: 'http://wiki.greasespot.net/@grant'});
  245. });