Created
January 16, 2013 05:38
-
-
Save navaneeth/4544929 to your computer and use it in GitHub Desktop.
Firefox addon-sdk - document.activeElement not available on some pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
self.port.on('init', init); | |
function init(data) { | |
var active = document.activeElement; | |
if (!active) { | |
console.log("Failed to get activeElement"); | |
} | |
if (active != document.body) { | |
console.log("got proper activeElement"); | |
console.log("type = " + document.activeElement.type); | |
console.log("id = " + document.activeElement.id); | |
} | |
else { | |
console.log("activeElement is not available"); | |
} | |
console.log("data = " + JSON.stringify(data)); | |
if (data.id != "") { | |
var elementById = document.getElementById(data.id); | |
if (elementById == null) { | |
console.log("failed to get document.getElementById()"); | |
} | |
else { | |
console.log('document.getElementById() is working'); | |
} | |
} | |
} | |
})(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var data = require("self").data, | |
contextMenu = require("context-menu"), | |
tabs = require('tabs'); | |
var workers = []; | |
var pageMod = require("page-mod"); | |
var page = pageMod.PageMod({ | |
include: '*', | |
contentScriptWhen: 'ready', | |
contentScriptFile: data.url('cs.js'), | |
onAttach: function(worker) { | |
workers.push(worker); | |
worker.on("detach", function() { | |
var index = workers.indexOf(worker); | |
if (index >= 0) workers.splice(index, 1); | |
}); | |
} | |
}); | |
function createContextMenu(kontext) { | |
var english = contextMenu.Item({ | |
label: "English", | |
data: 'en' | |
}), | |
french = contextMenu.Item({ | |
label: "French", | |
data: 'fr' | |
}); | |
var searchMenu = contextMenu.Menu({ | |
label: "My Addon", | |
context: kontext, | |
contentScriptWhen: 'ready', | |
contentScript: "self.on('click', function(node, data) {self.postMessage({'data': data, 'id': node.id});});", | |
items: [english, french], | |
onMessage: function(data) { | |
var worker = getActiveWorker(); | |
if (worker) { | |
worker.port.emit('init', data); | |
} | |
} | |
}); | |
return searchMenu; | |
}; | |
function getActiveWorker() { | |
for (var i = 0; i < workers.length; i++) { | |
if (workers[i].tab === tabs.activeTab) { | |
return workers[i]; | |
}; | |
} | |
return undefined; | |
} | |
var searchMenu = createContextMenu(contextMenu.SelectorContext("textarea, input")); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks man , this helped me pretty much