/
main.js
113 lines (98 loc) · 3.51 KB
/
main.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const {Cc,Ci,Cr,Cu} = require("chrome");
Cu.import("resource://services-sync/main.js");
const contextMenu = require("sdk/context-menu");
const tabs = require("sdk/tabs");
const xulApp = require("sdk/system/xul-app");
function promptAndSendURIToDevice(uri, title) {
let clientsEngine = Weave.Service.clientsEngine || // for Firefox 19+
Weave.Clients; // for Firefox up-to 18
let remoteClients = clientsEngine._store._remoteClients;
let promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
// get a list of the remote client name and its id.
let labels = [];
let ids = [];
for (let id in remoteClients) {
labels.push(remoteClients[id].name);
ids.push(id);
}
labels.push("All devices");
ids.push(undefined);
if (labels.length > 1) {
let selected = {};
let result = promptService.select(null, "Send to device...", "Send \""+title+"\" to device:",
labels.length, labels, selected);
if (result) {
clientsEngine.sendURIToClientForDisplay(uri, ids[selected.value], title);
clientsEngine.sync();
}
} else {
promptService.alert(null, "Send to device...", "You need to configure Firefox Sync.");
}
}
let sendUri = null;
let sendPage = null;
function setupContextMenus() {
if (sendUri == null) {
sendUri = contextMenu.Item({
label: "Send this link to device...",
context: contextMenu.SelectorContext("a[href]"),
contentScript: 'self.on("click", function(node) {' +
' let title = document.title;' +
' let uri = node.href;' +
' self.postMessage([uri, title]);' +
'});',
onMessage: function onMessage(data) {
promptAndSendURIToDevice(data[0], data[1]);
}
});
}
if (sendPage == null) {
sendPage = contextMenu.Item({
label: "Send this page to device...",
context: contextMenu.PageContext(),
contentScript: 'self.on("click", function(node) {' +
' let title = document.title;' +
' let uri = document.URL;' +
' self.postMessage([uri, title]);' +
'});',
onMessage: function onMessage(data) {
promptAndSendURIToDevice(data[0], data[1]);
}
});
}
}
function releaseContextMenus() {
if (sendUri != null) {
sendUri.destroy();
sendUri = null;
}
if (sendPage != null) {
sendPage.destroy();
sendPage = null;
}
}
function handleDisplayURI(subject, topic, data) {
console.log("Received a URI for display!");
tabs.open(subject['uri']);
}
let handleReceived = false;
exports.main = function main(options, callbacks) {
console.log("Starting send tab to device add-on");
setupContextMenus();
if (xulApp.versionInRange(xulApp.version, "1", "15")) {
console.info("Application does not yet support displaying tabs. " +
"Installing handler.");
handleReceived = true;
Svc.Obs.add("weave:engine:clients:display-uri", handleDisplayURI);
}
};
exports.onUnload = function onUnload(reason) {
if (handleReceived) {
Svc.Obs.remove("weave:engine:clients:display-uri", handleDisplayURI);
}
releaseContextMenus();
};