From 0ef7dbe253b3535c066f6b58573c2426d63ede5f Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 20 May 2019 19:23:16 +0200 Subject: [PATCH 1/7] Minimal working Firefox extension. --- browser-plugins/webextension/add_uri.html | 15 +++++++ browser-plugins/webextension/launch.js | 27 ++++++++++++ browser-plugins/webextension/manifest.json | 42 +++++++++++++++++++ .../native-wrapper/remwharead.json | 7 ++++ .../native-wrapper/remwharead_wrapper.sh | 5 +++ 5 files changed, 96 insertions(+) create mode 100644 browser-plugins/webextension/add_uri.html create mode 100644 browser-plugins/webextension/launch.js create mode 100644 browser-plugins/webextension/manifest.json create mode 100644 browser-plugins/webextension/native-wrapper/remwharead.json create mode 100755 browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh diff --git a/browser-plugins/webextension/add_uri.html b/browser-plugins/webextension/add_uri.html new file mode 100644 index 0000000..59e39af --- /dev/null +++ b/browser-plugins/webextension/add_uri.html @@ -0,0 +1,15 @@ + + + + + + +
+ + + +
+ + + + diff --git a/browser-plugins/webextension/launch.js b/browser-plugins/webextension/launch.js new file mode 100644 index 0000000..0cee334 --- /dev/null +++ b/browser-plugins/webextension/launch.js @@ -0,0 +1,27 @@ +var taburl; + +function onResponse(response) +{ + console.log(`Received ${response}`); +} + +function onError(error) +{ + console.log(`Error: ${error}`); +} + +function set_taburl(tabs) +{ + let tab = tabs[0]; + taburl = tab.url; +} + +function launch() +{ + let tags = document.getElementById("tags").value; + var sending = browser.runtime.sendNativeMessage("remwharead", "-t " + tags + " " + taburl); + sending.then(onResponse, onError); +} + +browser.tabs.query({currentWindow: true, active: true}).then(set_taburl); +add_uri.addEventListener("submit", launch); diff --git a/browser-plugins/webextension/manifest.json b/browser-plugins/webextension/manifest.json new file mode 100644 index 0000000..ab3369d --- /dev/null +++ b/browser-plugins/webextension/manifest.json @@ -0,0 +1,42 @@ +{ + "manifest_version": 2, + "name": "remwharead", + "version": "0.1.0", + + "description": "Integrates remwharead into your Browser.", + "homepage_url": "https://schlomp.space/tastytea/remwharead", + + "applications": + { + "gecko": + { + "id": "remwharead@tastytea.de", + "strict_min_version": "50.0" + } + }, + + "permissions": + [ + "activeTab", + "nativeMessaging" + ], + + "browser_action": + { + "default_title": "remwharead", + "default_popup": "add_uri.html", + "browser_style": true + }, + + "commands": + { + "open_popup": + { + "suggested_key": + { + "default": "Ctrl+Shift+S" + }, + "description": "Add URI to remwharead." + } + } +} diff --git a/browser-plugins/webextension/native-wrapper/remwharead.json b/browser-plugins/webextension/native-wrapper/remwharead.json new file mode 100644 index 0000000..ba77bf8 --- /dev/null +++ b/browser-plugins/webextension/native-wrapper/remwharead.json @@ -0,0 +1,7 @@ +{ + "name": "remwharead", + "description": "Saves URIs of things you want to remember in a database.", + "path": "/usr/lib/mozilla/native-messaging-hosts/remwharead_wrapper.sh", + "type": "stdio", + "allowed_extensions": [ "remwharead@tastytea.de" ] +} diff --git a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh new file mode 100755 index 0000000..c887aba --- /dev/null +++ b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +read args +args="$(echo ${args} | sed -E 's/^.+\"(.+)\"$/\1/')" # Remove length and quotes. +remwharead ${args} From 65c0bfc7ea100427719d3010605d8ef65d630cf3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 21 May 2019 09:42:58 +0200 Subject: [PATCH 2/7] Improved webextension and wrapper script. Got rid of the form, it was causing errors. Sending back a test message worked. --- CMakeLists.txt | 8 +++ README.adoc | 1 + browser-plugins/webextension/add_uri.html | 8 +-- browser-plugins/webextension/launch.js | 56 ++++++++++++++----- browser-plugins/webextension/manifest.json | 2 +- .../native-wrapper/remwharead_wrapper.sh | 21 ++++++- 6 files changed, 72 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24c7454..1f50309 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ project(remwharead set(WITH_MAN "YES" CACHE STRING "WITH_MAN defaults to \"YES\"") set(WITH_TESTS "NO" CACHE STRING "WITH_TESTS defaults to \"NO\"") +set(WITH_MOZILLA "NO" CACHE STRING "WITH_MOZILLA defaults to \"NO\"") include(GNUInstallDirs) find_package(PkgConfig REQUIRED) @@ -63,6 +64,13 @@ if (WITH_MAN) DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) endif() +if (WITH_MOZILLA) + install(FILES + browser-plugins/webextension/native-wrapper/remwharead.json + browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh + DESTINATION "${CMAKE_INSTALL_LIBDIR}/mozilla/native-messaging-hosts") +endif() + if(WITH_TESTS) add_library(${PROJECT_NAME}_testlib SHARED ${sources}) target_link_libraries(${PROJECT_NAME}_testlib ${COMMON_LIBRARIES}) diff --git a/README.adoc b/README.adoc index e24952a..8c2b3c8 100644 --- a/README.adoc +++ b/README.adoc @@ -96,6 +96,7 @@ cmake --build . * `-DCMAKE_BUILD_TYPE=Debug` for a debug build. * `-DWITH_MAN=NO` to not compile the manpage. * `WITH_TESTS=YES` to compile the tests. +* `WITH_MOZILLA=YES` to install the helper script for the Mozilla extension. * One of: ** `-DWITH_DEB=YES` if you want to be able to generate a deb-package. ** `-DWITH_RPM=YES` if you want to be able to generate an rpm-package. diff --git a/browser-plugins/webextension/add_uri.html b/browser-plugins/webextension/add_uri.html index 59e39af..2f46fb9 100644 --- a/browser-plugins/webextension/add_uri.html +++ b/browser-plugins/webextension/add_uri.html @@ -4,11 +4,9 @@ -
- - - -
+ + + diff --git a/browser-plugins/webextension/launch.js b/browser-plugins/webextension/launch.js index 0cee334..d90040a 100644 --- a/browser-plugins/webextension/launch.js +++ b/browser-plugins/webextension/launch.js @@ -1,27 +1,53 @@ var taburl; +var port; -function onResponse(response) -{ - console.log(`Received ${response}`); -} - -function onError(error) -{ - console.log(`Error: ${error}`); -} - -function set_taburl(tabs) +function set_taburl(tabs) // Set taburl to URL of current tab. { let tab = tabs[0]; taburl = tab.url; } -function launch() +function get_tags() // get tags from text input. { let tags = document.getElementById("tags").value; - var sending = browser.runtime.sendNativeMessage("remwharead", "-t " + tags + " " + taburl); - sending.then(onResponse, onError); + if (tags != "") + { + return "-t " + tags + " "; + } + return ""; } +function onResponse(response) { + console.log("Received " + response); +} + +function onError(error) { + console.log(`Error: ${error}`); +} + +function launch() // Launch wrapper and send tags + URL to stdin. +{ + var arguments = get_tags() + taburl; + console.log("Sending: " + arguments + " to remwharead"); + var sending = browser.runtime.sendNativeMessage("remwharead", arguments); + sending.then(onResponse, onError); + window.close(); +} + + +// Call set_taburl() with current tab. browser.tabs.query({currentWindow: true, active: true}).then(set_taburl); -add_uri.addEventListener("submit", launch); + +button.addEventListener("click", launch); // Call send() if submit is clicked. + +// Click button if enter is hit in text input. +document.querySelector("#tags").addEventListener( + "keyup", event => + { + if(event.key !== "Enter") + { + return; + } + document.querySelector("#button").click(); + event.preventDefault(); + }); diff --git a/browser-plugins/webextension/manifest.json b/browser-plugins/webextension/manifest.json index ab3369d..200a588 100644 --- a/browser-plugins/webextension/manifest.json +++ b/browser-plugins/webextension/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "remwharead", - "version": "0.1.0", + "version": "0.1.1", "description": "Integrates remwharead into your Browser.", "homepage_url": "https://schlomp.space/tastytea/remwharead", diff --git a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh index c887aba..911c07f 100755 --- a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh +++ b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh @@ -1,5 +1,20 @@ -#!/bin/sh +#!/bin/bash +args="" +start=0 + +# Read everything betweeen " and the next " into args. +while IFS= read -r -n1 c; do + if [[ ${start} -eq 0 ]]; then + [[ "${c}" == '"' ]] && start=1 + continue + fi + if [[ "$c" != '"' ]] ; then + args="${args}$c" + continue + fi + break; +done -read args -args="$(echo ${args} | sed -E 's/^.+\"(.+)\"$/\1/')" # Remove length and quotes. remwharead ${args} + +echo -en '\x06\x00\x00\x00''"test"' From de5d65d32aa529109a3ef1e610f4a64d0fd5d133 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 21 May 2019 13:05:17 +0200 Subject: [PATCH 3/7] Re-implemented wrapper in C**, added proper cmake recipe. --- CMakeLists.txt | 5 +- browser-plugins/webextension/launch.js | 2 +- browser-plugins/webextension/manifest.json | 2 +- .../native-wrapper/CMakeLists.txt | 7 ++ .../native-wrapper/remwharead.json | 2 +- .../native-wrapper/remwharead_wrapper.cpp | 91 +++++++++++++++++++ 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 browser-plugins/webextension/native-wrapper/CMakeLists.txt create mode 100644 browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f50309..7b58694 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,10 +65,7 @@ if (WITH_MAN) endif() if (WITH_MOZILLA) - install(FILES - browser-plugins/webextension/native-wrapper/remwharead.json - browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh - DESTINATION "${CMAKE_INSTALL_LIBDIR}/mozilla/native-messaging-hosts") + add_subdirectory(browser-plugins/webextension/native-wrapper) endif() if(WITH_TESTS) diff --git a/browser-plugins/webextension/launch.js b/browser-plugins/webextension/launch.js index d90040a..738d66f 100644 --- a/browser-plugins/webextension/launch.js +++ b/browser-plugins/webextension/launch.js @@ -18,7 +18,7 @@ function get_tags() // get tags from text input. } function onResponse(response) { - console.log("Received " + response); + console.log("Received: " + response); } function onError(error) { diff --git a/browser-plugins/webextension/manifest.json b/browser-plugins/webextension/manifest.json index 200a588..f6e1184 100644 --- a/browser-plugins/webextension/manifest.json +++ b/browser-plugins/webextension/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "remwharead", - "version": "0.1.1", + "version": "0.1.2", "description": "Integrates remwharead into your Browser.", "homepage_url": "https://schlomp.space/tastytea/remwharead", diff --git a/browser-plugins/webextension/native-wrapper/CMakeLists.txt b/browser-plugins/webextension/native-wrapper/CMakeLists.txt new file mode 100644 index 0000000..f8e49b9 --- /dev/null +++ b/browser-plugins/webextension/native-wrapper/CMakeLists.txt @@ -0,0 +1,7 @@ +set(MOZILLA_NMH_DIR "${CMAKE_INSTALL_LIBDIR}/mozilla/native-messaging-hosts") + +add_executable(${PROJECT_NAME}_wrapper ${PROJECT_NAME}_wrapper.cpp) + +install(TARGETS ${PROJECT_NAME}_wrapper DESTINATION ${MOZILLA_NMH_DIR}) +install(FILES remwharead.json remwharead_wrapper.sh + DESTINATION ${MOZILLA_NMH_DIR}) diff --git a/browser-plugins/webextension/native-wrapper/remwharead.json b/browser-plugins/webextension/native-wrapper/remwharead.json index ba77bf8..21cc13b 100644 --- a/browser-plugins/webextension/native-wrapper/remwharead.json +++ b/browser-plugins/webextension/native-wrapper/remwharead.json @@ -1,7 +1,7 @@ { "name": "remwharead", "description": "Saves URIs of things you want to remember in a database.", - "path": "/usr/lib/mozilla/native-messaging-hosts/remwharead_wrapper.sh", + "path": "/usr/lib/mozilla/native-messaging-hosts/remwharead_wrapper", "type": "stdio", "allowed_extensions": [ "remwharead@tastytea.de" ] } diff --git a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp new file mode 100644 index 0000000..4a04478 --- /dev/null +++ b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp @@ -0,0 +1,91 @@ +/* This file is part of remwharead. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +using std::string; +using std::cin; +using std::cout; +using std::uint32_t; +using std::system; + +const string read_input() +{ + string input; + char c; + + bool start = false; + while (cin.read(&c, 1).good()) + { + if (!start) + { + if (c == '"') + { + start = true; + } + continue; + } + if (c != '"') + { + input += c; + } + else + { + break; + } + } + return input; +} + +void send_message(const string &message) +{ + uint32_t length = message.length() + 2; + cout.write(reinterpret_cast(&length), sizeof(length)); + cout << '"' << message << '"'; +} + +int launch(const string &args) +{ + const string cmd = "remwharead " + args + " 2>/dev/null"; + int ret = system(cmd.c_str()); + if (WIFEXITED(ret)) + { + ret = WEXITSTATUS(ret); + } + + return ret; +} + +int main() +{ + const string args = read_input(); + int ret = launch(args); + + if (ret == 0) + { + send_message("Command successful."); + } + else + { + send_message("Command failed with status: " + std::to_string(ret) + '.'); + } + + return ret; +} From e05b154e21a87a82942ea932fe8e41261ffcc651 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 21 May 2019 13:30:53 +0200 Subject: [PATCH 4/7] Show error in popup. --- browser-plugins/webextension/add_uri.html | 1 + browser-plugins/webextension/launch.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/browser-plugins/webextension/add_uri.html b/browser-plugins/webextension/add_uri.html index 2f46fb9..ce42368 100644 --- a/browser-plugins/webextension/add_uri.html +++ b/browser-plugins/webextension/add_uri.html @@ -7,6 +7,7 @@ +
diff --git a/browser-plugins/webextension/launch.js b/browser-plugins/webextension/launch.js index 738d66f..0e68535 100644 --- a/browser-plugins/webextension/launch.js +++ b/browser-plugins/webextension/launch.js @@ -18,11 +18,21 @@ function get_tags() // get tags from text input. } function onResponse(response) { - console.log("Received: " + response); + console.log("Received: " + response); + if (response == "Command successful.") + { + window.close(); + } + else + { + document.getElementById("error").innerHTML = + "" + response + ""; + } + } function onError(error) { - console.log(`Error: ${error}`); + console.log(`Error: ${error}`); } function launch() // Launch wrapper and send tags + URL to stdin. @@ -31,7 +41,6 @@ function launch() // Launch wrapper and send tags + URL to stdin. console.log("Sending: " + arguments + " to remwharead"); var sending = browser.runtime.sendNativeMessage("remwharead", arguments); sending.then(onResponse, onError); - window.close(); } From 7a18b35856db73055b5c827b6ec3af4d59debff8 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 21 May 2019 13:59:35 +0200 Subject: [PATCH 5/7] Added icons. --- README.adoc | 4 ++++ browser-plugins/webextension/icon_dark.svg | 3 +++ browser-plugins/webextension/icon_light.svg | 3 +++ browser-plugins/webextension/manifest.json | 16 ++++++++++++++-- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 browser-plugins/webextension/icon_dark.svg create mode 100644 browser-plugins/webextension/icon_light.svg diff --git a/README.adoc b/README.adoc index 8c2b3c8..88d1ab6 100644 --- a/README.adoc +++ b/README.adoc @@ -106,6 +106,10 @@ You can run the tests with `ctest` inside the build directory. Install with == Copyright +The icons of the plugins are from the +https://github.com/PapirusDevelopmentTeam/papirus-icon-theme[Papirus icon +theme] with the license GPLv3. + ---- Copyright © 2019 tastytea . License GPLv3: GNU GPL version 3 . diff --git a/browser-plugins/webextension/icon_dark.svg b/browser-plugins/webextension/icon_dark.svg new file mode 100644 index 0000000..99621e3 --- /dev/null +++ b/browser-plugins/webextension/icon_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/browser-plugins/webextension/icon_light.svg b/browser-plugins/webextension/icon_light.svg new file mode 100644 index 0000000..e8d7cfb --- /dev/null +++ b/browser-plugins/webextension/icon_light.svg @@ -0,0 +1,3 @@ + + + diff --git a/browser-plugins/webextension/manifest.json b/browser-plugins/webextension/manifest.json index f6e1184..fa2dbd7 100644 --- a/browser-plugins/webextension/manifest.json +++ b/browser-plugins/webextension/manifest.json @@ -1,11 +1,16 @@ { "manifest_version": 2, "name": "remwharead", - "version": "0.1.2", + "version": "0.2.0", "description": "Integrates remwharead into your Browser.", "homepage_url": "https://schlomp.space/tastytea/remwharead", + "icons": + { + "16": "icon_dark.svg" + }, + "applications": { "gecko": @@ -25,7 +30,14 @@ { "default_title": "remwharead", "default_popup": "add_uri.html", - "browser_style": true + "browser_style": true, + "default_icon": "icon_dark.svg", + "theme_icons": + [{ // Dark means dark text in Firefox. 🙄 + "dark": "icon_light.svg", + "light": "icon_dark.svg", + "size": 16 + }] }, "commands": From f3ef0a79c4cb8779591104b0c5ebad9ef254fa9b Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 21 May 2019 15:01:42 +0200 Subject: [PATCH 6/7] Deleted shellscript-wrapper. --- .gitignore | 1 + browser-plugins/webextension/build_package.sh | 3 +++ .../native-wrapper/remwharead_wrapper.sh | 20 ------------------- 3 files changed, 4 insertions(+), 20 deletions(-) create mode 100755 browser-plugins/webextension/build_package.sh delete mode 100755 browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh diff --git a/.gitignore b/.gitignore index 84c048a..5f81c1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /build/ +*.zip diff --git a/browser-plugins/webextension/build_package.sh b/browser-plugins/webextension/build_package.sh new file mode 100755 index 0000000..df5a37c --- /dev/null +++ b/browser-plugins/webextension/build_package.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +zip -r -FS ../remwharead.zip * --exclude 'native-wrapper/*' --exclude build_package.sh diff --git a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh deleted file mode 100755 index 911c07f..0000000 --- a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash -args="" -start=0 - -# Read everything betweeen " and the next " into args. -while IFS= read -r -n1 c; do - if [[ ${start} -eq 0 ]]; then - [[ "${c}" == '"' ]] && start=1 - continue - fi - if [[ "$c" != '"' ]] ; then - args="${args}$c" - continue - fi - break; -done - -remwharead ${args} - -echo -en '\x06\x00\x00\x00''"test"' From a847ff7c482e9c9419316496be3cb4c8fb9ad80e Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 21 May 2019 15:02:10 +0200 Subject: [PATCH 7/7] Fixed linter errors and warnings. --- browser-plugins/webextension/add_uri.html | 2 +- browser-plugins/webextension/launch.js | 3 +-- browser-plugins/webextension/manifest.json | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/browser-plugins/webextension/add_uri.html b/browser-plugins/webextension/add_uri.html index ce42368..76a56d5 100644 --- a/browser-plugins/webextension/add_uri.html +++ b/browser-plugins/webextension/add_uri.html @@ -7,7 +7,7 @@ -
+ diff --git a/browser-plugins/webextension/launch.js b/browser-plugins/webextension/launch.js index 0e68535..b807b99 100644 --- a/browser-plugins/webextension/launch.js +++ b/browser-plugins/webextension/launch.js @@ -25,8 +25,7 @@ function onResponse(response) { } else { - document.getElementById("error").innerHTML = - "" + response + ""; + document.getElementById("error").textContent = response; } } diff --git a/browser-plugins/webextension/manifest.json b/browser-plugins/webextension/manifest.json index fa2dbd7..7f238fe 100644 --- a/browser-plugins/webextension/manifest.json +++ b/browser-plugins/webextension/manifest.json @@ -16,7 +16,7 @@ "gecko": { "id": "remwharead@tastytea.de", - "strict_min_version": "50.0" + "strict_min_version": "57.0" } },