File indexing completed on 2024-04-28 05:32:49

0001 /*
0002     Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     This program is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU General Public License as
0006     published by the Free Software Foundation; either version 3 of
0007     the License, or (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 var kdeConnectMenuIdPrefix = "kdeconnect_page_";
0019 var kdeConnectDevices = {};
0020 
0021 chrome.contextMenus.onClicked.addListener(function (info) {
0022     if (!info.menuItemId.startsWith(kdeConnectMenuIdPrefix)) {
0023         return;
0024     }
0025 
0026     const deviceId = info.menuItemId.substr(info.menuItemId.indexOf("@") + 1);
0027 
0028     var url = info.linkUrl || info.srcUrl || info.pageUrl;
0029     console.log("Send url", url, "to kdeconnect device", deviceId);
0030     if (!url) {
0031         return;
0032     }
0033 
0034     port.postMessage({
0035         subsystem: "kdeconnect",
0036         event: "shareUrl",
0037         url: url,
0038         deviceId: deviceId
0039     });
0040 });
0041 
0042 let knownKdeConnectMenuEntryIds = new Set();
0043 const createKdeConnectMenuEntry = (args) => {
0044     const id = kdeConnectMenuIdPrefix + args.key + "@" + args.deviceId;
0045 
0046     let props = {
0047         id,
0048         contexts: args.contexts,
0049         title: args.title
0050     };
0051 
0052     if (IS_FIREFOX && args.iconName) {
0053         props.icons = {
0054             "16": "icons/" + args.iconName + ".svg"
0055         };
0056     }
0057 
0058     if (args.args) {
0059         Object.keys(args.args).forEach((key) => {
0060             props[key] = args.args[key];
0061         });
0062     }
0063 
0064     chrome.contextMenus.create(props);
0065     knownKdeConnectMenuEntryIds.add(id);
0066 };
0067 
0068 addCallback("kdeconnect", "deviceAdded", function(message) {
0069     const deviceId = message.id;
0070     const name = message.name;
0071     const type = message.type;
0072 
0073     let iconName = "";
0074     switch (type) {
0075     case "smartphone":
0076     case "phone":
0077         iconName = "smartphone-symbolic";
0078         break;
0079     case "tablet":
0080         iconName = "tablet-symbolic";
0081         break;
0082     case "desktop":
0083     case "tv": // at this size you can't really tell desktop monitor icon from a TV
0084         iconName = "computer-symbolic";
0085         break;
0086     case "laptop":
0087         iconName = "computer-laptop-symbolic";
0088         break;
0089     }
0090 
0091     const httpPatterns = ["http://*/*", "https://*/*"];
0092 
0093     createKdeConnectMenuEntry({
0094         deviceId,
0095         iconName,
0096         key: "open_link",
0097         contexts: ["link", "image", "audio", "video"],
0098         title: chrome.i18n.getMessage("kdeconnect_open_device", name),
0099         args: {
0100             targetUrlPatterns: httpPatterns
0101         }
0102     });
0103 
0104     createKdeConnectMenuEntry({
0105         deviceId,
0106         iconName,
0107         key: "open_page",
0108         contexts: ["page"],
0109         title: chrome.i18n.getMessage("kdeconnect_open_device", name),
0110         args: {
0111             documentUrlPatterns: httpPatterns
0112         }
0113     });
0114 
0115     // Entry on tel: phone links
0116     createKdeConnectMenuEntry({
0117         deviceId,
0118         iconName: "call-start-symbolic",
0119         key: "call",
0120         contexts: ["link"],
0121         title: chrome.i18n.getMessage("kdeconnect_call_device", name),
0122         args: {
0123             targetUrlPatterns: [
0124                 "tel:*"
0125             ]
0126         }
0127     });
0128 
0129     try {
0130         // Entry on a tab in the tab bar (Firefox)
0131         createKdeConnectMenuEntry({
0132             deviceId,
0133             iconName,
0134             key: "open_tab",
0135             contexts: ["tab"],
0136             title: chrome.i18n.getMessage("kdeconnect_open_device", name),
0137             args: {
0138                 documentUrlPatterns: httpPatterns
0139             }
0140         });
0141     } catch (e) {
0142         console.warn("Failed to create 'tab' context menu", e);
0143     }
0144 
0145     kdeConnectDevices[deviceId] = {
0146         name, type
0147     };
0148 });
0149 
0150 addCallback("kdeconnect", "deviceRemoved", function(message) {
0151     let deviceId = message.id;
0152 
0153     if (!kdeConnectDevices[deviceId]) {
0154         return;
0155     }
0156 
0157     delete kdeConnectDevices[deviceId];
0158 
0159     for (let id of knownKdeConnectMenuEntryIds) {
0160         chrome.contextMenus.remove(id);
0161     }
0162     knownKdeConnectMenuEntryIds.clear();
0163 });