addon-sdk/source/lib/sdk/l10n/html.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial