omega_target_popup.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. function callBackgroundNoReply(method, args, cb) {
  2. chrome.runtime.sendMessage({
  3. method: method,
  4. args: args,
  5. noReply: true,
  6. refreshActivePage: true,
  7. });
  8. if (cb) return cb();
  9. }
  10. function callBackground(method, args, cb) {
  11. chrome.runtime.sendMessage({
  12. method: method,
  13. args: args,
  14. }, function(response) {
  15. if (chrome.runtime.lastError != null)
  16. return cb && cb(chrome.runtime.lastError)
  17. if (response.error) return cb && cb(response.error)
  18. return cb && cb(null, response.result)
  19. });
  20. }
  21. var requestInfoCallback = null;
  22. OmegaTargetPopup = {
  23. getState: function (keys, cb) {
  24. if (typeof localStorage === 'undefined' || !localStorage.length) {
  25. callBackground('getState', [keys], cb);
  26. return;
  27. }
  28. var results = {};
  29. keys.forEach(function(key) {
  30. try {
  31. results[key] = JSON.parse(localStorage['omega.local.' + key]);
  32. } catch (_) {
  33. return null;
  34. }
  35. });
  36. if (cb) cb(null, results);
  37. },
  38. applyProfile: function (name, cb) {
  39. callBackgroundNoReply('applyProfile', [name], cb);
  40. },
  41. openOptions: function (hash, cb) {
  42. var options_url = chrome.extension.getURL('options.html');
  43. chrome.tabs.query({
  44. url: options_url
  45. }, function(tabs) {
  46. if (!chrome.runtime.lastError && tabs && tabs.length > 0) {
  47. var props = {
  48. active: true
  49. };
  50. if (hash) {
  51. var url = options_url + hash;
  52. props.url = url;
  53. }
  54. chrome.tabs.update(tabs[0].id, props);
  55. } else {
  56. chrome.tabs.create({
  57. url: options_url
  58. });
  59. }
  60. if (cb) return cb();
  61. });
  62. },
  63. getActivePageInfo: function(cb) {
  64. chrome.tabs.query({active: true, lastFocusedWindow: true}, function (tabs) {
  65. if (tabs.length === 0 || !tabs[0].url) return cb();
  66. var args = {tabId: tabs[0].id, url: tabs[0].url};
  67. callBackground('getPageInfo', [args], cb)
  68. });
  69. },
  70. setDefaultProfile: function(profileName, defaultProfileName, cb) {
  71. callBackgroundNoReply('setDefaultProfile',
  72. [profileName, defaultProfileName], cb);
  73. },
  74. addTempRule: function(domain, profileName, cb) {
  75. callBackgroundNoReply('addTempRule', [domain, profileName], cb);
  76. },
  77. openManage: function(domain, profileName, cb) {
  78. chrome.tabs.create({
  79. url: 'chrome://extensions/?id=' + chrome.runtime.id,
  80. }, cb);
  81. },
  82. getMessage: chrome.i18n.getMessage.bind(chrome.i18n),
  83. };