index.js
1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
// Imports
let self = require('sdk/self');
let { Ci } = require('chrome');
let cm = require("sdk/context-menu");
let pm = require('sdk/page-mod');
let browser = require('sdk/window/utils').getMostRecentBrowserWindow();
//Globals
let app = {
window: null,
worker: null,
selectedText: ""
};
// Context menu
cm.Item({
label: "Traduzir para LIBRAS",
context: cm.SelectionContext(),
contentScript: 'self.on("context", function () {' +
' var text = window.getSelection().toString();' +
' if (text.length > 20)' +
' text = text.substr(0, 20) + "...";' +
' return "Traduzir \'" + text + "\' para LIBRAS";' +
'});' +
'self.on("click", function (node, data) {' +
' self.postMessage( window.getSelection().toString() );' +
'});',
onMessage: function(selectedText) {
app.selectedText = selectedText;
if ( !(app.window instanceof Ci.nsIDOMWindow) ) {
app.window = browser.open(
self.data.url('player/window.html'),
'VLibras Plugin',
'width=540,height=450,menubar=no,resizable=yes'
);
} else {
app.worker.port.emit('selectedText', app.selectedText);
app.window.focus();
}
}
});
// Page mod
pm.PageMod({
include: self.data.url('player/window.html'),
contentScriptWhen: 'end',
contentScriptFile: self.data.url('delegator.js'),
onAttach: function(worker) {
app.worker = worker;
app.worker.port.emit('selectedText', app.selectedText);
app.worker.on('detach', function () {
app.worker = undefined;
});
}
});