update readme
[dotfiles/.git] / .config / BraveSoftware / Brave-Browser / Default / Extensions / cimiefiiaegbelhefglklhhakcgmhkai / 1.7.6_0 / extension-kdeconnect.js
1 /*
2     Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de>
3
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License as
6     published by the Free Software Foundation; either version 3 of
7     the License, or (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 var kdeConnectMenuIdPrefix = "kdeconnect_page_";
19 var kdeConnectDevices = {};
20
21 chrome.contextMenus.onClicked.addListener(function (info) {
22     if (!info.menuItemId.startsWith(kdeConnectMenuIdPrefix)) {
23         return;
24     }
25
26     const deviceId = info.menuItemId.substr(info.menuItemId.indexOf("@") + 1);
27
28     var url = info.linkUrl || info.srcUrl || info.pageUrl;
29     console.log("Send url", url, "to kdeconnect device", deviceId);
30     if (!url) {
31         return;
32     }
33
34     port.postMessage({
35         subsystem: "kdeconnect",
36         event: "shareUrl",
37         url: url,
38         deviceId: deviceId
39     });
40 });
41
42 addCallback("kdeconnect", "deviceAdded", function(message) {
43     let deviceId = message.id;
44     let name = message.name;
45     let type = message.type;
46
47     let props = {
48         id: kdeConnectMenuIdPrefix + "open@" + deviceId,
49         contexts: ["link", "page", "image", "audio", "video"],
50         title: chrome.i18n.getMessage("kdeconnect_open_device", name),
51         targetUrlPatterns: [
52             "http://*/*", "https://*/*"
53         ]
54     };
55
56     if (IS_FIREFOX) {
57         let iconName = "";
58         switch (type) {
59             case "smartphone":
60             case "phone":
61                 iconName = "smartphone-symbolic";
62                 break;
63             case "tablet":
64                 iconName = "tablet-symbolic";
65                 break;
66             case "desktop":
67             case "tv": // at this size you can't really tell desktop monitor icon from a TV
68                 iconName = "computer-symbolic";
69                 break;
70             case "laptop":
71                 iconName = "computer-laptop-symbolic";
72                 break;
73         }
74
75         if (iconName) {
76             props.icons = {
77                 "16": "icons/" + iconName + ".svg"
78             };
79         }
80     }
81
82     chrome.contextMenus.create(props);
83
84     props = {
85         id: kdeConnectMenuIdPrefix + "call@" + deviceId,
86         contexts: ["link"],
87         title: chrome.i18n.getMessage("kdeconnect_call_device", name),
88         targetUrlPatterns: [
89             "tel:*"
90         ]
91     };
92
93     if (IS_FIREFOX) {
94         props.icons = {
95             "16": "icons/call-start-symbolic.svg"
96         };
97     }
98
99     chrome.contextMenus.create(props);
100
101     kdeConnectDevices[deviceId] = {
102         name, type
103     };
104 });
105
106 addCallback("kdeconnect", "deviceRemoved", function(message) {
107     let deviceId = message.id;
108
109     if (!kdeConnectDevices[deviceId]) {
110         return;
111     }
112
113     delete kdeConnectDevices[deviceId];
114     chrome.contextMenus.remove(kdeConnectMenuIdPrefix + "open@" + deviceId);
115     chrome.contextMenus.remove(kdeConnectMenuIdPrefix + "call@" + deviceId);
116 });