25 lines
1.2 KiB
JavaScript
25 lines
1.2 KiB
JavaScript
// Scoped to this plugin; host authentication and global fetch are unchanged.
|
|
async function dshChatFetch(path, init) {
|
|
const parts = window.location.pathname.split('/');
|
|
const prefix = parts[1] === 'dsh' && parts[2] ? '/dsh/' + parts[2] : '';
|
|
return fetch(prefix + path, init);
|
|
}
|
|
async function dshChatJson(response) {
|
|
const text = await response.text();
|
|
try { return JSON.parse(text); } catch {
|
|
throw new Error('Chat Manager: HTTP ' + response.status + ' returned a non-JSON response; check the instance proxy path and authentication.');
|
|
}
|
|
}
|
|
export const clientHelpers = dshChatFetch.toString() + String.fromCharCode(10) + dshChatJson.toString() + String.fromCharCode(10);
|
|
|
|
export function patchClient(source, version) {
|
|
if (version !== '1.5.20') throw new Error('unsupported chat-manager version');
|
|
if (source.includes('dshChatFetch') || source.split('await fetch(').length !== 4
|
|
|| source.split('await response.json()').length !== 4
|
|
|| source.split('/plugins/dsh-session-delete/').length !== 4) {
|
|
throw new Error('unexpected chat-manager client; review upstream changes');
|
|
}
|
|
return clientHelpers + source.replaceAll('await fetch(', 'await dshChatFetch(')
|
|
.replaceAll('await response.json()', 'await dshChatJson(response)');
|
|
}
|