diff --git a/extension/_locales/en/messages.json b/extension/_locales/en/messages.json --- a/extension/_locales/en/messages.json +++ b/extension/_locales/en/messages.json @@ -103,14 +103,14 @@ "message": "If you like what you saw, please consider donating to KDE, so we can continue to make the best free software possible." }, - "kdeconnect_open_via": { - "description": "Context menu, open link on device whose name we don't (yet) know", - "message": "Open via KDE Connect" - }, "kdeconnect_open_device": { "description": "Context menu, open link on device $1, similar to 'Open in New Tab'", "message": "Open on '$1'" }, + "kdeconnect_copy_device": { + "description": "Context menu, copy text to device $1 so it can be pasted there", + "message": "Copy to '$1'" + }, "general_error_title": { "description": "Title message for most error notifications", diff --git a/extension/extension.js b/extension/extension.js --- a/extension/extension.js +++ b/extension/extension.js @@ -140,44 +140,61 @@ // ------------------------------------------------------------------------ // -var kdeConnectMenuIdPrefix = "kdeconnect_page_"; +var kdeConnectOpenMenuIdPrefix = "kdeconnect_open_"; +var kdeConnectCopyMenuIdPrefix = "kdeconnect_copy_"; var kdeConnectDevices = []; chrome.contextMenus.onClicked.addListener(function (info) { - if (!info.menuItemId.startsWith(kdeConnectMenuIdPrefix)) { + let menuId = info.menuItemId; + if (!menuId.startsWith(kdeConnectOpenMenuIdPrefix) + && !menuId.startsWith(kdeConnectCopyMenuIdPrefix)) { return; } - var deviceId = info.menuItemId.substr(kdeConnectMenuIdPrefix.length); + let url = info.linkUrl || info.srcUrl || info.pageUrl; + let selection = info.selectionText; + if (!url && !selection) { + return; + } + + let deviceId = ""; + if (menuId.startsWith(kdeConnectOpenMenuIdPrefix)) { + deviceId = menuId.substr(kdeConnectOpenMenuIdPrefix.length); + } else if (menuId.startsWith(kdeConnectCopyMenuIdPrefix)) { + deviceId = menuId.substr(kdeConnectCopyMenuIdPrefix.length); + } - var url = info.linkUrl || info.pageUrl; - console.log("Send url", url, "to kdeconnect device", deviceId); - if (!url) { + if (!deviceId) { // shouldn't happen + console.warn("No device id for menu", menuId); return; } port.postMessage({ subsystem: "kdeconnect", event: "shareUrl", url: url, + text: selection, deviceId: deviceId }); }); addCallback("kdeconnect", "deviceAdded", function(message) { - var id = message.id; - var name = message.name; - - var menuEntryTitle = chrome.i18n.getMessage("kdeconnect_open_device", name); - var menuId = kdeConnectMenuIdPrefix + id; + let deviceId = message.id; + let deviceName = message.name; chrome.contextMenus.create({ - id: menuId, - contexts: ["link", "page"], - title: menuEntryTitle, + id: kdeConnectOpenMenuIdPrefix + deviceId, + contexts: ["link", "page", "image", "audio", "video"], + title: chrome.i18n.getMessage("kdeconnect_open_device", deviceName) + }); + // Create a separate menu item for "copy to", so copying text isn't "open on" + chrome.contextMenus.create({ + id: kdeConnectCopyMenuIdPrefix + deviceId, + contexts: ["selection"], + title: chrome.i18n.getMessage("kdeconnect_copy_device", deviceName) }); - kdeConnectDevices.push(id); + kdeConnectDevices.push(deviceId); }); addCallback("kdeconnect", "deviceRemoved", function(message) { diff --git a/host/kdeconnectplugin.cpp b/host/kdeconnectplugin.cpp --- a/host/kdeconnectplugin.cpp +++ b/host/kdeconnectplugin.cpp @@ -178,14 +178,19 @@ if (event == QLatin1String("shareUrl")) { const QString deviceId = json.value(QStringLiteral("deviceId")).toString(); const QString url = json.value(QStringLiteral("url")).toString(); + const QString text = json.value(QStringLiteral("text")).toString(); - debug() << "sending kde connect url" << url << "to device" << deviceId; + debug() << "sending kde connect url" << url << "or text" << text << "to device" << deviceId; QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect/devices/") + deviceId + QStringLiteral("/share"), QStringLiteral("org.kde.kdeconnect.device.share"), - QStringLiteral("shareUrl")); - msg.setArguments({url}); + !text.isEmpty() ? QStringLiteral("shareText") : QStringLiteral("shareUrl")); + if (!text.isEmpty()) { + msg.setArguments({text}); + } else { + msg.setArguments({url}); + } QDBusConnection::sessionBus().call(msg, QDBus::NoBlock); } }