This repository was archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebchat.js
More file actions
99 lines (90 loc) · 2.88 KB
/
webchat.js
File metadata and controls
99 lines (90 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Techtronix Webchat
*
* Embed helper script
*/
'use strict';
/** Configuration object
* @property {string} host The IRC server hostname
* @property {number} port The IRC server port
* @property {boolean} secure Is the IRC port set to accept secure connections?
*/
const ircConfig = {
host: 'irc.techtronix.net',
port: 6697,
secure: true
}
/** Build IRC URL
* @param {string} host The IRC host
* @param {number} port The IRC port
* @param {boolean} secure Is the IRC port set to accept secure connections?
* @return {string} The constructed URL
*/
function buildIrcUrl(host, port, secure) {
// If port is 6667, but secure is enabled, send a warning to console
if ((port === 6667) && secure) {
console.warn('Specified port is 6667, but secure is set to true. Did you mean for secure to be false?');
}
// Continue on with the URL building
let urlString = '';
if (secure) {
urlString += 'ircs://';
} else {
urlString += 'irc://';
}
urlString += host;
urlString += ':';
urlString += port;
return encodeURI(urlString);
}
/** Build KiwiIRC link
*
* Useful for embedding into an iframe
* @param {string} host The IRC host
* @param {number} port The IRC port
* @param {boolean} secure Is the IRC port set to accept secure connections?
* @return {string} An encoded URL string
*
* Example:
* https://kiwiirc.com/nextclient/#ircs://irc.techtronix.net:6697/?nick=Guest?&channel=#test
*/
function buildKiwiLink(host, port, secure) {
// Variables
const baseUrl = 'https://kiwiirc.com/nextclient/#';
// Get page URL information
let url = new URL(window.location);
// Construct the iframe src URL
let urlString = baseUrl;
urlString += buildIrcUrl(host, port, secure);
urlString += '/';
// If a hash e.g. #lounge is specified, include it
if (url.hash !== '') {
urlString += url.hash;
}
// If parameters were provided, include it
if (url.search !== '') {
urlString += url.search;
} else {
// Only include this if there was no hash
if (url.hash === '') {
urlString += '?nick=Guest?&channel=#lounge';
}
}
return encodeURI(urlString);
}
/** Change embed iframe dimensions on window resize
*/
function changeChatFrameDimensions() {
let embed = document.getElementById('chat-iframe');
embed.setAttribute('height', window.innerHeight);
embed.setAttribute('width', window.innerWidth);
}
// Events to attach to
window.addEventListener('load', () => {
let embed = document.createElement('iframe');
embed.setAttribute('id', 'chat-iframe');
embed.setAttribute('frameborder', 0);
let kiwiUrl = buildKiwiLink(ircConfig.host, ircConfig.port, ircConfig.secure);
embed.setAttribute('src', kiwiUrl);
document.getElementById('chat').appendChild(embed);
console.info('KiwiIRC embed created:', kiwiUrl);
});