Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Multiple windows (fixes #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre committed May 10, 2015
1 parent a93822e commit 75257d0
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { translate, translateUrl } = require('./providers/google-translate')
const languages = require('./languages')
const { getMostRecentBrowserWindow } = require('sdk/window/utils')
const addonUnload = require('sdk/system/unload')
const windows = require('sdk/windows').browserWindows
const { viewFor } = require('sdk/view/core')

// Context Menu
const LABEL_LOADING = 'Fetching translation…'
Expand Down Expand Up @@ -110,9 +112,9 @@ const getSelectionFromWin = win => {
return (popupNode && getSelectionFromNode(popupNode)) || ''
}

// Addon loaded
const start = () => {
const win = getMostRecentBrowserWindow()
// Initialize the addon on a window
const start = win => {

const doc = win.document
const cmNode = doc.getElementById('contentAreaContextMenu')
const elt = eltCreator(doc)
Expand Down Expand Up @@ -229,12 +231,24 @@ const start = () => {

updateLangMenuChecks()

// Addon unloaded
addonUnload.when(() => {
return function destroy() {
cmNode.removeEventListener('popupshowing', onPopupshowing)
cmNode.removeEventListener('command', onContextCommand)
cmNode.removeChild(translateMenu)
})
}
}

start()
const destroyFns = []
const initWin = win => {
const destroy = start(viewFor(win))
if (destroy) destroyFns.push(destroy)
}

// Init an instance when a new window is opened
windows.on('open', initWin)

// Init new instances on startup
Array.from(windows).forEach(initWin)

// When the addon is unloaded, destroy all gtranslate instances
addonUnload.when(() => destroyFns.forEach(fn => fn()))

0 comments on commit 75257d0

Please sign in to comment.