utils.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'engines': {
  7. 'Firefox': '*'
  8. }
  9. };
  10. const { Cu } = require('chrome');
  11. const { getMostRecentBrowserWindow } = require('sdk/window/utils');
  12. const { fromIterator } = require('sdk/util/array');
  13. const BUILTIN_SIDEBAR_MENUITEMS = exports.BUILTIN_SIDEBAR_MENUITEMS = [
  14. 'menu_socialSidebar',
  15. 'menu_historySidebar',
  16. 'menu_bookmarksSidebar'
  17. ];
  18. function isSidebarShowing(window) {
  19. window = window || getMostRecentBrowserWindow();
  20. let sidebar = window.document.getElementById('sidebar-box');
  21. return !sidebar.hidden;
  22. }
  23. exports.isSidebarShowing = isSidebarShowing;
  24. function getSidebarMenuitems(window) {
  25. window = window || getMostRecentBrowserWindow();
  26. return fromIterator(window.document.querySelectorAll('#viewSidebarMenu menuitem'));
  27. }
  28. exports.getSidebarMenuitems = getSidebarMenuitems;
  29. function getExtraSidebarMenuitems() {
  30. let menuitems = getSidebarMenuitems();
  31. return menuitems.filter(function(mi) {
  32. return BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) < 0;
  33. });
  34. }
  35. exports.getExtraSidebarMenuitems = getExtraSidebarMenuitems;
  36. function makeID(id) {
  37. return 'jetpack-sidebar-' + id;
  38. }
  39. exports.makeID = makeID;
  40. function simulateCommand(ele) {
  41. let window = ele.ownerDocument.defaultView;
  42. let { document } = window;
  43. var evt = document.createEvent('XULCommandEvent');
  44. evt.initCommandEvent('command', true, true, window,
  45. 0, false, false, false, false, null);
  46. ele.dispatchEvent(evt);
  47. }
  48. exports.simulateCommand = simulateCommand;
  49. function simulateClick(ele) {
  50. let window = ele.ownerDocument.defaultView;
  51. let { document } = window;
  52. let evt = document.createEvent('MouseEvents');
  53. evt.initMouseEvent('click', true, true, window,
  54. 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  55. ele.dispatchEvent(evt);
  56. }
  57. exports.simulateClick = simulateClick;
  58. // OSX and Windows exhibit different behaviors when 'checked' is false,
  59. // so compare against the consistent 'true'. See bug 894809.
  60. function isChecked(node) {
  61. return node.getAttribute('checked') === 'true';
  62. };
  63. exports.isChecked = isChecked;