From dbd42bc6bf4471226ba02c3d5624c6442789b838 Mon Sep 17 00:00:00 2001 From: ed Date: Sun, 11 Aug 2024 23:51:17 +0000 Subject: [PATCH] add option to load custom js on all pages --- contrib/plugins/README.md | 7 +++ contrib/plugins/banner.js | 93 +++++++++++++++++++++++++++++++++++++ copyparty/__main__.py | 7 +-- copyparty/httpcli.py | 10 ++++ copyparty/web/browser2.html | 1 - copyparty/web/md.html | 3 ++ copyparty/web/mde.html | 3 ++ copyparty/web/msg.html | 3 ++ copyparty/web/splash.html | 3 ++ copyparty/web/svcs.html | 3 ++ 10 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 contrib/plugins/banner.js diff --git a/contrib/plugins/README.md b/contrib/plugins/README.md index 630cf74f..baeb08f1 100644 --- a/contrib/plugins/README.md +++ b/contrib/plugins/README.md @@ -20,6 +20,13 @@ point `--js-browser` to one of these by URL: +## example any-js +point `--js-browser` and/or `--js-other` to one of these by URL: + +* [`banner.js`](banner.js) shows a very enterprise [legal-banner](https://github.com/user-attachments/assets/8ae8e087-b209-449c-b08d-74e040f0284b) + + + ## example browser-css point `--css-browser` to one of these by URL: diff --git a/contrib/plugins/banner.js b/contrib/plugins/banner.js new file mode 100644 index 00000000..701ff3cb --- /dev/null +++ b/contrib/plugins/banner.js @@ -0,0 +1,93 @@ +(function() { + +// usage: copy this to '.banner.js' in your webroot, +// and run copyparty with the following argument: +// --body-foot '' + + + +// had to pick the most chuuni one as the default +var bannertext = '' + +'

You are accessing a U.S. Government (USG) Information System (IS) that is provided for USG-authorized use only.

' + +'

By using this IS (which includes any device attached to this IS), you consent to the following conditions:

' + +''; + + + +// fancy div to insert into pages +function bannerdiv(border) { + var ret = mknod('div', null, bannertext); + if (border) + ret.setAttribute("style", "border:1em solid var(--fg); border-width:.3em 0; margin:3em 0"); + return ret; +} + + + +// keep all of these false and then selectively enable them in the if-blocks below +var show_msgbox = false, + login_top = false, + top = false, + bottom = false, + top_bordered = false, + bottom_bordered = false; + +if (QS("h1#cc") && QS("a#k")) { + // this is the controlpanel + // (you probably want to keep just one of these enabled) + show_msgbox = true; + login_top = true; + bottom = true; +} +else if (ebi("swin") && ebi("smac")) { + // this is the connect-page, same deal here + show_msgbox = true; + top_bordered = true; + bottom_bordered = true; +} +else if (ebi("op_cfg") || ebi("div#mw") ) { + // we're running in the main filebrowser (op_cfg) or markdown-viewer/editor (div#mw), + // fragile pages which break if you do something too fancy + show_msgbox = true; +} + + + +// shows a fullscreen messagebox; works on all pages +if (show_msgbox) { + var now = Math.floor(Date.now() / 1000), + last_shown = sread("bannerts") || 0; + + // 60 * 60 * 17 = 17 hour cooldown + if (now - last_shown > 60 * 60 * 17) { + swrite("bannerts", now); + modal.confirm(bannertext, null, function () { + location = 'https://this-page-intentionally-left-blank.org/'; + }); + } +} + +// show a message on the page footer; only works on the connect-page +if (top || top_bordered) { + var dst = ebi('wrap'); + dst.insertBefore(bannerdiv(top_bordered), dst.firstChild); +} + +// show a message on the page footer; only works on the controlpanel and connect-page +if (bottom || bottom_bordered) { + ebi('wrap').appendChild(bannerdiv(bottom_bordered)); +} + +// show a message on the top of the page; only works on the controlpanel +if (login_top) { + var dst = QS('h1'); + dst.parentNode.insertBefore(bannerdiv(false), dst); +} + +})(); diff --git a/copyparty/__main__.py b/copyparty/__main__.py index 917fab50..63c68078 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -1366,9 +1366,10 @@ def add_ui(ap, retry): ap2.add_argument("--unlist", metavar="REGEX", type=u, default="", help="don't show files matching \033[33mREGEX\033[0m in file list. Purely cosmetic! Does not affect API calls, just the browser. Example: [\033[32m\\.(js|css)$\033[0m] (volflag=unlist)") ap2.add_argument("--favico", metavar="TXT", type=u, default="c 000 none" if retry else "🎉 000 none", help="\033[33mfavicon-text\033[0m [ \033[33mforeground\033[0m [ \033[33mbackground\033[0m ] ], set blank to disable") ap2.add_argument("--mpmc", metavar="URL", type=u, default="", help="change the mediaplayer-toggle mouse cursor; URL to a folder with {2..5}.png inside (or disable with [\033[32m.\033[0m])") - ap2.add_argument("--js-browser", metavar="L", type=u, default="", help="URL to additional JS to include") - ap2.add_argument("--css-browser", metavar="L", type=u, default="", help="URL to additional CSS to include") - ap2.add_argument("--html-head", metavar="TXT", type=u, default="", help="text to append to the of all HTML pages; can be @PATH to send the contents of a file at PATH, and/or begin with %% to render as jinja2 template (volflag=html_head)") + ap2.add_argument("--css-browser", metavar="L", type=u, default="", help="URL to additional CSS to include in the filebrowser html") + ap2.add_argument("--js-browser", metavar="L", type=u, default="", help="URL to additional JS to include in the filebrowser html") + ap2.add_argument("--js-other", metavar="L", type=u, default="", help="URL to additional JS to include in all other pages") + ap2.add_argument("--html-head", metavar="TXT", type=u, default="", help="text to append to the of all HTML pages (except for basic-browser); can be @PATH to send the contents of a file at PATH, and/or begin with %% to render as jinja2 template (volflag=html_head)") ap2.add_argument("--ih", action="store_true", help="if a folder contains index.html, show that instead of the directory listing by default (can be changed in the client settings UI, or add ?v to URL for override)") ap2.add_argument("--textfiles", metavar="CSV", type=u, default="txt,nfo,diz,cue,readme", help="file extensions to present as plaintext") ap2.add_argument("--txt-max", metavar="KiB", type=int, default=64, help="max size of embedded textfiles on ?doc= (anything bigger will be lazy-loaded by JS)") diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 3232000d..19655686 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -228,6 +228,11 @@ class HttpCli(object): ka["s_doctitle"] = self.args.doctitle ka["tcolor"] = self.vn.flags["tcolor"] + if self.args.js_other and "js" not in ka: + zs = self.args.js_other + zs += "&" if "?" in zs else "?" + ka["js"] = zs + zso = self.vn.flags.get("html_head") if zso: ka["this"] = self @@ -3793,6 +3798,11 @@ class HttpCli(object): "arg_base": arg_base, } + if self.args.js_other and "js" not in targs: + zs = self.args.js_other + zs += "&" if "?" in zs else "?" + targs["js"] = zs + zfv = self.vn.flags.get("html_head") if zfv: targs["this"] = self diff --git a/copyparty/web/browser2.html b/copyparty/web/browser2.html index 03195b4e..4a71282a 100644 --- a/copyparty/web/browser2.html +++ b/copyparty/web/browser2.html @@ -11,7 +11,6 @@ td{border:1px solid #999;border-width:1px 1px 0 0;padding:0 5px} a{display:block} -{{ html_head }} diff --git a/copyparty/web/md.html b/copyparty/web/md.html index 78ab505b..8e1722c9 100644 --- a/copyparty/web/md.html +++ b/copyparty/web/md.html @@ -159,5 +159,8 @@ try { l.light = drk? 0:1; } catch (ex) { } {%- if edit %} {%- endif %} + {%- if js %} + + {%- endif %} diff --git a/copyparty/web/mde.html b/copyparty/web/mde.html index e95508ba..8cc8a1b1 100644 --- a/copyparty/web/mde.html +++ b/copyparty/web/mde.html @@ -53,5 +53,8 @@ try { l.light = drk? 0:1; } catch (ex) { } + {%- if js %} + + {%- endif %} diff --git a/copyparty/web/msg.html b/copyparty/web/msg.html index 002b6558..4ccac31f 100644 --- a/copyparty/web/msg.html +++ b/copyparty/web/msg.html @@ -46,6 +46,9 @@ }, 1000); {%- endif %} + {%- if js %} + + {%- endif %} diff --git a/copyparty/web/splash.html b/copyparty/web/splash.html index eca8133f..5ce62b57 100644 --- a/copyparty/web/splash.html +++ b/copyparty/web/splash.html @@ -119,6 +119,9 @@ document.documentElement.className = (STG && STG.cpp_thm) || "{{ this.args.theme +{%- if js %} + +{%- endif %} diff --git a/copyparty/web/svcs.html b/copyparty/web/svcs.html index 217de9b6..6f8f386e 100644 --- a/copyparty/web/svcs.html +++ b/copyparty/web/svcs.html @@ -245,6 +245,9 @@ document.documentElement.className = (STG && STG.cpp_thm) || "{{ args.theme }}"; +{%- if js %} + +{%- endif %}