html.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "stability": "unstable"
  7. };
  8. const { Ci, Cu } = require("chrome");
  9. const events = require("../system/events");
  10. const core = require("./core");
  11. const assetsURI = require('../self').data.url();
  12. const { Services } = Cu.import("resource://gre/modules/Services.jsm");
  13. const hideContentStyle = "data:text/css,:root {visibility: hidden !important;}";
  14. const hideSheetUri = Services.io.newURI(hideContentStyle, null, null);
  15. // Taken from Gaia:
  16. // https://github.com/andreasgal/gaia/blob/04fde2640a7f40314643016a5a6c98bf3755f5fd/webapi.js#L1470
  17. function translateElement(element) {
  18. element = element || document;
  19. // check all translatable children (= w/ a `data-l10n-id' attribute)
  20. var children = element.querySelectorAll('*[data-l10n-id]');
  21. var elementCount = children.length;
  22. for (var i = 0; i < elementCount; i++) {
  23. var child = children[i];
  24. // translate the child
  25. var key = child.dataset.l10nId;
  26. var data = core.get(key);
  27. if (data)
  28. child.textContent = data;
  29. }
  30. }
  31. exports.translateElement = translateElement;
  32. function onDocumentReady2Translate(event) {
  33. let document = event.target;
  34. document.removeEventListener("DOMContentLoaded", onDocumentReady2Translate,
  35. false);
  36. translateElement(document);
  37. try {
  38. // Finally display document when we finished replacing all text content
  39. let winUtils = document.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
  40. .getInterface(Ci.nsIDOMWindowUtils);
  41. winUtils.removeSheet(hideSheetUri, winUtils.USER_SHEET);
  42. }
  43. catch(e) {
  44. console.exception(e);
  45. }
  46. }
  47. function onContentWindow(event) {
  48. let document = event.subject;
  49. // Accept only HTML documents
  50. if (!(document instanceof Ci.nsIDOMHTMLDocument))
  51. return;
  52. // Bug 769483: data:URI documents instanciated with nsIDOMParser
  53. // have a null `location` attribute at this time
  54. if (!document.location)
  55. return;
  56. // Accept only document from this addon
  57. if (document.location.href.indexOf(assetsURI) !== 0)
  58. return;
  59. try {
  60. // First hide content of the document in order to have content blinking
  61. // between untranslated and translated states
  62. let winUtils = document.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
  63. .getInterface(Ci.nsIDOMWindowUtils);
  64. winUtils.loadSheet(hideSheetUri, winUtils.USER_SHEET);
  65. }
  66. catch(e) {
  67. console.exception(e);
  68. }
  69. // Wait for DOM tree to be built before applying localization
  70. document.addEventListener("DOMContentLoaded", onDocumentReady2Translate,
  71. false);
  72. }
  73. // Listen to creation of content documents in order to translate them as soon
  74. // as possible in their loading process
  75. const ON_CONTENT = "document-element-inserted";
  76. let enabled = false;
  77. function enable() {
  78. if (!enabled) {
  79. events.on(ON_CONTENT, onContentWindow);
  80. enabled = true;
  81. }
  82. }
  83. exports.enable = enable;
  84. function disable() {
  85. if (enabled) {
  86. events.off(ON_CONTENT, onContentWindow);
  87. enabled = false;
  88. }
  89. }
  90. exports.disable = disable;
  91. require("sdk/system/unload").when(disable);