From 72e389bde47b1b3228432eb008098ea070a43a5b Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 19 Apr 2023 23:31:44 -0600 Subject: [PATCH] tig-spy MVP --- projects/tig-spy/manifest.json | 2 +- projects/tig-spy/package-lock.json | 82 ++++++++++++++ projects/tig-spy/src/tig_spy/Background.hx | 1 + projects/tig-spy/src/tig_spy/Background.kiss | 27 +++-- projects/tig-spy/src/tig_spy/Main_.kiss | 107 ++++++++++++------- projects/tig-spy/src/tig_spy/Message.hx | 6 ++ projects/tig-spy/src/tig_spy/Util.kiss | 13 --- 7 files changed, 178 insertions(+), 60 deletions(-) create mode 100644 projects/tig-spy/package-lock.json create mode 100644 projects/tig-spy/src/tig_spy/Message.hx delete mode 100644 projects/tig-spy/src/tig_spy/Util.kiss diff --git a/projects/tig-spy/manifest.json b/projects/tig-spy/manifest.json index 56a17a05..565e6bb3 100644 --- a/projects/tig-spy/manifest.json +++ b/projects/tig-spy/manifest.json @@ -17,7 +17,7 @@ "bin/main.js" ], "matches": [ - "*://forums.tigsource.com/index.php?topic=*.0" + "*://forums.tigsource.com/index.php?topic=*" ] } ], diff --git a/projects/tig-spy/package-lock.json b/projects/tig-spy/package-lock.json new file mode 100644 index 00000000..c77d9c41 --- /dev/null +++ b/projects/tig-spy/package-lock.json @@ -0,0 +1,82 @@ +{ + "name": "tig-spy", + "version": "0.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "tig-spy", + "version": "0.0.0", + "hasInstallScript": true, + "dependencies": { + "@types/webextension-polyfill": "^0.10.0", + "webextension-polyfill": "^0.10.0" + }, + "devDependencies": { + "dts2hx": "^0.19.0" + } + }, + "node_modules/@types/webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==" + }, + "node_modules/dts2hx": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/dts2hx/-/dts2hx-0.19.2.tgz", + "integrity": "sha512-1+X3kOB941APt/O6b5jX9+XMefRPqvFk4fvGDHPLIbuYdIAjNwcX3XOuZJ9fYtUD7Odn4ACPfK2ql2HWaFrLrg==", + "dev": true, + "dependencies": { + "typescript": "3.7.4" + }, + "bin": { + "dts2hx": "cli.js" + } + }, + "node_modules/typescript": { + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, + "node_modules/webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g==" + } + }, + "dependencies": { + "@types/webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==" + }, + "dts2hx": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/dts2hx/-/dts2hx-0.19.2.tgz", + "integrity": "sha512-1+X3kOB941APt/O6b5jX9+XMefRPqvFk4fvGDHPLIbuYdIAjNwcX3XOuZJ9fYtUD7Odn4ACPfK2ql2HWaFrLrg==", + "dev": true, + "requires": { + "typescript": "3.7.4" + } + }, + "typescript": { + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", + "dev": true + }, + "webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g==" + } + } +} diff --git a/projects/tig-spy/src/tig_spy/Background.hx b/projects/tig-spy/src/tig_spy/Background.hx index e74376b7..148ef643 100644 --- a/projects/tig-spy/src/tig_spy/Background.hx +++ b/projects/tig-spy/src/tig_spy/Background.hx @@ -3,6 +3,7 @@ package tig_spy; import kiss.Prelude; import kiss_firefox.API; import haxe.Constraints; +import tig_spy.Message; @:build(kiss.Kiss.build()) class Background {} \ No newline at end of file diff --git a/projects/tig-spy/src/tig_spy/Background.kiss b/projects/tig-spy/src/tig_spy/Background.kiss index bafa6b5c..1ea0a7ce 100644 --- a/projects/tig-spy/src/tig_spy/Background.kiss +++ b/projects/tig-spy/src/tig_spy/Background.kiss @@ -1,7 +1,22 @@ -(load "Util.kiss") +(var &mut :Array collectedAuthors []) +(var &mut :Array pages []) -(API.browser.runtime.onMessage.addListener ->[message sender sendResponse] { - (let [:Function sendResponse (cast sendResponse)] - (sendResponse message)) - true -}) \ No newline at end of file +(function sendNextPage [:Function sendResponse] + (awaitLet [tabs (API.browser.tabs.query (object active true)) + &sync id .id (first tabs)] + (if pages + (API.browser.tabs.sendMessage id (object type "nextPage" data [(pages.shift)])) + (API.browser.tabs.sendMessage id (object type "authorList" data collectedAuthors))))) + +(API.browser.runtime.onMessage.addListener ->[:Message message sender sendResponse] + (let [:Function sendResponse sendResponse] + (case message + ((object type "authors" data authors) + (set collectedAuthors (collectedAuthors.concat authors)) + (sendNextPage sendResponse)) + ((object type "pages" data pageUrls) + (set collectedAuthors []) + (set pages pageUrls) + (sendResponse "")) + (never otherwise)) + true)) diff --git a/projects/tig-spy/src/tig_spy/Main_.kiss b/projects/tig-spy/src/tig_spy/Main_.kiss index 60e7b8b1..dfeb8532 100644 --- a/projects/tig-spy/src/tig_spy/Main_.kiss +++ b/projects/tig-spy/src/tig_spy/Main_.kiss @@ -1,7 +1,6 @@ (import kiss_firefox.API) (import js.lib.Promise) - -(load "Util.kiss") +(import tig_spy.Message) (var :Dynamic document js.Lib.global.document) (var :Dynamic window js.Lib.global.window) @@ -11,7 +10,35 @@ (set element.innerHTML text) element)) +(let [analyticsDiv (document.createElement "div")] + (document.body.prepend analyticsDiv) + (set Prelude.printStr ->s (analyticsDiv.appendChild (p s)))) + +(function postElements [:Dynamic document] + (let [postTable (.item (document.getElementsByClassName "bordercolor") 0) + postTableBody (postTable.children.item 0) + tableRows (for i (range postTableBody.children.length) (postTableBody.children.item i))] + // There is an empty row at the end: + (tableRows.pop) + tableRows)) + +(function postAuthors [:Dynamic document] + (API.browser.runtime.sendMessage + (object + type "authors" + data + (for [idx post] (enumerate (postElements document)) + (let [links (post.getElementsByTagName "a") + userLink (links.item (min idx 1))] + userLink.innerHTML))))) + (function printPostStats [:Array authors] + (let [:String subjectLine .innerHTML (document.getElementById "top_subject") + lastLeftParenIndex (subjectLine.lastIndexOf "(") + lastRightParenIndex (subjectLine.lastIndexOf ")") + [Read n times] (.split (substr subjectLine lastLeftParenIndex lastRightParenIndex) " ")] + (print "$n views")) + (let [op (first authors) :Map repliers (new Map) &mut updates 0 @@ -26,44 +53,44 @@ (print "$updates updates by OP") (print "$replies replies by $(count repliers) followers"))) -(let [analyticsLink (document.createElement "a")] - (analyticsLink.addEventListener "click" - ->:Void { - (let [analyticsDiv (document.createElement "div")] - (document.body.prepend analyticsDiv) - (set Prelude.printStr ->s (analyticsDiv.appendChild (p s)))) - - (let [:String subjectLine .innerHTML (document.getElementById "top_subject") - lastLeftParenIndex (subjectLine.lastIndexOf "(") - lastRightParenIndex (subjectLine.lastIndexOf ")") - [Read n times] (.split (substr subjectLine lastLeftParenIndex lastRightParenIndex) " ")] - (print "$n views")) - - - (let [:String url - window.location.href - urlWithoutStartingPost0 - (substr url 0 -1) - postsPerPage - 20 - navPageElements - (document.getElementsByClassName "navPages") - numPages - (if (< 0 navPageElements.length) - (Std.parseInt .innerHTML (nth navPageElements (- navPageElements.length 1))) - 1)] - (for page (range 1 numPages) - (let [pageUrl "${urlWithoutStartingPost0}$(* page postsPerPage)"] - (awaitLet [resp (API.browser.runtime.sendMessage pageUrl)] - ~resp)))) - - (printPostStats (postAuthors document)) - - }) - (set analyticsLink.innerHTML "Run analytics") - (document.body.prepend analyticsLink)) +(cond + ((StringTools.endsWith window.location.href ".0") + (let [analyticsLink (document.createElement "a")] + (analyticsLink.addEventListener "click" + ->:Void { + (let [:String url + window.location.href + urlWithoutStartingPost0 + (substr url 0 -1) + postsPerPage + 20 + navPageElements + (document.getElementsByClassName "navPages") + numPages + (if (< 0 navPageElements.length) + (Std.parseInt .innerHTML (nth navPageElements (- navPageElements.length 1))) + 1)] + + (awaitLet [_ + (API.browser.runtime.sendMessage + (object + type "pages" + data (for page (range 1 numPages) "${urlWithoutStartingPost0}$(* page postsPerPage)")))] + (postAuthors document))) + }) + (set analyticsLink.innerHTML "Run analytics") + (document.body.prepend analyticsLink))) + (true + (window.addEventListener "load" + ->:Void (postAuthors document)))) (API.browser.runtime.onMessage.addListener - ->[message sender sendResponse] { - ~message + ->[:Message message sender sendResponse] { + (case message + ((object type "nextPage" data [pageUrl]) + (set window.location.href pageUrl)) + ((object type "authorList" data authors) + (printPostStats authors)) + (never otherwise)) + true }) \ No newline at end of file diff --git a/projects/tig-spy/src/tig_spy/Message.hx b/projects/tig-spy/src/tig_spy/Message.hx new file mode 100644 index 00000000..17efdd44 --- /dev/null +++ b/projects/tig-spy/src/tig_spy/Message.hx @@ -0,0 +1,6 @@ +package tig_spy; + +typedef Message = { + type:String, + data:Array +}; \ No newline at end of file diff --git a/projects/tig-spy/src/tig_spy/Util.kiss b/projects/tig-spy/src/tig_spy/Util.kiss deleted file mode 100644 index 152ca91f..00000000 --- a/projects/tig-spy/src/tig_spy/Util.kiss +++ /dev/null @@ -1,13 +0,0 @@ -(function postElements [:Dynamic document] - (let [postTable (.item (document.getElementsByClassName "bordercolor") 0) - postTableBody (postTable.children.item 0) - tableRows (for i (range postTableBody.children.length) (postTableBody.children.item i))] - // There is an empty row at the end: - (tableRows.pop) - tableRows)) - -(function postAuthors [:Dynamic document] - (for [idx post] (enumerate (postElements document)) - (let [links (post.getElementsByTagName "a") - userLink (links.item (min idx 1))] - userLink.innerHTML)))