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 1.9KB

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