Jump to content

Code not working


jimfog

Recommended Posts

I am trying to make a mozilla addon but i have problem with the code.I cannot understand why it is not working.It is a jetpack in which i want a menu item to appear in the context menu upon testing an if statement.It is comprised of 2 files, main.js and my-content-script.jsmain.js:

Code: var contextMenu = require("context-menu"); var selection = require("selection"); var item = contextMenu.Item({ label: "Alert", context: contextMenu.SelectionContext(), contentScript: require("self").data.url("my-content-script.js") }); item.port.on("item-click", function() { var selection = require("selection"); var x=selection.text; if (typeof x=="string") {alert('hi'); } });
my-content-script.js:
Code: self.on("item-click", function() { self.port.emit("item-click"); });
I am posting in this forum because it is been very helpful so far.Of course it would be easier for someone to understand the code if he is on to mozilla addon development.The 2 files communicate with each other.I want to accomplish the following, if the selection of a text the user makes is a string then an alert message will appear saying hi.This if statement will be tested when the user clicks a context menu item named alert(the context menu, of course, appears, upon right clicking).I hope you will be able to understand the code because i am frustrated.
Link to comment
Share on other sites

You didn't say what the problem is, but when you're using the typeof operator you should include parentheses.if ((typeof x)=="string") {alert('hi');You may also want to alert (typeof x) to see what it actually is. Other than that, a description of the problem helps determine the solution.

Link to comment
Share on other sites

You didn't say what the problem is, but when you're using the typeof operator you should include parentheses.if ((typeof x)=="string") {alert('hi');You may also want to alert (typeof x) to see what it actually is. Other than that, a description of the problem helps determine the solution.
I tried your code and it is still not working.But let me try and explain what i want to do:1.The user selects text from the page2. Then, he right-clicks on the selected text and the context menu appears with an item named "Alert".3. The user goes to choose the "alert" item.,4. If the selection he made is a string that he should receive an alert box saying "Hi.5. In the opposite case, the selection is not a string, well, i have not write code yet for that.My sole purpose, with the above scenario, is to make an if statement work and nothing else.It might not make any sense as an application, but its purpose is not to provide any functionality at all, i am just trying to learn javascript and use it in mozilla addon development.Well, did you understand what i am trying to accomplish here?I hope you got it, because i am stuck here for days. I have tried various code combinations and i still cannot get it to work.
Link to comment
Share on other sites

Is your item appearing in the context menu?
Νο, it does not even appear.Here is the code once more:The code in the filemain.js
var contextMenu = require("context-menu");var selection = require("selection");var item = contextMenu.Item({  label: "Alert",  context: contextMenu.SelectionContext(),    contentScript: require("self").data.url("my-content-script.js")  });item.port.on("click", function() {	var selection = require("selection");	var x=selection.text;	if ((typeof x)=="string") {alert('hi');	}});

and the code in the file my-content-script.js

self.on("click", function() {self.port.emit("click");});

Link to comment
Share on other sites

It sounds like you're creating the item, but you probably need to pass the item as a parameter to another function to append it to the context menu.I don't have the reference with me, but it should look something like this:contextMenu.appendItem(item);If you can find a reference for the Firefox context menu functions, see if there's one that appends items to it.

Link to comment
Share on other sites

It sounds like you're creating the item, but you probably need to pass the item as a parameter to another function to append it to the context menu.I don't have the reference with me, but it should look something like this:contextMenu.appendItem(item);If you can find a reference for the Firefox context menu functions, see if there's one that appends items to it.
Ok, i will check your code but i have one last question though.In the mozilla documentation i see often(in the example code segments) the .on statement.For example:
self.on("context", function() {  // Handle the event});

Is this a javascript property?Thanks

Link to comment
Share on other sites

Ok, i will check your code but i have one last question though.In the mozilla documentation i see often(in the example code segments) the .on statement.For example:
self.on("context", function() {  // Handle the event});

Is this a javascript property?Thanks

It's not native to Javascript. That's specific to Mozilla's objects. It looks like it's meant to handle internal events.
Link to comment
Share on other sites

It's not native to Javascript. That's specific to Mozilla's objects. It looks like it's meant to handle internal events.
I thought so too.Thanks, i must experiment with the code and try to make this thing work.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...