A lot of changes and reducing the size of index.js

This should have general improvements, some things are half implemented, and still need some work, let me know if y'all see any bugs
This commit is contained in:
MathMan05 2024-06-13 17:03:10 -05:00
parent 21b520f10c
commit 618cbf6167
8 changed files with 221 additions and 137 deletions

View file

@ -5,21 +5,21 @@ class channel{
channel.contextmenu.addbutton("Copy channel id",function(){
console.log(this)
navigator.clipboard.writeText(this.id);
})
});
channel.contextmenu.addbutton("Mark as read",function(){
console.log(this)
this.readbottom();
})
});
channel.contextmenu.addbutton("Delete channel",function(){
console.log(this)
this.deleteChannel();
},null,_=>{return thisuser.isAdmin()})
},null,_=>{console.log(_);return _.isAdmin()});
channel.contextmenu.addbutton("Edit channel",function(){
editchannelf(this);
},null,_=>{return thisuser.isAdmin()})
},null,_=>{return _.isAdmin()});
}
constructor(JSON,owner){
if(JSON===-1){
@ -42,6 +42,9 @@ class channel{
this.lastreadmessageid=null;
this.lastmessageid=JSON.last_message_id;
}
isAdmin(){
return this.owner.isAdmin();
}
readStateInfo(json){
this.lastreadmessageid=json.last_message_id;
this.mentions=json.mention_count;
@ -186,7 +189,7 @@ class channel{
}
get myhtml(){
const search=document.getElementById("channels").children[0].children
if(this.owner!==thisuser.lookingguild){
if(this.owner!==this.owner.owner.lookingguild){
return null
}else if(this.parrent){
for(const thing of search){
@ -449,7 +452,7 @@ class channel{
messageCreate(messagep,focus){
const messagez=new cmessage(messagep.d);
this.lastmessageid=messagez.id;
if(messagez.author===thisuser.user){
if(messagez.author===this.owner.owner.user){
this.lastreadmessageid=messagez.id;
if(this.myhtml){
this.myhtml.classList.remove("cunread");

View file

@ -3,7 +3,9 @@ class dirrect extends guild{
super(-1);
console.log(JSON);
this.owner=owner;
this.owner??=thisuser;
if(!this.owner){
console.error("Owner was not included, please fix")
}
this.channels=[];
this.channelids={};
this.id="@me";
@ -97,7 +99,7 @@ class group extends channel{
messageCreate(messagep,focus){
const messagez=new cmessage(messagep.d);
this.lastmessageid=messagez.id;
if(messagez.author===thisuser.user){
if(messagez.author===this.owner.owner.user){
this.lastreadmessageid=messagez.id;
}
this.messages.unshift(messagez);
@ -111,7 +113,7 @@ class group extends channel{
scrolly.scrollTop = scrolly.scrollHeight;
}
console.log(document.getElementById("channels").children)
if(thisuser.lookingguild===this.owner){
if(this.owner.owner.lookingguild===this.owner){
const channellist=document.getElementById("channels").children[0]
for(const thing of channellist.children){
if(thing.myinfo===this){

View file

@ -180,21 +180,27 @@ class fullscreen{
select.addEventListener("change",array[3]);
return div;
}
case "tabs":
case "tabs":{
const table=document.createElement("table");
const tabs=document.createElement("tr");
tabs.classList.add("tabbed-head");
table.appendChild(tabs);
const td=document.createElement("td");
tabs.appendChild(td);
const content=document.createElement("tr");
content.classList.add("tabbed-content");
table.appendChild(content);
let shown;
for(const thing of array[1]){
const td=document.createElement("td");
const button=document.createElement("button");
button.innerText=thing[0];
td.appendChild(button);
tabs.appendChild(td);
const tdcontent=document.createElement("td");
tdcontent.colSpan=array[1].length;
tdcontent.appendChild(this.tohtml(thing[1]));
content.appendChild(tdcontent);
if(!shown){
@ -209,6 +215,7 @@ class fullscreen{
})
}
return table;
}
default:
console.error("can't find element:"+array[0]," full element:"+array)
return;

View file

@ -1,5 +1,5 @@
class guild{
static contextmenu=new contextmenu("channel menu");
static contextmenu=new contextmenu("guild menu");
static setupcontextmenu(){
guild.contextmenu.addbutton("Copy Guild id",function(){
console.log(this)
@ -31,7 +31,9 @@ class guild{
}
console.log(JSON);
this.owner=owner;
this.owner??=thisuser;
if(!this.owner){
console.error("Owner was not included, please fix")
}
this.channels=[];
this.channelids={};
this.id=JSON.id;

View file

@ -18,7 +18,7 @@ setDynamicHeight();
let token=gettoken();
let ws
initwebsocket();
//initwebsocket();
let READY;
var currentmenu="";
@ -33,7 +33,7 @@ document.addEventListener('click', function(event) {
});
let replyingto=null;
{
const menu=new contextmenu("create backclick");
const menu=new contextmenu("create rightclick");
menu.addbutton("Create channel",function(){
createchannels(thisuser.lookingguild.createChannel.bind(thisuser.lookingguild));
},null,_=>{return thisuser.isAdmin()})
@ -262,122 +262,8 @@ let serverid=[];
let thisuser=null;
function initwebsocket(){
ws = new WebSocket(info.gateway.toString());
ws.addEventListener('open', (event) => {
console.log('WebSocket connected');
ws.send(JSON.stringify({
"op": 2,
"d": {
"token":token,
"capabilities": 16381,
"properties": {
"browser": "Jank Client",
"client_build_number": 0,
"release_channel": "Custom",
"browser_user_agent": navigator.userAgent
},
"compress": false,
"presence": {
"status": "online",
"since": new Date().getTime(),
"activities": [],
"afk": false
}
}
}))
});
ws.addEventListener('message', (event) => {
try{
const temp=JSON.parse(event.data);
console.log(temp)
if(temp.op==0){
switch(temp.t){
case "MESSAGE_CREATE":
if(thisuser){
thisuser.messageCreate(temp);
}
break;
case "READY":
thisuser=new localuser(temp);
thisuser.loaduser();
READY=temp;
thisuser.init();
genusersettings();
document.getElementById("loading").classList.add("doneloading");
document.getElementById("loading").classList.remove("loading")
break;
case "MESSAGE_UPDATE":
if(thisuser){
if(window.location.pathname.split("/")[3]==temp.d.channel_id){
const find=temp.d.id;
for(const message of messagelist){
if(message.all.id===find){
message.all.content=temp.d.content;
message.txt.innerHTML=markdown(temp.d.content).innerHTML;
break;
}
}
}
}
break;
case "TYPING_START":
if(thisuser){
thisuser.typeingStart(temp);
}
break;
case "USER_UPDATE":
if(thisuser){
const users=user.userids[temp.d.id];
console.log(users,temp.d.id)
if(users){
users.userupdate(temp.d);
console.log("in here");
}
}
break
case "CHANNEL_UPDATE":
if(thisuser){
thisuser.updateChannel(temp.d);
}
break;
case "CHANNEL_CREATE":
if(thisuser){
thisuser.createChannel(temp.d);
}
break;
case "CHANNEL_DELETE":
if(thisuser){
thisuser.delChannel(temp.d);
}
break;
}
}else if(temp.op===10){
console.log("heartbeat down")
setInterval(function(){
ws.send(JSON.stringify({op:1,d:packets}))
},temp.d.heartbeat_interval)
packets=1;
}else if(temp.op!=11){
packets++
}
}catch(error){
console.error(error)
}
});
ws.addEventListener('close', (event) => {
console.log('WebSocket closed');
});
}
thisuser=new localuser();
let cchanel=0;

View file

@ -1,5 +1,10 @@
class localuser{
constructor(ready){
constructor(){
this.initwebsocket();
this.initialized=false;
}
gottenReady(ready){
this.initialized=true;
this.ready=ready;
this.guilds=[];
this.guildids={};
@ -33,6 +38,120 @@ class localuser{
}
this.typing=[];
}
initwebsocket(){
this.ws = new WebSocket(info.gateway.toString());
this.ws.addEventListener('open', (event) => {
console.log('WebSocket connected');
this.ws.send(JSON.stringify({
"op": 2,
"d": {
"token":token,
"capabilities": 16381,
"properties": {
"browser": "Jank Client",
"client_build_number": 0,
"release_channel": "Custom",
"browser_user_agent": navigator.userAgent
},
"compress": false,
"presence": {
"status": "online",
"since": new Date().getTime(),
"activities": [],
"afk": false
}
}
}))
});
this.ws.addEventListener('message', (event) => {
try{
const temp=JSON.parse(event.data);
console.log(temp)
if(temp.op==0){
switch(temp.t){
case "MESSAGE_CREATE":
if(this.initialized){
this.messageCreate(temp);
}
break;
case "READY":
this.gottenReady(temp);
this.loaduser();
READY=temp;
this.init();
genusersettings();
document.getElementById("loading").classList.add("doneloading");
document.getElementById("loading").classList.remove("loading")
break;
case "MESSAGE_UPDATE":
if(this.initialized){
if(window.location.pathname.split("/")[3]==temp.d.channel_id){
const find=temp.d.id;
for(const message of messagelist){
if(message.all.id===find){
message.all.content=temp.d.content;
message.txt.innerHTML=markdown(temp.d.content).innerHTML;
break;
}
}
}
}
break;
case "TYPING_START":
if(this.initialized){
this.typeingStart(temp);
}
break;
case "USER_UPDATE":
if(this.initialized){
const users=user.userids[temp.d.id];
console.log(users,temp.d.id)
if(users){
users.userupdate(temp.d);
console.log("in here");
}
}
break
case "CHANNEL_UPDATE":
if(this.initialized){
this.updateChannel(temp.d);
}
break;
case "CHANNEL_CREATE":
if(this.initialized){
this.createChannel(temp.d);
}
break;
case "CHANNEL_DELETE":
if(this.initialized){
this.delChannel(temp.d);
}
break;
}
}else if(temp.op===10){
console.log("heartbeat down")
setInterval(_=>{
this.ws.send(JSON.stringify({op:1,d:packets}))
},temp.d.heartbeat_interval)
packets=1;
}else if(temp.op!=11){
packets++
}
}catch(error){
console.error(error)
}
});
this.ws.addEventListener('close', (event) => {
console.log('WebSocket closed');
});
}
resolveGuildidFromChannelID(ID){
let resolve=this.guilds.find(guild => guild.channelids[ID])
resolve??=undefined;
@ -157,13 +276,63 @@ class localuser{
div.innerText="+";
div.classList.add("addserver","servericon")
serverlist.appendChild(div)
div.onclick=function(){
div.onclick=_=>{
console.log("clicked :3")
this.createGuild();
}
}
this.unreads();
}
createGuild(){
let inviteurl="";
const error=document.createElement("span");
const full=new fullscreen(["tabs",[
["Join using invite",[
"vdiv",
["textbox",
"Invite Link/Code",
"",
function(){
console.log(this)
inviteurl=this.value;
}
],
["html",error]
,
["button",
"",
"Submit",
_=>{
let parsed="";
if(inviteurl.includes("/")){
parsed=inviteurl.split("/")[inviteurl.split("/").length-1]
}else{
parsed=inviteurl;
}
fetch(info.api.toString()+"/v9/invites/"+parsed,{
method:"POST",
headers:{
"Content-type": "application/json; charset=UTF-8",
Authorization:token
},
}).then(r=>r.json()).then(_=>{
console.log(_);
if(_.message){
error.innerText=_.message;
}
})
}
]
]],
["Create Server",[
"text","Not currently implemented, sorry"
]]
]])
full.show();
}
messageCreate(messagep){
messagep.d.guild_id??="@me";
this.guildids[messagep.d.guild_id].channelids[messagep.d.channel_id].messageCreate(messagep,this.channelfocus===messagep.d.channel_id);

View file

@ -4,7 +4,7 @@ class cmessage{
cmessage.contextmenu.addbutton("Copy raw text",function(){
console.log(this)
navigator.clipboard.writeText(this.content);
})
});
cmessage.contextmenu.addbutton("Reply",function(div){
console.log(this)
if(replyingto){
@ -13,11 +13,15 @@ class cmessage{
replyingto=div;
console.log(div);
replyingto.classList.add("replying");
})
});
cmessage.contextmenu.addbutton("Copy message id",function(){
console.log(this)
navigator.clipboard.writeText(this.id);
})
});
cmessage.contextmenu.addbutton("Copy user id",function(){
console.log(this)
navigator.clipboard.writeText(this.author.id);
});
cmessage.contextmenu.addbutton("Message user",function(){
console.log(this)
fetch(info.api.toString()+"/v9/users/@me/channels",

View file

@ -265,7 +265,7 @@ input {
background-color: var(--message-bg-hover);
color: var(--primary-text);
height: .4in;
width: 100%;
width: 97.5%;
}
div {
@ -763,3 +763,14 @@ span {
.servernoti {
position: relative;
}
.tabbed-head{
background: var(--textarea-bg);
}
.tabbed-content td{
/* border-color: var(--textarea-bg); */
/* border-width:1in; */
border-inline: medium solid var(--textarea-bg);
}
.tabbed-head td button{
min-width:1.5in;
}