简体   繁体   中英

contentScript: self.on('click', function() { tabs.open(url) }) - I can't open new tab here

In my menu item user see already translated selected text - this is doing in onMessage (and now I can't translate and open tab in this function - only translate), and I don't understand - how I can open new tab after user click on item - tabs.open not working at contentScript , even if at contentScript I call a function, or if I change to contentScriptFile . Code of my addon:

var contextMenu = require("sdk/context-menu");
var Request = require("sdk/request").Request;
var self = require('sdk/self');
var tabs = require('sdk/tabs');
var translating = 'translating...';

var menuItem = contextMenu.Item({
    label: translating,
    image: self.data.url('ico.png'),
    context: contextMenu.SelectionContext(),
    contentScript: 'self.on("context", function() {' +
                        'self.postMessage(window.getSelection().toString());' +
                        'return true;' +
                    '});' +
                    'self.on("click", function() {' +
                        'tabs.open("https://translate.yandex.by/?text=" + window.getSelection().toString());' + // this is not working here
                    '})',
    onMessage: function(selectionText) {
        menuItem.label = translating;
        Request({
            url: "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150402T173446Z.82a90fe78ca2aeaf.a3bd7c7a0f72b260e28f5d92e4f242cf6ba189d3&lang=ru&text="+selectionText,
            onComplete: function(response) {
                var translated = response.json.text[0];
                menuItem.label = translated;
            }
        }).get();
    }
});

You're really close. The 'tabs' object is only available in index.js - you need to think of the eval'd code you supply as the 'contentScript' property as a completely different environment. I've update the code, basically all that's different is I'm sending the yandex url back to index.js via postMessage so that I can open the tab in index.js:

var contextMenu = require("sdk/context-menu");
var Request = require("sdk/request").Request;
var self = require('sdk/self');
var tabs = require('sdk/tabs');
var translating = 'translating...';

var menuItem = contextMenu.Item({
    label: translating,
    image: self.data.url('ico.png'),
    context: contextMenu.SelectionContext(),
    contentScript: 'self.on("context", function() {' +
                        'var selectionText = window.getSelection().toString();' +
                        'self.postMessage({name:"context",data:selectionText});' +
                        'return true;' +
                    '});' +
                    'self.on("click", function() {' +
                        'var selectionText = window.getSelection().toString();' +
                        'self.postMessage({name:"click", data:"https://translate.yandex.by/?text=" + selectionText});' +
                    '})',
    onMessage: function(message) {
        if (message.name === 'context') {
            // handle the context message
            menuItem.label = translating;
            Request({
                url: "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150402T173446Z.82a90fe78ca2aeaf.a3bd7c7a0f72b260e28f5d92e4f242cf6ba189d3&lang=ru&text="+selectionText,
                onComplete: function(response) {
                    var translated = response.json.text[0];
                    menuItem.label = translated;
                }
            }).get();
        }
        else if (message.name === 'click') {
            // handle the click message
            tabs.open(message.data);
        }
    }
});

See the SDK's extensive documentation on how content scripts work for more info on this topic.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM