menu.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var app = require('../app');
  2. var MixIn = require('./mixin');
  3. var _ = require('../../common');
  4. module.exports = {
  5. mixins: [MixIn],
  6. data: function () {
  7. var _this = this;
  8. return {
  9. top: [{
  10. name: _.i18n('menuManageScripts'),
  11. symbol: 'cog',
  12. onClick: function () {
  13. var url = chrome.extension.getURL(chrome.app.getDetails().options_page);
  14. chrome.tabs.query({
  15. currentWindow: true,
  16. url: url,
  17. }, function (tabs) {
  18. var tab = tabs.find(function (tab) {
  19. var hash = tab.url.match(/#(\w+)/);
  20. return !hash || hash[1] !== 'confirm';
  21. });
  22. if (tab) chrome.tabs.update(tab.id, {active: true});
  23. else chrome.tabs.create({url: url});
  24. });
  25. },
  26. }, {
  27. name: _.i18n('menuFindScripts'),
  28. symbol: 'search',
  29. hide: function () {
  30. var domains = this.store.domains;
  31. return !domains || !domains.length;
  32. },
  33. onClick: function () {
  34. var matches = _this.store.currentTab.url.match(/:\/\/(?:www\.)?([^\/]*)/);
  35. chrome.tabs.create({
  36. url: 'https://greasyfork.org/scripts/search?q=' + matches[1],
  37. });
  38. },
  39. detailClick: function () {
  40. app.navigate('Domains');
  41. },
  42. }, {
  43. name: _.i18n('menuCommands'),
  44. symbol: 'arrow-right',
  45. hide: function () {
  46. var commands = _this.store.commands;
  47. return !commands || !commands.length;
  48. },
  49. onClick: function () {
  50. app.navigate('Commands');
  51. },
  52. }, {
  53. name: null,
  54. symbol: null,
  55. disabled: null,
  56. init: function (options) {
  57. options.update(options);
  58. _.options.hook(function (data) {
  59. data.isApplied != null && options.update(options);
  60. });
  61. },
  62. update: function (options) {
  63. options.disabled = !_.options.get('isApplied');
  64. options.name = options.disabled ? _.i18n('menuScriptDisabled') : _.i18n('menuScriptEnabled');
  65. options.symbol = options.disabled ? 'remove' : 'check';
  66. },
  67. onClick: function (options) {
  68. _.options.set('isApplied', options.disabled);
  69. },
  70. }],
  71. };
  72. },
  73. computed: {
  74. bot: function () {
  75. var _this = this;
  76. return _this.store.scripts.map(function (script) {
  77. return {
  78. name: script.custom.name || _.getLocaleString(script.meta, 'name'),
  79. className: 'ellipsis',
  80. symbol: null,
  81. disabled: null,
  82. init: function (options) {
  83. options.disabled = !script.enabled;
  84. options.symbol = options.disabled ? 'remove' : 'check';
  85. },
  86. onClick: function (options) {
  87. var vm = this;
  88. _.sendMessage({
  89. cmd: 'UpdateScriptInfo',
  90. data: {
  91. id: script.id,
  92. enabled: !script.enabled,
  93. },
  94. })
  95. .then(function () {
  96. script.enabled = !script.enabled;
  97. options.init.call(vm, options);
  98. _.options.get('autoReload') && chrome.tabs.reload(_this.store.currentTab.id);
  99. });
  100. },
  101. };
  102. });
  103. },
  104. },
  105. watch: {
  106. 'store.scripts': 'fixStyles',
  107. 'store.commands': 'fixStyles',
  108. 'store.domains': 'fixStyles',
  109. },
  110. };