Skip to content
Snippets Groups Projects
Commit 9dcd8bbc authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Refactor existing code

parent 5a7d92e2
No related branches found
No related tags found
No related merge requests found
/**
* Application settings content script.
*
* @module content-scripts/settings
*/
'use strict';
/** /**
* Fires once the content script worker is initialized. * Fires once the content script worker is initialized.
* *
* @event worker:initialized
* @param {Object} initialState The initial state of the settings module. * @param {Object} initialState The initial state of the settings module.
* @listens main/worker:initialized
*/ */
self.port.once('worker:initialized', function (initialState) { self.port.once('worker:initialized', function (initialState) {
applyL10n(initialState.l10n); applyL10n(initialState.l10n);
}); });
/**
* Fires once a preference has been fetched.
*
* @param {Object} preference The externally fetched preference.
* @listens main/preference:fetched
*/
self.port.on('preference:fetched', function () { self.port.on('preference:fetched', function () {
// TODO Re-render the user interface. // TODO Re-render the user interface.
}); });
...@@ -18,8 +31,8 @@ self.port.on('preference:fetched', function () { ...@@ -18,8 +31,8 @@ self.port.on('preference:fetched', function () {
* Applies a given localization to marked document elements. * Applies a given localization to marked document elements.
* https://bugzilla.mozilla.org/show_bug.cgi?id=787351 * https://bugzilla.mozilla.org/show_bug.cgi?id=787351
* *
* @private
* @param {Object} l10n A localization object. * @param {Object} l10n A localization object.
* @private
*/ */
function applyL10n (l10n) { function applyL10n (l10n) {
...@@ -35,15 +48,22 @@ function applyL10n (l10n) { ...@@ -35,15 +48,22 @@ function applyL10n (l10n) {
}); });
} }
/**
* Fetch preference event emitter.
*
* @event preference:fetch
* @property {String} preferenceKey The key of the requested preference.
*/
function fetchPreference (preferenceKey) { function fetchPreference (preferenceKey) {
self.port.emit('preference:fetch', preferenceKey); self.port.emit('preference:fetch', preferenceKey);
} }
/** /**
* Emitters * Preference changed event emitter.
*
* @event preference:changed
* @property {Object} preference The updated preference.
*/ */
function preferenceChanged (preference) {
self.port.emit('preference:changed', { self.port.emit('preference:changed', preference);
key: 'sliderPosition', }
value: 2
});
/** /**
* Main * Application entry point.
*
* @module main
*/ */
'use strict'; 'use strict';
/** /**
* Imports * Simplifies the process of obtaining browser service references.
*
* @var {Object} Services
*/ */
var { Services } = require('resource://gre/modules/Services.jsm'); var { Services } = require('resource://gre/modules/Services.jsm');
/**
* Can be used to get localization entries by key name.
*
* @var {function} _
*/
var _ = require('sdk/l10n').get; var _ = require('sdk/l10n').get;
var pageMod = require('sdk/page-mod');
var self = require("sdk/self");
/** /**
* Constants * Facilitates running scripts in the context of web pages.
*
* @var {Object} pageMod
*/ */
var pageMod = require('sdk/page-mod');
const BROWSER_NAME = 'Orfox'; /**
* Provides access to extension metadata.
*
* @var {Object} self
*/
var self = require("sdk/self");
/** /**
* Variables * The browser's chrome window object.
*
* @var {Object} chromeWindow
*/ */
var chromeWindow = Services.wm.getMostRecentWindow('navigator:browser');
var browserWindow = Services.wm.getMostRecentWindow('navigator:browser'); /**
var browserWindowIdentifier = null; * Uniquely identifies the extension's chrome menu item.
*
* @var {Number} chomeMenuItemIdentifier
*/
var chromeMenuItemIdentifier = null;
/** /**
* Public Functions * The name of the target browser.
*
* @constant BROWSER_NAME
* @type {String}
* @default
*/ */
const BROWSER_NAME = 'Orfox';
/**
* Executed as soon as the add-on is loaded.
*/
exports.main = function () { exports.main = function () {
pageMod.PageMod({ pageMod.PageMod({
...@@ -40,11 +69,12 @@ exports.main = function () { ...@@ -40,11 +69,12 @@ exports.main = function () {
onAttach: startListening onAttach: startListening
}); });
browserWindowIdentifier = browserWindow.NativeWindow.menu.add({ // Add the extension's chrome menu item to the main browser menu.
chromeMenuItemIdentifier = chromeWindow.NativeWindow.menu.add({
name: _('settings_label', BROWSER_NAME), name: _('settings_label', BROWSER_NAME),
callback: function () { callback: function () {
var tabBrowser = browserWindow.BrowserApp; var tabBrowser = chromeWindow.BrowserApp;
tabBrowser.addTab('chrome://tor-browser-settings/content/settings.html', { tabBrowser.addTab('chrome://tor-browser-settings/content/settings.html', {
selected: true, selected: true,
...@@ -55,16 +85,20 @@ exports.main = function () { ...@@ -55,16 +85,20 @@ exports.main = function () {
}); });
}; };
/**
* Executed when the add-on is unloaded.
*/
exports.onUnload = function () { exports.onUnload = function () {
// Clean up add-on state. // Clean up add-on state.
browserWindow.NativeWindow.menu.remove(browserWindowIdentifier); chromeWindow.NativeWindow.menu.remove(chromeMenuItemIdentifier);
}; };
/** /**
* Private Functions * Executed as soon as a content script has been attached to a page.
*
* @param {Object} worker Allows for direct communication with content scripts.
*/ */
function startListening (worker) { function startListening (worker) {
worker.port.emit('worker:initialized', { worker.port.emit('worker:initialized', {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment