For a mouseless future.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var tabs = require("sdk/tabs");
  2. var self = require("sdk/self");
  3. var simple_prefs = require("sdk/simple-prefs");
  4. var clipboard = require("sdk/clipboard");
  5. var conf = {};
  6. var keys = {};
  7. function contains(arr, val) {
  8. if (typeof arr !== "object")
  9. return false;
  10. return (arr.indexOf(val) !== -1);
  11. }
  12. function prepareConf(prefs) {
  13. var keys = {};
  14. var conf = {};
  15. for (var i in prefs) {
  16. var pref = prefs[i];
  17. if (i === "chars") {
  18. conf[i] = pref;
  19. continue;
  20. }
  21. var modifiers = pref.match(/<[^>]+>/g) || [];
  22. var key = pref.replace(/<.+>/g, "");
  23. if (/^[A-Z]$/.test(key))
  24. modifiers.push("<Shift>");
  25. keys[i] = {
  26. code: key,
  27. shiftKey: contains(modifiers, "<Shift>"),
  28. ctrlKey: contains(modifiers, "<Control>")
  29. }
  30. }
  31. return {keys: keys, conf: conf};
  32. }
  33. var res = prepareConf(simple_prefs.prefs);
  34. conf = res.conf;
  35. keys = res.keys;
  36. simple_prefs.on("", function() {
  37. var res = prepareConf(simple_prefs.prefs);
  38. conf = res.conf;
  39. keys = res.keys;
  40. });
  41. tabs.on("ready", function(tab) {
  42. var worker = tab.attach({
  43. contentScriptFile: self.data.url("onload.js")
  44. });
  45. worker.port.emit("conf", conf);
  46. worker.port.emit("keys", keys);
  47. function selectRelativeTab(n) {
  48. var tabList = [];
  49. var currentTabIndex;
  50. for (let t of tabs) {
  51. tabList[t.index] = t;
  52. if (t.index == tab.index) {
  53. currentTabIndex = t.index;
  54. }
  55. }
  56. var newTabIndex = currentTabIndex + n;
  57. if (newTabIndex >= tabList.length) {
  58. newTabIndex = 0;
  59. } else if (newTabIndex < 0) {
  60. newTabIndex = tabList.length - 1;
  61. }
  62. tabList[newTabIndex].activate();
  63. }
  64. function moveRelativeTab(n) {
  65. tab.index += n;
  66. }
  67. worker.port.on("tab_open", function(url) {
  68. tabs.open(url);
  69. });
  70. worker.port.on("change_tab_left", function() {
  71. selectRelativeTab(-1);
  72. });
  73. worker.port.on("change_tab_right", function() {
  74. selectRelativeTab(1);
  75. });
  76. worker.port.on("move_tab_left", function() {
  77. moveRelativeTab(-1);
  78. });
  79. worker.port.on("move_tab_right", function() {
  80. moveRelativeTab(1);
  81. });
  82. worker.port.on("clipboard_set", function(text) {
  83. clipboard.set(text);
  84. });
  85. });