Skip to content

Commit

Permalink
add option to load custom js on all pages
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Aug 11, 2024
1 parent c862ec1 commit dbd42bc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 4 deletions.
7 changes: 7 additions & 0 deletions contrib/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
93 changes: 93 additions & 0 deletions contrib/plugins/banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(function() {

// usage: copy this to '.banner.js' in your webroot,
// and run copyparty with the following argument:
// --body-foot '<script src="/.banner.js"></script>'



// had to pick the most chuuni one as the default
var bannertext = '' +
'<h3>You are accessing a U.S. Government (USG) Information System (IS) that is provided for USG-authorized use only.</h3>' +
'<p>By using this IS (which includes any device attached to this IS), you consent to the following conditions:</p>' +
'<ul>' +
'<li>The USG routinely intercepts and monitors communications on this IS for purposes including, but not limited to, penetration testing, COMSEC monitoring, network operations and defense, personnel misconduct (PM), law enforcement (LE), and counterintelligence (CI) investigations.</li>' +
'<li>At any time, the USG may inspect and seize data stored on this IS.</li>' +
'<li>Communications using, or data stored on, this IS are not private, are subject to routine monitoring, interception, and search, and may be disclosed or used for any USG-authorized purpose.</li>' +
'<li>This IS includes security measures (e.g., authentication and access controls) to protect USG interests -- not for your personal benefit or privacy.</li>' +
'<li>Notwithstanding the above, using this IS does not constitute consent to PM, LE or CI investigative searching or monitoring of the content of privileged communications, or work product, related to personal representation or services by attorneys, psychotherapists, or clergy, and their assistants. Such communications and work product are private and confidential. See User Agreement for details.</li>' +
'</ul>';



// 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);
}

})();
7 changes: 4 additions & 3 deletions copyparty/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <head> 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 <head> 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)")
Expand Down
10 changes: 10 additions & 0 deletions copyparty/httpcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ def j2s(self, name: str, **ka: Any) -> str:
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
Expand Down Expand Up @@ -3793,6 +3798,11 @@ def tx_md(self, vn: VFS, fs_path: str) -> bool:
"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
Expand Down
1 change: 0 additions & 1 deletion copyparty/web/browser2.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
td{border:1px solid #999;border-width:1px 1px 0 0;padding:0 5px}
a{display:block}
</style>
{{ html_head }}
</head>

<body>
Expand Down
3 changes: 3 additions & 0 deletions copyparty/web/md.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,8 @@
{%- if edit %}
<script src="{{ r }}/.cpr/md2.js?_={{ ts }}"></script>
{%- endif %}
{%- if js %}
<script src="{{ js }}_={{ ts }}"></script>
{%- endif %}
</body></html>

3 changes: 3 additions & 0 deletions copyparty/web/mde.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@
<script src="{{ r }}/.cpr/deps/marked.js?_={{ ts }}"></script>
<script src="{{ r }}/.cpr/deps/easymde.js?_={{ ts }}"></script>
<script src="{{ r }}/.cpr/mde.js?_={{ ts }}"></script>
{%- if js %}
<script src="{{ js }}_={{ ts }}"></script>
{%- endif %}
</body></html>

3 changes: 3 additions & 0 deletions copyparty/web/msg.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ <h2>{{ h2 }}</h2>
}, 1000);
</script>
{%- endif %}
{%- if js %}
<script src="{{ js }}_={{ ts }}"></script>
{%- endif %}
</body>

</html>
Expand Down
3 changes: 3 additions & 0 deletions copyparty/web/splash.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ <h1 id="l">login for more:</h1>
</script>
<script src="{{ r }}/.cpr/util.js?_={{ ts }}"></script>
<script src="{{ r }}/.cpr/splash.js?_={{ ts }}"></script>
{%- if js %}
<script src="{{ js }}_={{ ts }}"></script>
{%- endif %}
</body>
</html>

3 changes: 3 additions & 0 deletions copyparty/web/svcs.html
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ <h1>SMB / CIFS</h1>
</script>
<script src="{{ r }}/.cpr/util.js?_={{ ts }}"></script>
<script src="{{ r }}/.cpr/svcs.js?_={{ ts }}"></script>
{%- if js %}
<script src="{{ js }}_={{ ts }}"></script>
{%- endif %}
</body>
</html>

0 comments on commit dbd42bc

Please sign in to comment.