aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-10-27 23:38:11 +1100
committerYuchen Pei <id@ypei.org>2023-10-27 23:38:11 +1100
commit8ee5aba1bd932f248297f8de3eb7bbcdae601bb4 (patch)
treeefb5c4cccf0fa689c00559adc719b711092d02b2
parent8926cb68f45ab3dc0bd7bc7eed2cd40c4cc437cd (diff)
[PoC] Log LibreJS reportsnative-messaging-app
- run ./build.sh - install the built librejs.xpi as a temporary extension[1] - check the librejs version is 7.21.2 - load some sites e.g. <https://fsf.org>, <https://archive.org>, and the reports should be logged into /tmp/librejs.log [1] https://extensionworkshop.com/documentation/develop/temporary-installation-in-firefox/
-rwxr-xr-xbuild.sh3
-rw-r--r--main_background.js6
-rw-r--r--manifest.json3
-rwxr-xr-xnative-apps/librejs-logger.py53
-rw-r--r--native-apps/librejs.json7
5 files changed, 71 insertions, 1 deletions
diff --git a/build.sh b/build.sh
index 526ac8b..96d8800 100755
--- a/build.sh
+++ b/build.sh
@@ -42,3 +42,6 @@ cd ../
rm -r ./build_temp
# change the zip file to a xpi file that can be uploaded
mv librejs.zip librejs.xpi
+
+cp native-apps/librejs.json $HOME/.mozilla/native-messaging-hosts/
+cp native-apps/librejs-logger.py /tmp/
diff --git a/main_background.js b/main_background.js
index 87b80ae..18e5818 100644
--- a/main_background.js
+++ b/main_background.js
@@ -30,6 +30,10 @@ const { makeDebugLogger } = require('./common/debug.js');
const PRINT_DEBUG = false;
const dbgPrint = makeDebugLogger('main_background.js', PRINT_DEBUG, Date.now());
+const appPort = browser.runtime.connectNative("librejs");
+appPort.onMessage.addListener((response) => {
+ console.log(`Received: ${response}`);
+});
/*
*
@@ -422,6 +426,8 @@ async function checkScriptAndUpdateReport(scriptSrc, url, tabId, whitelisted, is
// Updates the extension icon in the toolbar.
function updateBadge(tabId, report = null, forceRed = false) {
+ if (report)
+ appPort.postMessage(report);
const blockedCount = report ? report.blocked.length + report.blacklisted.length : 0;
const [text, color] = blockedCount > 0 || forceRed
? [blockedCount && blockedCount.toString() || '!', 'red'] : ['✓', 'green']
diff --git a/manifest.json b/manifest.json
index 73bc918..4301a07 100644
--- a/manifest.json
+++ b/manifest.json
@@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "GNU LibreJS",
"short_name": "LibreJS",
- "version": "7.21.1",
+ "version": "7.21.2",
"author": "various",
"description": "Only allows free and/or trivial Javascript to run.",
"applications": {
@@ -15,6 +15,7 @@
"64": "icons/librejs.png"
},
"permissions": [
+ "nativeMessaging",
"contextMenus",
"webRequest",
"webRequestBlocking",
diff --git a/native-apps/librejs-logger.py b/native-apps/librejs-logger.py
new file mode 100755
index 0000000..be79409
--- /dev/null
+++ b/native-apps/librejs-logger.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env -S python3 -u
+
+# Note that running python with the `-u` flag is required on Windows,
+# in order to ensure that stdin and stdout are opened in binary, rather
+# than text, mode.
+
+import sys
+import json
+import struct
+import subprocess
+import os
+import logging
+
+# Read a message from stdin and decode it.
+def getMessage():
+ rawLength = sys.stdin.buffer.read(4)
+ if len(rawLength) == 0:
+ sys.exit(0)
+ messageLength = struct.unpack('@I', rawLength)[0]
+ message = sys.stdin.buffer.read(messageLength).decode('utf-8')
+ return json.loads(message)
+
+# Encode a message for transmission,
+# given its content.
+def encodeMessage(messageContent):
+ # https://docs.python.org/3/library/json.html#basic-usage
+ # To get the most compact JSON representation, you should specify
+ # (',', ':') to eliminate whitespace.
+ # We want the most compact representation because the browser rejects # messages that exceed 1 MB.
+ encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8')
+ encodedLength = struct.pack('@I', len(encodedContent))
+ return {'length': encodedLength, 'content': encodedContent}
+
+# Send an encoded message to stdout
+def sendMessage(encodedMessage):
+ sys.stdout.buffer.write(encodedMessage['length'])
+ sys.stdout.buffer.write(encodedMessage['content'])
+ sys.stdout.buffer.flush()
+
+
+counter = 0
+logging.basicConfig(filename="/tmp/librejs.log",
+ filemode='a',
+ format='%(asctime)s.%(msecs)d %(name)s %(levelname)s %(message)s',
+ datefmt='%H:%M:%S',
+ level=logging.DEBUG)
+
+while True:
+ receivedMessage = getMessage()
+ devnull = open(os.devnull, 'w')
+ counter += 1
+ logging.info(f'[{counter}] {receivedMessage}')
+ sendMessage(encodeMessage(f'Logged message No. {counter}'))
diff --git a/native-apps/librejs.json b/native-apps/librejs.json
new file mode 100644
index 0000000..b927b76
--- /dev/null
+++ b/native-apps/librejs.json
@@ -0,0 +1,7 @@
+{
+ "name": "librejs",
+ "description": "Logging LibreJS reports",
+ "path": "/tmp/librejs-logger.py",
+ "type": "stdio",
+ "allowed_extensions": ["jid1-KtlZuoiikVfFew@jetpack"]
+}