After reading these two sites , I cannot get a piece of JS code to run from a context menu.
index.js:
var self = require('sdk/self');
var contextMenu = require("sdk/context-menu");
var uri = "";
var script = 'self.on("click", function (node, data) {' +
'var uri = "http://test.net/?blee=" + node.href + "blue=true";' +
'console.log(uri);'+
'tabs.open(uri);'+
'});';
var tabs = require('sdk/tabs');
var menuItem = contextMenu.Item({
label: "label",
context: contextMenu.SelectorContext("a[href]"),
contentScript: script,
onMessage: function (data) {
console.log(data);
}
});`
That's the entire extension right there and what's strange is that it outputs the var uri
to the console, but it fails at tabs.open(uri)
. Should tabs.open
be inside a function or something instead of this awkward script?
I'm attempting to write my chrome extension for firefox to do the exact same thing. Here's the code from the chrome extension
main.js:
runFunction= function(word){
var query = word.linkUrl;
chrome.tabs.create({url: "http://test.net/?blee=" + query + "&blue=True"});
};
chrome.contextMenus.create({
title: "Title!",
contexts:["link"],
onclick: runFunction
});
In the contextMenu script you self.postMessage
the uri
Then open the tab in the menuItem onMessage
function
var self = require('sdk/self');
var contextMenu = require("sdk/context-menu");
var uri = "";
var script = 'self.on("click", function (node, data) {' +
'var uri = "http://test.net/?blee=" + node.href + "blue=true";' +
'console.log(uri);'+
'self.postMessage(uri);'+
'});';
var tabs = require('sdk/tabs');
var menuItem = contextMenu.Item({
label: "label",
context: contextMenu.SelectorContext("a[href]"),
contentScript: script,
onMessage: function (data) {
tabs.open(data);
}
});
If the script for the context menus is getting cumbersome, put it in a file in the data
folder of your extension - it has to be data
folder
For example if it's named data/contextScript.js
, then, instead of
contentScript: script,
use
contentScriptFile: './contextScript.js',
note the lack of data
in the path
That syntax,
contentScriptFile: './filename.js'
is a shortcut for
contentScriptFile: self.data.url("filename.js")
Which is the syntax for earlier versions of jpm SDK
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.