Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/jfn_cef/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static RESOURCES: &[(&str, Embedded)] = &[
embedded!("overlay.html", "text/html"),
embedded!("overlay.js", "application/javascript"),
embedded!("overlay.lang.js", "application/javascript"),
embedded!("server-url.js", "application/javascript"),
];

fn lookup(url_path: &str) -> Option<&'static Embedded> {
Expand Down
4 changes: 4 additions & 0 deletions src/web/overlay.css
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ button:disabled {
padding-left: 1.5em;
padding-right: 1.5em;
}
.dialog-hint {
margin-top: 1em;
color: #fff;
}
.dialog-button {
margin-top: 2em;
width: auto;
Expand Down
1 change: 1 addition & 0 deletions src/web/overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ <h1 id="title"></h1>
</div>
<script src="overlay.lang.js"></script>
<script src="connectivityHelper.js"></script>
<script src="server-url.js"></script>
<script src="overlay.js"></script>
</body>
</html>
13 changes: 11 additions & 2 deletions src/web/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const cancelOnEscape = (e) => {
}
};

const showConnectionFailedDialog = () => {
const showConnectionFailedDialog = (server) => {
const dialog = document.createElement('div');
dialog.className = 'dialog scaleIn';

Expand All @@ -119,6 +119,14 @@ const showConnectionFailedDialog = () => {
message.innerText = messageUnableToConnectToServerText;
message.className = 'dialog-message';

const suggestedUrl = window.jmpDefaultServerUrlForHostOnly(server);
let hint = null;
if (suggestedUrl) {
hint = document.createElement('div');
hint.innerText = `No scheme or port was provided. For a default local Jellyfin server, try ${suggestedUrl}.`;
hint.className = 'dialog-message dialog-hint';
}

const button = document.createElement('button');
button.innerText = buttonGotItText;
button.type = 'button';
Expand All @@ -129,6 +137,7 @@ const showConnectionFailedDialog = () => {

dialog.appendChild(header);
dialog.appendChild(message);
if (hint) dialog.appendChild(hint);
dialog.appendChild(button);
document.body.appendChild(dialog);
};
Expand Down Expand Up @@ -166,7 +175,7 @@ const startConnecting = async () => {
button.style.visibility = 'visible';
document.removeEventListener('keydown', cancelOnEscape);
updateButtonState();
showConnectionFailedDialog();
showConnectionFailedDialog(server);
}
};

Expand Down
25 changes: 25 additions & 0 deletions src/web/server-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(function(global) {
function defaultServerUrlForHostOnly(address) {
const value = String(address || '').trim();
if (!value || value.includes('://') || value.startsWith('//')) return null;

// A bare IPv6 address needs brackets before the URL parser can
// distinguish it from a host with an explicit port.
const colonCount = (value.match(/:/g) || []).length;
const candidate = colonCount > 1 && !value.startsWith('[')
? `http://[${value}]`
: `http://${value}`;

try {
const url = new URL(candidate);
if (url.username || url.password || url.port || url.pathname !== '/' || url.search || url.hash) {
return null;
}
return `http://${url.hostname}:8096`;
} catch (_) {
return null;
}
}

global.jmpDefaultServerUrlForHostOnly = defaultServerUrlForHostOnly;
})(typeof window === 'undefined' ? globalThis : window);
17 changes: 17 additions & 0 deletions src/web/server-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('node:assert/strict');
const test = require('node:test');

require('./server-url.js');

test('suggests the Jellyfin default URL for host-only input', () => {
assert.equal(jmpDefaultServerUrlForHostOnly('jellyfin.local'), 'http://jellyfin.local:8096');
assert.equal(jmpDefaultServerUrlForHostOnly(' 192.168.1.50 '), 'http://192.168.1.50:8096');
assert.equal(jmpDefaultServerUrlForHostOnly('::1'), 'http://[::1]:8096');
});

test('does not suggest a URL when connection details were supplied', () => {
assert.equal(jmpDefaultServerUrlForHostOnly('http://jellyfin.local'), null);
assert.equal(jmpDefaultServerUrlForHostOnly('jellyfin.local:8096'), null);
assert.equal(jmpDefaultServerUrlForHostOnly('jellyfin.local/web'), null);
assert.equal(jmpDefaultServerUrlForHostOnly(''), null);
});