Skip to content

Instantly share code, notes, and snippets.

@navaneeth
Created January 16, 2013 05:38
Show Gist options
  • Save navaneeth/4544929 to your computer and use it in GitHub Desktop.
Save navaneeth/4544929 to your computer and use it in GitHub Desktop.
Firefox addon-sdk - document.activeElement not available on some pages
(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');
}
}
}
})();
(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"));
})();
@ahmadmarafa
Copy link

thanks man , this helped me pretty much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment