2024-07-05 13:19:38 +03:00
const app = { } // this is the app, it'll contain every possible thing about the app
app . invoke = window ? . _ _TAURI _ _ ? . core ? . invoke
app . fetch = window ? . _ _TAURI _ _ ? . http ? . fetch //|| fetch.bind(window)
app . clipboard = window ? . _ _TAURI _ _ ? . clipboardManager
2024-07-07 15:20:19 +03:00
app . fileExists = window ? . _ _TAURI _ _ ? . fs ? . exists
2024-07-05 13:19:38 +03:00
app . readTextFile = window ? . _ _TAURI _ _ ? . fs ? . readTextFile
app . writeTextFile = window ? . _ _TAURI _ _ ? . fs ? . writeTextFile
app . process = window ? . _ _TAURI _ _ ? . process
app . version = window ? . _ _TAURI _ _ ? . app ? . getVersion ( )
app . name = window ? . _ _TAURI _ _ ? . app ? . getName ( )
app . tversion = window ? . _ _TAURI _ _ ? . app ? . getTauriVersion ( )
app . platform = window ? . _ _TAURI _ _ ? . os ? . platform ( )
app . arch = window ? . _ _TAURI _ _ ? . os ? . arch ( )
app . opened _tabs = [ ]
app . collections = [ ]
app . environments = [ ]
app . tab _history = [ ]
app . request _history = [ ]
app . current _request
let greetInputEl ;
let greetMsgEl ;
let scrollbar _fix = ' < style > \
: : - webkit - scrollbar { \
width : 6 pt ; \
height : 6 pt ; \
border - radius : 5 pt ; \
} \
: : - webkit - scrollbar - track { \
background : # 111 ; \
border - radius : 5 pt ; \
} \
: : - webkit - scrollbar - thumb { \
background : # 888 ; \
border - radius : 5 pt ; \
} \
: : - webkit - scrollbar - thumb : hover { \
background : # 655 ; \
border - radius : 5 pt ; \
} < / s t y l e > '
async function greet ( ) {
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
greetMsgEl . textContent = await app . invoke ( "greet" , { name : greetInputEl . value } ) ;
}
2024-07-07 15:20:19 +03:00
2024-07-05 13:19:38 +03:00
// window.addEventListener("DOMContentLoaded", () => {
// greetInputEl = document.querySelector("#greet-input");
// greetMsgEl = document.querySelector("#greet-msg");
// document.querySelector("#greet-form").addEventListener("submit", (e) => {
// e.preventDefault();
// greet();
// });
// });
console . log ( app )
console . log ( window ? . _ _TAURI _ _ )
function fetchStream ( stream ) {
const reader = stream . getReader ( ) ;
let charsReceived = 0 ;
let result = ''
// read() returns a promise that resolves
// when a value has been received
reader . read ( ) . then ( function processText ( { done , value } ) {
// Result objects contain two properties:
// done - true if the stream has already given you all its data.
// value - some data. Always undefined when done is true.
if ( done ) {
console . log ( "Stream complete" ) ;
return ;
}
// value for fetch streams is a Uint8Array
charsReceived += value . length ;
const chunk = value ;
result += chunk ;
// Read some more, and call this function again
return reader . read ( ) . then ( processText ) ;
} ) ;
}
function highlight ( code ) {
let hl = hljs . highlightAuto ( code , [ 'JSON' , 'XML' , 'HTML' , 'JavaScript' ] ) . value
// let line = 1;
// hl = hl.replace(/^/gm, function() {
// return '<span class="line-number-position">​<span class="line-number">' + line++ + '</span></span>';
// });
return hl
}
function build _request _url ( ) {
let url _input = document . getElementById ( 'url_input' )
let full _url = url _input . value
let request _params _table = document . getElementById ( "query_params_table" )
let inputs = request _params _table . getElementsByTagName ( "input" )
let result = [ ]
let [ url , params ] = full _url . split ( "?" )
for ( let param of ( params || "" ) . split ( "&" ) ) {
let [ key , value ] = param . split ( "=" )
add _row ( null , request _params _table , { key : key , value : value , enabled : true } )
}
for ( let i = 0 ; i < inputs . length / 5 ; i ++ ) {
let enabled = inputs [ ( i * 5 ) ] . checked
let key = inputs [ ( i * 5 ) + 1 ] . value
let value = inputs [ ( i * 5 ) + 2 ] . value
2024-07-07 15:20:19 +03:00
if ( enabled ) { // need to save disabled as well... somehow
2024-07-05 13:19:38 +03:00
if ( key ) {
2024-07-07 15:20:19 +03:00
//console.log(i, inputs[(i*5)+1].value, inputs[(i*5)+2].value, inputs[(i*5)+3].value)
2024-07-05 13:19:38 +03:00
if ( value ) {
result . push ( inputs [ ( i * 5 ) + 1 ] . value + "=" + inputs [ ( i * 5 ) + 2 ] . value )
} else {
result . push ( inputs [ ( i * 5 ) + 1 ] . value )
}
}
}
}
for ( let i = 0 ; i < inputs . length / 5 ; i ++ ) {
let key = inputs [ ( i * 5 ) + 1 ] . value
let value = inputs [ ( i * 5 ) + 2 ] . value
// let description = inputs[(i*5)+3].value
let delete _butt = inputs [ ( i * 5 ) + 4 ]
// key && value === 0, and thus falsy (description misn't necessary)
if ( ! ( key && value ) ) {
if ( window . getComputedStyle ( delete _butt ) . display != "none" ) {
2024-07-07 15:20:19 +03:00
delete _row ( null , inputs [ ( i * 5 ) ] )
2024-07-05 13:19:38 +03:00
}
}
}
if ( ~ url . indexOf ( "http://" ) || ~ url . indexOf ( "https://" ) ) {
} else {
url = "https://" + url
}
url _input . value = url
if ( result . length > 0 ) {
url = url + "?" + result . join ( "&" )
}
console . log ( "ret" , url )
return url
}
function request _table _to _json ( table _name ) {
let table = document . getElementById ( table _name )
let inputs = table . getElementsByTagName ( "input" )
let result = { }
for ( let i = 0 ; i < inputs . length / 5 ; i ++ ) {
let enabled = inputs [ ( i * 5 ) ] . checked
let key = inputs [ ( i * 5 ) + 1 ] . value
let value = inputs [ ( i * 5 ) + 2 ] . value
let counter = 0
if ( enabled ) {
if ( key && value ) {
counter = counter + 1
result [ inputs [ ( i * 5 ) + 1 ] . value ] = inputs [ ( i * 5 ) + 2 ] . value
}
}
}
return result
}
function build _request _headers ( ) {
return request _table _to _json ( "headers_table" )
}
async function read _file _synchronyously ( file ) {
let result = await new Promise ( ( resolve ) => {
let reader = new FileReader ( ) ;
reader . onload = ( e ) => resolve ( reader . result ) ;
reader . readAsArrayBuffer ( file ) ;
} ) ;
console . log ( result ) ;
return result ;
}
async function build _request ( ) {
let raw _request _body _data = document . getElementById ( 'raw_request_body_data' )
let response _area = document . getElementById ( 'response_body' )
let method = document . getElementById ( 'metod_select' ) . getAttribute ( 'value' )
let auth _type = document . getElementById ( 'auth_type_select' ) . value
let auth _panel = document . getElementById ( 'auth_panel_' + auth _type )
let auth _inputs = auth _panel . getElementsByClassName ( "auth_input" )
2024-07-07 15:20:19 +03:00
let user
let password
let token
let domain
let workstation
2024-07-05 13:19:38 +03:00
let result = { }
result . url = build _request _url ( )
2024-07-07 15:20:19 +03:00
result . base _url = result . url . split ( "?" ) [ 0 ]
2024-07-05 13:19:38 +03:00
result . timeout = 30
result . headers = build _request _headers ( )
result . method = method
2024-07-07 15:20:19 +03:00
result . auth _type = auth _type
result . query _params = { }
let [ url , params ] = result . url . split ( "?" )
for ( let param of ( params || "" ) . split ( "&" ) ) {
console . log ( param )
let [ key , value ] = param . split ( "=" )
result . query _params [ key ] = value
}
2024-07-05 13:19:38 +03:00
// process authentication
switch ( auth _type ) {
case "inherit" :
// traverse the request tree till something that's not "inherit" is found
break
case "basic" :
for ( let input of auth _inputs ) {
if ( input . classList . contains ( "username_input" ) ) {
user = input . value
}
if ( input . classList . contains ( "password_input" ) ) {
password = input . value
}
}
if ( user && password ) {
let token = user + ":" + password
2024-07-07 15:20:19 +03:00
result . headers . Authorization = 'Basic ' + btoa ( token )
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
result . user = user
result . password = password
2024-07-05 13:19:38 +03:00
break
case "bearer" :
for ( let input of auth _inputs ) {
if ( input . classList . contains ( "token_input" ) ) {
token = input . value
}
}
if ( token ) {
result . headers . Authorization = 'Bearer ' + token
}
2024-07-07 15:20:19 +03:00
result . token = token
2024-07-05 13:19:38 +03:00
break
case "ntlm" :
2024-07-07 15:20:19 +03:00
for ( let input of auth _inputs ) {
if ( input . classList . contains ( "username_input" ) ) {
user = input . value
}
if ( input . classList . contains ( "password_input" ) ) {
password = input . value
}
if ( input . classList . contains ( "domain_input" ) ) {
user = input . value
}
if ( input . classList . contains ( "token_input" ) ) {
password = input . value
}
}
2024-07-05 13:19:38 +03:00
break
default :
// do nothing, "none" goes in here by default
break
}
// process body
let request _body _tabbar = document . getElementById ( 'request_body_tabbar' )
let radios = request _body _tabbar . getElementsByClassName ( 'tab_header_button' )
let body _type = "none"
for ( let radio of radios ) {
if ( radio . checked ) {
body _type = radio . value
}
}
2024-07-07 15:20:19 +03:00
result . body _type = body _type
2024-07-05 13:19:38 +03:00
switch ( body _type ) {
case "form-data" :
// this is not a result, this is only a JSON object with everything necessary in it
//result.body = request_table_to_json("form-data_table")
break
case "x-www-form-urlencoded" :
// this is not a result, this is only a JSON object with everything necessary in it
//result.body = request_table_to_json("x-www-form-urlencoded_table")
break
case "raw" :
result . body = body _editor . getValue ( )
break
case "binary" :
let body _binary _file _input = document . getElementById ( 'body_binary_file_input' )
let body _binary _file = body _binary _file _input . files [ 0 ]
result . body = await read _file _synchronyously ( body _binary _file )
break
default :
break
}
console . log ( "resul" , result )
return result
}
// now I can call our Command!
// You will see "Welcome from Tauri" replaced
// by "Hello, World!"!
async function send _request ( ) {
// {
// method:method_name,
// headers:{
// 'Accept': '*'
// },
// }
// the request itself is built in here
let request = await build _request ( ) // fucking blobs made me do this
2024-07-07 15:20:19 +03:00
console . log ( request )
2024-07-05 13:19:38 +03:00
app . current _request = JSON . parse ( JSON . stringify ( request ) )
let url = request . url
delete request . url
// and then I add "extra" fields based on the current state of the software
app . useragent = app . useragent || await app . name + "/" + await app . version + " (Tauri " + await app . tversion + ") " + app . platform + " (" + app . arch + ")"
request . headers [ "User-Agent" ] = request . headers [ "User-Agent" ] || app . useragent
request . headers . Accept = request . headers . Accept || "*/*"
request . headers [ "Accept-Encoding" ] = request . headers [ "Accept-Encoding" ] || "gzip, deflate, br"
request . headers . Connection = request . headers . Connection || "keep-alive"
2024-07-07 15:20:19 +03:00
await write _request _file ( app . current _tab )
2024-07-05 13:19:38 +03:00
// oauth
/ *
var token _ // variable will store the token
var userName = "clientID" ; // app clientID
var passWord = "secretKey" ; // app clientSecret
var caspioTokenUrl = "https://xxx123.caspio.com/oauth/token" ; // Your application token endpoint
var request = new XMLHttpRequest ( ) ;
function getToken ( url , clientID , clientSecret ) {
var key ;
request . open ( "POST" , url , true ) ;
request . setRequestHeader ( "Content-type" , "application/json" ) ;
request . send ( "grant_type=client_credentials&client_id=" + clientID + "&" + "client_secret=" + clientSecret ) ; // specify the credentials to receive the token on request
request . onreadystatechange = function ( ) {
if ( request . readyState == request . DONE ) {
var response = request . responseText ;
var obj = JSON . parse ( response ) ;
key = obj . access _token ; //store the value of the accesstoken
token _ = key ; // store token in your global variable "token_" or you could simply return the value of the access token from the function
}
}
}
// Get the token
getToken ( caspioTokenUrl , userName , passWord ) ;
function CallWebAPI ( ) {
var request _ = new XMLHttpRequest ( ) ;
var encodedParams = encodeURIComponent ( params ) ;
request _ . open ( "GET" , "https://xxx123.caspio.com/rest/v1/tables/" , true ) ;
request _ . setRequestHeader ( "Authorization" , "Bearer " + token _ ) ;
request _ . send ( ) ;
request _ . onreadystatechange = function ( ) {
if ( request _ . readyState == 4 && request _ . status == 200 ) {
var response = request _ . responseText ;
var obj = JSON . parse ( response ) ;
// handle data as needed...
}
}
}
* /
let enable _proxy = document . getElementById ( "enable_proxy" ) ? . checked
if ( enable _proxy ) {
console . log ( "proxy is on" )
request . proxy = { all : { url : "127.0.0.1:8080" } }
}
let result1
let start _time = new Date ( )
// sending actuall request
try {
console . log ( "second fetch" , JSON . stringify ( request , null , 4 ) )
result1 = await app . fetch ( url , request )
console . log ( "result1" , result1 )
} catch ( err ) {
let network _info = document . getElementById ( "network_info" )
let status _info = document . getElementById ( "status_info" )
let timespan _info = document . getElementById ( "timespan_info" )
let request _size _info = document . getElementById ( "request_size_info" )
network _info . innerHTML = [ '❌' ] . join ( " " )
status _info . innerHTML = [ "Status:" , "<span class='red'>-</span>" ] . join ( " " )
timespan _info . innerHTML = [ "Time:" , "<span class='red'>-</span>" ] . join ( " " )
request _size _info . innerHTML = [ "Size:" , "<span class='red'>-</span>" ] . join ( " " )
let raw _response _editor = document . getElementById ( "raw_response_body_data" )
let pre _response _editor = document . getElementById ( "raw_response_body_data" )
console . log ( err )
// settings output views:
/* pretty */ response _editor . setValue ( err . message || err )
/* raw */ raw _response _editor . textContent = err . message || err
/* preview */ pre _response _editor . innerHTML = err . message || err
document . getElementById ( 'preview_response_body_data' ) . src = "data:text/html;charset=utf-8," + encodeURIComponent ( scrollbar _fix + ( err . message || err ) ) ;
return
}
let end _time = new Date ( )
let date _time = new Date ( )
let response _table = document . getElementById ( "response_headers_table" )
response _table . innerHTML = "<tr><th>Key</th><th>Value</th></tr>"
2024-07-07 15:20:19 +03:00
for ( let i in window . sessionStorage ) {
let item = window . sessionStorage [ i ]
//console.log(item)
}
//console.log(window.sessionStorage.length)
2024-07-05 13:19:38 +03:00
if ( result1 ) {
let response _headers = { }
for ( let pair of result1 . headers . entries ( ) ) {
let tr = document . createElement ( 'tr' )
let td1 = document . createElement ( 'td' )
let td2 = document . createElement ( 'td' )
td1 . textContent = pair [ 0 ]
td2 . textContent = pair [ 1 ]
response _headers [ pair [ 0 ] ] = pair [ 1 ]
tr . appendChild ( td1 )
tr . appendChild ( td2 )
response _table . appendChild ( tr )
2024-07-07 15:20:19 +03:00
//console.log(pair)
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
//console.log(result1.headers.get('set-cookie'))
2024-07-05 13:19:38 +03:00
//for (let pair of result1.headers.get('set-cookie')) {
// console.log(pair)
//}
let result2
let code = ""
let mode = "text"
if ( ~ response _headers [ 'content-type' ] ? . indexOf ( "json" ) ) {
mode = "json"
}
if ( ~ response _headers [ 'content-type' ] ? . indexOf ( "xml" ) ) {
mode = "xml"
}
if ( ~ response _headers [ 'content-type' ] ? . indexOf ( "html" ) ) {
mode = "html"
}
if ( ~ response _headers [ 'content-type' ] ? . indexOf ( "css" ) ) {
mode = "css"
}
try {
result2 = ""
switch ( mode ) {
case "json" :
result2 = JSON . stringify ( await ( result1 ) . json ( ) )
break ;
default :
result2 = await ( result1 ) . text ( )
break ;
}
code = result2
} catch ( err ) {
console . log ( err , result1 )
}
let network _info = document . getElementById ( "network_info" )
let status _info = document . getElementById ( "status_info" )
let timespan _info = document . getElementById ( "timespan_info" )
let request _size _info = document . getElementById ( "request_size_info" )
let elapsed = 2 * end _time . getTime ( ) - start _time . getTime ( ) - date _time . getTime ( )
let body _length = code . length
let headers _length = JSON . stringify ( response _headers ) . length
2024-07-07 15:20:19 +03:00
//console.log("body_length", body_length, "headers_length", headers_length)
2024-07-05 13:19:38 +03:00
network _info . innerHTML = [ '🌍' ] . join ( " " )
status _info . innerHTML = [ "Status:" , "<span class='green'>" , result1 . status , result1 . statusText , '</span>' ] . join ( " " )
timespan _info . innerHTML = [ "Time:" , "<span class='green'>" , elapsed , 'ms' , '</span>' ] . join ( " " )
request _size _info . innerHTML = [ "Size:" , "<span class='green'>" , Math . ceil ( ( body _length + headers _length ) / 10 ) / 100 , 'KB' , '</span>' ] . join ( " " )
let raw _response _editor = document . getElementById ( "raw_response_body_data" )
let pre _response _editor = document . getElementById ( "raw_response_body_data" )
// settings output views:
/* pretty */ response _editor . setValue ( code || "" )
/* raw */ raw _response _editor . textContent = code
/* preview */ pre _response _editor . innerHTML = code
document . getElementById ( 'preview_response_body_data' ) . src = "data:text/html;charset=utf-8," + encodeURIComponent ( scrollbar _fix + code ) ;
// beautify response
document . getElementById ( 'beautify_raw_data_response' ) . click ( )
}
}
// ====================================================
function code _beautify ( source , mode ) {
//console.log('code_beautify', source, mode)
let result = source
let options = {
"indent_size" : "2" ,
"indent_char" : " " ,
"max_preserve_newlines" : "0" ,
"preserve_newlines" : true ,
"keep_array_indentation" : true ,
"break_chained_methods" : false ,
"indent_scripts" : "normal" ,
"brace_style" : "expand" ,
"space_before_conditional" : false ,
"unescape_strings" : false ,
"jslint_happy" : false ,
"end_with_newline" : true ,
"wrap_line_length" : "0" ,
"indent_inner_html" : true ,
"e4x" : true ,
"indent_empty_lines" : false
}
switch ( mode ) {
case "xml" :
2024-07-07 15:20:19 +03:00
//console.log('sorting as XML')
2024-07-05 13:19:38 +03:00
let xml = new DOMParser ( ) . parseFromString ( source , 'application/xml' ) ;
var sorting _params = new DOMParser ( ) . parseFromString ( [
// describes how I want to modify the XML - indent everything
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' ,
' <xsl:strip-space elements="*"/>' ,
' <xsl:template match="para[content-style][not(text())]">' , // change to just text() to strip space in text nodes
' <xsl:value-of select="normalize-space(.)"/>' ,
' </xsl:template>' ,
' <xsl:template match="node()|@*">' ,
' <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>' ,
' </xsl:template>' ,
' <xsl:output indent="yes"/>' ,
'</xsl:stylesheet>' ,
] . join ( '\n' ) , 'application/xml' ) ;
let processor = new XSLTProcessor ( ) ;
processor . importStylesheet ( sorting _params ) ;
let xml _doc = processor . transformToDocument ( xml )
let sub _result = new XMLSerializer ( ) . serializeToString ( xml _doc ) ;
if ( ! ~ sub _result . indexOf ( 'parsererror' ) ) {
result = sub _result
} else {
result = html _beautify ( source , options )
}
break
case 'html' :
result = html _beautify ( source , options )
break
case 'css' :
result = css _beautify ( source , options )
break
case 'application/ld+json' :
result = js _beautify ( source , options )
break
case 'javascript' :
result = js _beautify ( source , options )
break
}
return result
} ;
function beautify _button _click ( event ) {
let mode = document . getElementById ( this . getAttribute ( 'syntax_type_selector' ) ) . value
let editor _name = this . getAttribute ( 'editor' )
//console.log('beautify click', event, editor_name, mode )
let editor = window . exports [ editor _name ]
let new _code = code _beautify ( editor . getValue ( ) , mode )
//console.log(new_code)
editor . setValue ( new _code )
}
function open _tab ( event , element ) {
let tabview = element || this
while ( ! tabview . classList . contains ( "tabview" ) ) {
tabview = tabview . parentElement
}
let panes = tabview . getElementsByClassName ( "tabpanel" )
// only immediate children (depth==1) should be affected by show/hide
for ( let pane of panes ) {
// console.log("========= this",this)
// console.log("========= elem",element)
// console.log("========= jeq?",tabview == pane.parentElement)
// console.log("========= tabv",tabview)
// console.log("========= pane",pane.parentElement)
if ( ~ pane . id . indexOf ( this . value ) && tabview == pane . parentElement ) {
pane . style . display = "block" ;
// console.log(pane.id, 'block')
} else if ( tabview == pane . parentElement ) {
pane . style . display = "none" ;
// console.log(pane.id, 'none')
} else {
// console.log(pane.id, 'else')
}
if ( this . value == "authorization" ) {
let auth _type _select = document . getElementById ( "auth_type_select" )
show _auth _tab ( auth _type _select . value )
}
if ( this . value == "raw" ) {
let body _type _select = document . getElementById ( "body_type_select" )
body _type _select . style . display = "inline"
} else {
let body _type _select = document . getElementById ( "body_type_select" )
body _type _select . style . display = "none"
}
}
}
function enable _row ( ) {
let row = this
while ( ! ( row . tagName . toLowerCase ( ) == "tr" ) ) {
row = row . parentElement
}
let inputs = row . getElementsByTagName ( 'input' )
let key _found
for ( let input of inputs ) {
if ( input . classList . contains ( 'key' ) && input . value . trim ( ) != '' ) {
key _found = true
}
}
if ( key _found ) {
let checkbox = row . getElementsByClassName ( 'enable_row' ) [ 0 ]
checkbox . checked = true
}
}
2024-07-07 15:20:19 +03:00
function delete _row ( event , element ) {
//console.log("removing row", this, element)
2024-07-05 13:19:38 +03:00
// find a row *this* button is from
let row = element || this
2024-07-07 15:20:19 +03:00
while ( ! ( row ? . tagName ? . toLowerCase ( ) == "tr" ) ) {
//console.log("row",row, row?.tagName)
2024-07-05 13:19:38 +03:00
row = row . parentElement
}
// the last row's button is always hidden, so there's no way to delete it
row . remove ( )
// tus, all of this is not needed:
// // find the table *this* button's row is from
// let table = row
// while (!(table.tagName.toLowerCase() =="table")) {
// table = table.parentElement
// }
// // get row length of the table
// let rows = table.getElementsByTagName("tr")
// // get last row
// let last_row = rows[rows.length-1]
// // if this isn't the last row then delete (the last row is always empty and its addition is automatized)
// if (row != last_row) {
// row.remove()
// } else {
// console.log("don't delete, this is the last row")
// }
}
function select _all ( event ) {
if ( event . code == 'KeyA' && event . ctrlKey ) {
let range = document . createRange ( ) ;
let element = this
range . selectNodeContents ( element )
let selection = window . getSelection ( ) ;
selection . removeAllRanges ( ) ;
selection . addRange ( range ) ;
}
}
// remove ctrl+a across the app, allowing to select only what is fine to be selected
document . onkeydown = function ( event )
{
if ( ( event . target . tagName . toLowerCase ( ) != 'input' ) && ( event . target . tagName . toLowerCase ( ) != 'textarea' ) ) {
if ( event . code == 'KeyA' && event . ctrlKey == true ) {
return false ;
}
}
}
function add _row2 ( event , element ) {
//document.getElementById("url_input").value = stringify_request_query_params()
let row = element || this
while ( ! ( row . tagName . toLowerCase ( ) == "tr" ) ) {
row = row . parentElement
}
let inputs = row . getElementsByTagName ( 'input' )
let key _found
let value _found
for ( let input of inputs ) {
if ( input . classList . contains ( 'key' ) && input . value . trim ( ) != '' ) {
key _found = true
}
if ( input . classList . contains ( 'value' ) && input . value . trim ( ) != '' ) {
value _found = true
}
}
if ( key _found && value _found ) {
add _row ( event , row )
}
}
function add _row ( event , element , data ) {
2024-07-07 15:20:19 +03:00
console . log ( "adding row" , data )
2024-07-05 13:19:38 +03:00
let table = element || this
while ( ! table . classList . contains ( "request_params_table" ) ) {
2024-07-07 15:20:19 +03:00
//console.log(table.parentElement)
2024-07-05 13:19:38 +03:00
table = table . parentElement
}
let rows = table . getElementsByTagName ( 'tr' )
if ( rows . length > 2 ) {
let last _row = rows [ rows . length - 1 ]
let tds = last _row . getElementsByTagName ( "input" )
let tabpanel = table
while ( ! ( tabpanel . classList ? . contains ( 'tabpanel' ) ) ) {
tabpanel = tabpanel . parentElement
}
if ( event && event . code == 'ArrowDown' ) {
tabpanel . scrollTo ( 0 , tabpanel . scrollHeight ) ;
tds [ 1 ] . focus ( )
}
// inputs: checkbox, 3*text, button (not an input)
if ( tds [ 1 ] . value != "" && tds [ 2 ] . value != "" ) {
} else {
2024-07-07 15:20:19 +03:00
// prevents addition of new empty rows upon key press
if ( ! data ) {
console . log ( "here?" )
return
}
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
} else {
2024-07-05 13:19:38 +03:00
}
let tr = document . createElement ( 'tr' )
let td1 = document . createElement ( 'td' )
let td2 = document . createElement ( 'td' )
let td3 = document . createElement ( 'td' )
let td4 = document . createElement ( 'td' )
let td5 = document . createElement ( 'td' )
let lb = document . createElement ( 'label' )
let cb = document . createElement ( 'input' )
let db = document . createElement ( 'input' )
let ed1 = document . createElement ( 'input' )
let ed2 = document . createElement ( 'input' )
let ed3 = document . createElement ( 'input' )
ed1 . type = 'text'
ed1 . classList . add ( 'key' )
ed2 . type = 'text'
ed2 . classList . add ( 'value' )
ed3 . type = 'text'
cb . type = 'checkbox'
cb . classList . add ( 'enable_row' )
db . type = 'button'
db . value = '🗑️'
db . addEventListener ( "click" , delete _row ) ;
ed1 . addEventListener ( "blur" , enable _row ) ;
ed1 . addEventListener ( "keyup" , add _row2 ) ;
ed2 . addEventListener ( "keyup" , add _row2 ) ;
ed3 . addEventListener ( "keyup" , add _row2 ) ;
lb . appendChild ( cb )
td1 . appendChild ( lb )
td2 . appendChild ( ed1 )
td3 . appendChild ( ed2 )
td4 . appendChild ( ed3 )
td5 . appendChild ( db )
tr . appendChild ( td1 )
tr . appendChild ( td2 )
tr . appendChild ( td3 )
tr . appendChild ( td4 )
tr . appendChild ( td5 )
2024-07-07 15:20:19 +03:00
table . appendChild ( tr )
2024-07-05 13:19:38 +03:00
if ( data ) {
console . log ( "data" , data )
ed1 . value = data . key || ""
ed2 . value = data . value || ""
ed3 . value = data . descpription || ""
cb . checked = data . enabled || false
2024-07-07 15:20:19 +03:00
// this is needed because when I manually enter stuff, it add a new row below the current one
// but when I call this from the code, it inserts empty rows after each `data`
if ( ! data . prevent _addition _of _a _new _row ) {
let event = new Event ( 'keyup' )
ed1 . dispatchEvent ( event ) ;
}
2024-07-05 13:19:38 +03:00
}
}
2024-07-07 15:20:19 +03:00
function option _selected ( event , element ) {
let combobox = element || this
2024-07-05 13:19:38 +03:00
while ( ! ( combobox . classList ? . contains ( 'dropdown' ) ) ) {
combobox = combobox . parentElement
}
let button = combobox . getElementsByClassName ( 'dropbtn' ) [ 0 ]
let menu = combobox . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || combobox . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
2024-07-07 15:20:19 +03:00
let style = window . getComputedStyle ( element || this )
2024-07-05 13:19:38 +03:00
let color = style . getPropertyValue ( 'color' )
2024-07-07 15:20:19 +03:00
button . value = ( element || this ) . getAttribute ( 'value' )
2024-07-05 13:19:38 +03:00
if ( ~ button . id . indexOf ( "body_type_select" ) ) {
let editor = window . exports [ button . getAttribute ( 'editor' ) ]
console . log ( 'settings mode' , button . value )
if ( button . value == 'html' ) {
editor . setOption ( "mode" , {
name : "htmlmixed" ,
tags : {
style : [ [ "type" , /^text\/(x-)?scss$/ , "text/x-scss" ] ,
[ null , null , "css" ] ] ,
custom : [ [ null , null , "customMode" ] ]
}
} ) ;
} else {
editor . setOption ( "mode" , button . value ) ;
}
if ( button . value == 'javascript' ) {
editor . setOption ( "lint" , true )
} else {
editor . setOption ( "lint" , false )
}
}
if ( button . id == "metod_select" ) {
button . style . color = color
}
if ( button . id == "auth_type_select" ) {
show _auth _tab ( button . value )
}
2024-07-07 15:20:19 +03:00
button . textContent = ( element || this ) . textContent
2024-07-05 13:19:38 +03:00
menu . style . display = 'none'
}
function open _combobox ( ) {
let combobox = this
while ( ! ( combobox . classList ? . contains ( 'dropdown' ) ) ) {
combobox = combobox . parentElement
}
let opened = ( JSON . parse ( combobox . getAttribute ( 'opened' ) ) )
2024-07-07 15:20:19 +03:00
//console.log(opened)
2024-07-05 13:19:38 +03:00
if ( opened ) {
let menu = combobox . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || combobox . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
// menu_items might not be there if there's no submenu
if ( menu ) {
menu . style . display = 'none'
}
} else {
let menu = combobox . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || combobox . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
let button = combobox . getElementsByClassName ( 'dropbtn' ) [ 0 ]
let items = menu . getElementsByClassName ( 'dropdown-item' )
for ( let item of items ) {
if ( item . getAttribute ( 'value' ) == button . value ) {
item . setAttribute ( 'selected' , true )
} else {
item . setAttribute ( 'selected' , false )
}
}
button . value = this . textContent
menu . style . display = 'block'
}
combobox . setAttribute ( 'opened' , ! opened )
}
function init _comboboxes ( ) {
let comboboxes _names = [ "metod_select" , "body_type_select" , "response_body_type_select" , "auth_type_select" ]
for ( let combobox _name of comboboxes _names ) {
let combobox = document . getElementById ( combobox _name )
let select = combobox . parentElement
let button = select . getElementsByClassName ( 'dropbtn' ) [ 0 ]
let options = select . getElementsByClassName ( 'dropdown-item' )
for ( let option of options ) {
option . addEventListener ( 'click' , option _selected )
}
button . addEventListener ( 'click' , open _combobox )
}
}
function open _menu ( event ) {
close _all _menus ( event )
let menu = this
while ( ! ( menu . classList ? . contains ( 'dropdown' ) ) ) {
menu = menu . parentElement
}
let navbar = menu
while ( ! ( navbar . classList ? . contains ( 'navbar' ) ) ) {
navbar = navbar . parentElement
}
let opened = ! ( JSON . parse ( navbar . getAttribute ( 'opened' ) ) )
navbar . setAttribute ( 'opened' , opened )
let menu _items = menu . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || menu . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
if ( opened ) {
menu _items . style . display = 'block'
} else {
menu _items . style . display = 'none'
}
}
function menu _item _clicked ( ) {
let menu = this
// process the click --------
switch ( this . id ) {
// file
case "app_exit_menu_item" :
app . process . exit ( 0 ) ;
break ;
case "password_generator_menu_item" :
let pass = btoa ( String . fromCharCode . apply ( null , crypto . getRandomValues ( new Uint8Array ( 18 ) ) ) )
app . clipboard . writeText ( pass )
break ;
default :
let popup _id = this . id . replace ( "_menu_item" , "" )
let popup = document . getElementById ( popup _id )
popup . style . display = 'flex'
break ;
}
// --------------------------
while ( ! ( menu . classList ? . contains ( 'dropdown' ) ) ) {
menu = menu . parentElement
}
let navbar = menu
while ( ! ( navbar . classList ? . contains ( 'navbar' ) ) ) {
navbar = navbar . parentElement
}
navbar . setAttribute ( 'opened' , false )
let menu _items = menu . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || menu . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
menu _items . style . display = 'none'
}
function enter _menu ( ) {
let menu = this
while ( ! ( menu . classList ? . contains ( 'dropdown' ) ) ) {
menu = menu . parentElement
}
let navbar = menu
while ( ! ( navbar . classList ? . contains ( 'navbar' ) ) ) {
navbar = navbar . parentElement
}
let opened = ( JSON . parse ( navbar . getAttribute ( 'opened' ) ) )
if ( opened ) {
let menus = navbar . getElementsByClassName ( 'dropdown' )
for ( let me of menus ) {
let menu _items = me . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || me . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
// menu_items might not be there if there's no submenu
if ( menu _items ) {
if ( me != menu ) {
menu _items . style . display = 'none'
} else {
menu _items . style . display = 'block'
}
}
}
}
}
function close _all _menus ( event ) {
if ( event ? . srcElement != this && ( event ? . srcElement ? . classList ? . contains ( 'dropdown' ) || event ? . srcElement ? . classList ? . contains ( 'dropbtn' ) ) ) {
return
}
let menus = this . getElementsByClassName ( 'dropdown' )
for ( let me of menus ) {
let menu _items = me . getElementsByClassName ( 'dropdown-content' ) [ 0 ] || me . getElementsByClassName ( 'dropdown-content-replace' ) [ 0 ]
// menu_items might not be there if there's no submenu
if ( menu _items ) {
menu _items . style . display = 'none'
}
let opened = ( JSON . parse ( me . getAttribute ( 'opened' ) ) )
//me.setAttribute('opened', !opened) // this fixes menus opening on 2nd click after select+ 2/more clicks anywhere else
me . setAttribute ( 'opened' , false )
}
let navbars = this . getElementsByClassName ( 'navbar' )
for ( let navbar of navbars ) {
navbar . setAttribute ( 'opened' , false )
}
}
function init _menus ( ) {
// firstly, add global closing menu callback
document . getElementsByTagName ( "body" ) [ 0 ] . addEventListener ( "click" , close _all _menus ) ;
// secondly, make all menus work
let menus = document . getElementsByClassName ( "navbar" )
for ( let menu of menus ) {
let buttons = menu . getElementsByClassName ( 'dropbtn' )
for ( let button of buttons ) {
let items = menu . getElementsByClassName ( 'dropdown-item' )
for ( let item of items ) {
item . addEventListener ( 'click' , menu _item _clicked )
}
button . addEventListener ( 'click' , open _menu )
button . addEventListener ( 'mouseenter' , enter _menu )
}
}
}
function hide _element ( ) {
this . style . display = 'none'
}
function init _popups ( ) {
let popups = document . getElementsByClassName ( "popup_wrapper" )
for ( let popup of popups ) {
popup . addEventListener ( 'click' , hide _element )
}
}
2024-07-07 15:20:19 +03:00
async function keyboard _shortcuts _handler ( event ) {
//console.log(event)
if ( event . ctrlKey && event . code === 'KeyS' ) {
2024-07-05 13:19:38 +03:00
// save current request
2024-07-07 15:20:19 +03:00
let request = await build _request ( )
app . current _request = JSON . parse ( JSON . stringify ( request ) )
await write _request _file ( app . current _tab )
2024-07-05 13:19:38 +03:00
2024-07-07 15:20:19 +03:00
} else if ( event . ctrlKey && event . code === "Enter" ) {
2024-07-05 13:19:38 +03:00
// run qurent request
2024-07-07 15:20:19 +03:00
await send _request ( )
2024-07-05 13:19:38 +03:00
}
}
function handle _buttons ( self ) {
2024-07-07 15:20:19 +03:00
//console.log(self.id)
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
function set _modified ( id , yes ) {
let button = document . getElementById ( id )
//console.log(button)
if ( yes ) {
// make tab appear modified
button . setAttribute ( "modified" , true )
} else {
// make tab appear unmodified
button . setAttribute ( "modified" , false )
}
}
2024-07-05 13:19:38 +03:00
2024-07-07 15:20:19 +03:00
async function write _request _file ( request _id , is _modified ) {
if ( request _id ) {
try {
// I have 2 files: tmp_ and the regular one
let filename = request _id + '.json'
if ( is _modified ) {
// if modified, then write only to the _tmp one
filename = "tmp_" + filename
console . log ( "writing " + filename )
await app . writeTextFile ( filename , JSON . stringify ( app . current _request , null , 2 ) , { baseDir : 15 } ) // localappdata
} else {
// if NOT modified (i.e. saved), saving goes to BOTH files (since we first read the tmp file)
console . log ( "writing " + filename )
await app . writeTextFile ( filename , JSON . stringify ( app . current _request , null , 2 ) , { baseDir : 15 } ) // localappdata
filename = "tmp_" + filename
console . log ( "writing " + filename )
await app . writeTextFile ( filename , JSON . stringify ( app . current _request , null , 2 ) , { baseDir : 15 } ) // localappdata
}
// RESULT: the _tmp file is always saved and loaded first
// permanent changes go to the ergular file (the one eventually shared across dvices)
set _modified ( request _id , is _modified )
} catch ( err ) {
console . log ( 'can\'t write file' , err )
}
// this ^ never returns anything
} else {
console . log ( "Can't write the file, as request_id is undefined" )
2024-07-05 13:19:38 +03:00
}
}
async function read _request _file ( request _id ) {
console . log ( "reading " + request _id + '.json' )
try {
2024-07-07 15:20:19 +03:00
let filename = request _id + '.json'
let data
try {
data = await app . readTextFile ( "tmp_" + filename , { baseDir : 15 } )
} catch ( err ) {
try {
data = await app . readTextFile ( filename , { baseDir : 15 } )
} catch ( err ) {
}
}
console . log ( "read file contents:" + data )
let result = JSON . parse ( data ) // if data is null, this will fail and catch() will fire
return result
2024-07-05 13:19:38 +03:00
} catch ( err ) {
2024-07-07 15:20:19 +03:00
console . log ( err )
2024-07-05 13:19:38 +03:00
}
}
let scrolling _tab _bar _enabled
function main _scroll _tabbar _start ( ) {
clearInterval ( scrolling _tab _bar _enabled )
let scrollable = document . getElementById ( 'request_bar_scroll_area' )
if ( this . id == 'request_row_right_button' ) {
scrolling _tab _bar _enabled = setInterval (
function ( ) {
scrollable . scrollLeft = scrollable . scrollLeft + 10
}
)
} else {
scrolling _tab _bar _enabled = setInterval (
function ( ) {
scrollable . scrollLeft = scrollable . scrollLeft - 10
}
)
}
}
function main _scroll _tabbar _end ( ) {
clearInterval ( scrolling _tab _bar _enabled )
}
function main _scroll _tabbar _wheel ( event ) {
let delta = event . deltaY
this . scrollLeft = this . scrollLeft + delta
}
2024-07-07 15:20:19 +03:00
async function request _tab _click ( event ) {
console . log ( "CLICKED!!!!!!!!!!" )
let request = await build _request ( )
app . current _request = JSON . parse ( JSON . stringify ( request ) )
let current _tab = document . getElementById ( app . current _tab )
let is _modified = JSON . parse ( current _tab . getAttribute ( "modified" ) )
//console.log("setting modified inside tab_click", is_modified)
if ( is _modified ) { // true/false shenanigans
await write _request _file ( app . current _tab , true )
} else {
await write _request _file ( app . current _tab )
}
2024-07-05 13:19:38 +03:00
2024-07-07 15:20:19 +03:00
2024-07-05 13:19:38 +03:00
if ( ( this . scrollWidth - event . offsetX < 25 * 1.34 ) || event . target . classList . contains ( 'close_button' ) ) {
let opened _tabs = [ ]
for ( let tid of app . opened _tabs ) {
if ( this . id != tid ) {
opened _tabs . push ( tid )
}
}
app . opened _tabs = opened _tabs
this . remove ( )
} else {
let tab _id = this . id
2024-07-07 15:20:19 +03:00
//console.log("^^^^^^^^^^^^^^^^^^^^^^")
//console.log("tab_id", tab_id)
//console.log("app.opened_tabs", app.opened_tabs)
2024-07-05 13:19:38 +03:00
for ( let tid of app . opened _tabs ) {
2024-07-07 15:20:19 +03:00
//console.log("tid", tid)
2024-07-05 13:19:38 +03:00
if ( tab _id == tid ) {
app . current _tab = tid
break
}
}
2024-07-07 15:20:19 +03:00
//console.log("vvvvvvvvvvvvvvvvvvvvvv")
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
await manage _tab _history ( ) // this also manages file reading/writing
2024-07-05 13:19:38 +03:00
adjust _main _tabbar _buttons ( )
}
2024-07-07 15:20:19 +03:00
async function manage _tab _history ( new _tab ) {
2024-07-05 13:19:38 +03:00
// unconditionally add current tab to the history
app . tab _history . push ( app . current _tab )
console . log ( "adding " + app . current _tab + ' to the history' )
console . log ( "app.tab_history" , app . tab _history )
console . log ( "app.opened_tabs" , app . opened _tabs )
// then get all the currently opened tabs and delete closed ones from the history
let tabs = app . opened _tabs
let tmp _history = [ ]
let last _added
for ( let hid of app . tab _history ) {
2024-07-07 15:20:19 +03:00
//console.log("hid", hid)
2024-07-05 13:19:38 +03:00
for ( let tid of tabs ) {
2024-07-07 15:20:19 +03:00
//console.log("tid", tid, tid == hid)
2024-07-05 13:19:38 +03:00
if ( tid == hid ) {
if ( last _added != hid ) {
tmp _history . push ( hid )
last _added = hid
} else {
2024-07-07 15:20:19 +03:00
//console.log("removed duplicate item", hid)
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
//console.log("break")
2024-07-05 13:19:38 +03:00
break
}
}
}
2024-07-07 15:20:19 +03:00
//console.log("tmp_history", tmp_history)
2024-07-05 13:19:38 +03:00
app . tab _history = tmp _history
let tab _id = tmp _history [ tmp _history . length - 1 ]
for ( let tid of app . opened _tabs ) {
2024-07-07 15:20:19 +03:00
//console.log(tab_id, tid)
2024-07-05 13:19:38 +03:00
if ( tid == tab _id ) {
app . current _tab = tid
break
}
}
// whatever in that file gets into current request data structure (and I can expect this to alvays be there)
2024-07-07 15:20:19 +03:00
if ( ! new _tab ) {
try {
app . current _request = await read _request _file ( app . current _tab )
console . log ( "app.current_request" , app . current _request )
} catch ( err ) {
// this is fine if there's no file to read (i.e. it's a new tab)
console . log ( err )
}
} else {
app . current _request = build _empty _request ( )
}
// fills UI with data from the file
fill _in _forms ( )
}
function build _empty _request ( ) {
let result = {
method : "GET" ,
url : "" ,
base _url : "" ,
query _params : { } ,
headers : [ ] ,
auth _type : "none" ,
body _type : "none" ,
scripts : {
pre : "" ,
post : ","
}
}
return result
}
function fill _in _forms ( ) {
if ( app . current _request ) { //
console . log ( 'filling forms with' , app . current _request )
let container = document . getElementById ( "request_body_wrapper" )
let tables = container . getElementsByClassName ( "request_params_table" )
2024-07-05 13:19:38 +03:00
2024-07-07 15:20:19 +03:00
for ( let table of tables ) {
table . innerHTML = "<tr><th>✅</th><th>Key</th><th>Value</th><th>Description</th><th>🗑️</th></tr>"
let param _count = 0
for ( let pid in app . current _request ? . query _params ) {
param _count = param _count + 1
}
}
// fill in headers
let headers _table = document . getElementById ( "headers_table" )
for ( let hid in app . current _request . headers ) {
add _row ( null , headers _table , { key : hid , value : app . current _request . headers [ hid ] , enabled : true } )
}
let query _params _table = document . getElementById ( "query_params_table" )
console . log ( "add rows start =========================" )
for ( let qpid in app . current _request . query _params ) {
console . log ( qpid , app . current _request . query _params [ qpid ] )
add _row ( null , query _params _table , { key : qpid , value : app . current _request . query _params [ qpid ] , enabled : true , prevent _addition _of _a _new _row : true } )
}
console . log ( "add rows end ===========================" )
let metod _select = document . getElementById ( "metod_select" )
metod _select . value = app . current _request . method || ""
let url _input = document . getElementById ( "url_input" )
url _input . value = app . current _request . base _url || ""
let auth _type _select = document . getElementById ( "auth_type_select" )
let items = auth _type _select . parentElement . getElementsByClassName ( "dropdown-item" )
//console.log("items",items)
for ( let item of items ) {
if ( item . getAttribute ( "value" ) == app . current _request . auth _type ) {
//console.log("clicking",item)
option _selected ( null , item )
break
}
}
auth _type _select . value = app . current _request . auth _type
///===============================================================================
let auth _panel = document . getElementById ( 'auth_right_side' )
let auth _inputs = auth _panel . getElementsByClassName ( "auth_input" )
for ( let input of auth _inputs ) {
if ( input . classList . contains ( "username_input" ) ) {
input . value = app . current _request . user || ""
//console.log("setting username",input.id)
}
if ( input . classList . contains ( "password_input" ) ) {
input . value = app . current _request . password || ""
//console.log("setting password",input.id)
}
if ( input . classList . contains ( "token_input" ) ) {
input . value = app . current _request . token || ""
//console.log("setting token",input.id)
}
if ( input . classList . contains ( "domain_input" ) ) {
input . value = app . current _request . domain || ""
//console.log("setting domain",input.id)
}
if ( input . classList . contains ( "workstation_input" ) ) {
input . value = app . current _request . workstation || ""
//console.log("setting workstation",input.id)
}
}
// process body
let request _body _tabbar = document . getElementById ( 'request_body_tabbar' )
let radios = request _body _tabbar . getElementsByClassName ( 'tab_header_button' )
for ( let radio of radios ) {
if ( radio . value == app . current _request . body _type ) {
radio . click ( )
}
}
for ( let table of tables ) {
add _row ( null , table )
}
///===============================================================================
} else {
console . log ( 'can\'t fill anything as app.current_request is empty' )
}
2024-07-05 13:19:38 +03:00
}
2024-07-07 15:20:19 +03:00
2024-07-05 13:19:38 +03:00
function adjust _main _tabbar _buttons ( ) {
let request _bar _scroll _area = document . getElementById ( 'request_bar_scroll_area' )
let enable _scroll _buttons = request _bar _scroll _area . scrollWidth > ( document . getElementById ( 'request_area' ) . clientWidth - 150 )
2024-07-07 15:20:19 +03:00
//console.log(enable_scroll_buttons)
2024-07-05 13:19:38 +03:00
let request _row _right _button = document . getElementById ( 'request_row_right_button' )
let request _row _left _button = document . getElementById ( 'request_row_left_button' )
let buttons = request _bar _scroll _area . getElementsByClassName ( "request_tabbutton" )
for ( let button of buttons ) {
if ( enable _scroll _buttons ) {
request _row _right _button . style . display = "initial"
request _row _left _button . style . display = "initial"
button . classList . remove ( "request_tabbutton_few" )
} else {
button . classList . add ( "request_tabbutton_few" )
request _row _right _button . style . display = "none"
request _row _left _button . style . display = "none"
}
if ( button . id == app . current _tab ) {
button . style . backgroundColor = "#444"
} else {
button . style . backgroundColor = "#222"
}
}
let sendbar = document . getElementById ( "sendbar" )
let current _request _path = document . getElementById ( "current_request_path" )
let request _data _wrapper = document . getElementById ( "request_data_wrapper" )
let starting _message = document . getElementById ( "starting_message" )
if ( buttons . length < 1 ) {
// disable right side
sendbar . style . display = 'none'
current _request _path . style . display = 'none'
request _data _wrapper . style . display = 'none'
starting _message . style . display = 'flex'
} else {
// enable right side
sendbar . style . display = 'flex'
current _request _path . style . display = 'block'
request _data _wrapper . style . display = 'block'
starting _message . style . display = 'none'
}
let tabs = document . getElementsByClassName ( "request_tabbutton" )
app . opened _tabs = [ ]
for ( let tab of tabs ) {
// неправльно, табы должны быть в памяти ВСЕГДА, т.к. в них вся инфа по запросу.
// or is it?
// every tab is a structure, saved on a disc
// opened a tab = read a file
// saved a tab = wrote a file
// closed a tab = asked if wanna save
// created a tab = *nothing*
// shared a tab
// so I can safely recreate tabs like this
app . opened _tabs . push ( tab . id )
}
}
function window _resize _trigger ( ) {
adjust _main _tabbar _buttons ( )
}
2024-07-07 15:20:19 +03:00
async function add _request ( ) {
if ( app . current _tab ) {
let request = await build _request ( )
app . current _request = JSON . parse ( JSON . stringify ( request ) )
let current _tab = document . getElementById ( app . current _tab )
let is _modified = JSON . parse ( current _tab . getAttribute ( "modified" ) )
//console.log("setting modified inside tab_click", is_modified)
if ( is _modified ) { // true/false shenanigans
await write _request _file ( app . current _tab , true )
} else {
await write _request _file ( app . current _tab )
}
}
2024-07-05 13:19:38 +03:00
let request _id = crypto . randomUUID ( )
let request _name = "New Request" //+ " " + app.opened_tabs.length
let container = document . getElementById ( "request_body_wrapper" )
let tables = container . getElementsByClassName ( "request_params_table" )
for ( let table of tables ) {
table . innerHTML = "<tr><th>✅</th><th>Key</th><th>Value</th><th>Description</th><th>🗑️</th></tr>"
2024-07-07 15:20:19 +03:00
console . log ( "add request" )
2024-07-05 13:19:38 +03:00
add _row ( null , table )
}
let tab _button = document . createElement ( 'button' )
tab _button . classList . add ( "request_tabbutton" )
tab _button . innerHTML = request _name + '<span class="close_button">❌</span>'
tab _button . id = request _id
tab _button . addEventListener ( "click" , request _tab _click )
app . opened _tabs . push ( request _id )
app . current _tab = request _id
let request _bar _scroll _area = document . getElementById ( 'request_bar_scroll_area' )
request _bar _scroll _area . scrollBy ( Number . MAX _SAFE _INTEGER , 0 )
request _bar _scroll _area . appendChild ( tab _button )
2024-07-07 15:20:19 +03:00
// set modified flag, cause it's not saved
set _modified ( request _id , true )
2024-07-05 13:19:38 +03:00
2024-07-07 15:20:19 +03:00
await manage _tab _history ( true ) // pass true to indincate this is a new tab
2024-07-05 13:19:38 +03:00
adjust _main _tabbar _buttons ( )
}
function show _auth _tab ( id ) {
let auth _right _side = document . getElementById ( "auth_right_side" )
let tabpanels = auth _right _side . getElementsByClassName ( "tabpanel" )
for ( let tabpanel of tabpanels ) {
if ( tabpanel . id == "auth_panel_" + id ) {
tabpanel . style . display = "block" ;
} else {
tabpanel . style . display = "none" ;
}
}
}
function init ( ) {
console . log ( 'init' )
// enable add_row buttons (those adding rows to param tables)
let add _row _buttons = document . getElementsByClassName ( 'add_row_button' )
for ( let element of add _row _buttons ) {
element . addEventListener ( "click" , add _row ) ;
}
// enable tab switching for request params tab bar
let tab _headers = document . getElementsByClassName ( 'tab_header_button' )
for ( let element of tab _headers ) {
element . addEventListener ( "click" , open _tab ) ;
}
// add ctrl+a functionality to the response output element
//document.getElementById("response_body").addEventListener("keydown", select_all);
// enable main tab bar scrolling with button presses
let request _row _right _button = document . getElementById ( 'request_row_right_button' )
let request _row _left _button = document . getElementById ( 'request_row_left_button' )
request _row _right _button . addEventListener ( "mousedown" , main _scroll _tabbar _start ) ;
request _row _right _button . addEventListener ( "mouseup" , main _scroll _tabbar _end ) ;
request _row _left _button . addEventListener ( "mousedown" , main _scroll _tabbar _start ) ;
request _row _left _button . addEventListener ( "mouseup" , main _scroll _tabbar _end ) ;
// enable main tab bar scrolling (mousewheel)
let request _bar _scroll _area = document . getElementById ( 'request_bar_scroll_area' )
request _bar _scroll _area . addEventListener ( "wheel" , main _scroll _tabbar _wheel ) ;
init _comboboxes ( )
init _menus ( )
init _popups ( )
// show request query params tab
let tab _header _buttons = document . getElementsByClassName ( "tab_header_button" )
for ( let button of tab _header _buttons ) {
if ( button . checked ) {
let event = new Event ( 'click' )
button . dispatchEvent ( event ) ;
}
}
let beautify _raw _data _body = document . getElementById ( 'beautify_raw_data_body' )
let beautify _raw _data _response = document . getElementById ( 'beautify_raw_data_response' )
beautify _raw _data _body . addEventListener ( "click" , beautify _button _click ) ;
beautify _raw _data _response . addEventListener ( "click" , beautify _button _click ) ;
let add _request _button = document . getElementById ( 'add_request_button' )
2024-07-07 15:20:19 +03:00
let mock _button = document . getElementById ( 'mock_button' )
2024-07-05 13:19:38 +03:00
add _request _button . addEventListener ( "click" , add _request ) ;
2024-07-07 15:20:19 +03:00
mock _button . addEventListener ( "click" , add _request ) ;
2024-07-05 13:19:38 +03:00
document . addEventListener ( "keydown" , keyboard _shortcuts _handler ) ;
setTimeout ( ( ) => {
let style = document . getElementById ( "animations" )
style . textContent = '* {transition: all 0.1s;} .tabbar label {transition: all 0.5s;} .editor-container * {transition:none }'
} , 1000 )
//dirty hack for debug
let event1 = new Event ( 'click' )
body _editor . setValue ( '<label><input type="radio" name="request_params_tabbar_body" value="none" class="tab_header_button" checked >none</label>' )
window . addEventListener ( "resize" , window _resize _trigger )
fill _mock _data ( )
test ( )
}
function fill _mock _data ( ) {
2024-07-07 15:20:19 +03:00
let container = document . getElementById ( "request_body_wrapper" )
let tables = container . getElementsByClassName ( "request_params_table" )
for ( let table of tables ) {
table . innerHTML = "<tr><th>✅</th><th>Key</th><th>Value</th><th>Description</th><th>🗑️</th></tr>"
add _row ( null , table )
}
2024-07-05 13:19:38 +03:00
let auth _panel = document . getElementById ( 'auth_right_side' )
let auth _inputs = auth _panel . getElementsByClassName ( "auth_input" )
for ( let input of auth _inputs ) {
if ( input . classList . contains ( "username_input" ) ) {
2024-07-07 15:20:19 +03:00
//input.value = "ezwrk@ntrnx.ru"
2024-07-05 13:19:38 +03:00
}
if ( input . classList . contains ( "password_input" ) ) {
2024-07-07 15:20:19 +03:00
//input.value = "7VOlvV3frvhYJhDiHNf/xh6p"
2024-07-05 13:19:38 +03:00
}
if ( input . classList . contains ( "token_input" ) ) {
input . value = "eyJhbGciOiJSUzI1NiIsImtpZCI6IkE3QTY1NDE0OEM1MjMxRUE3QzAwRUNERDBEMTRDRjBGIiwidHlwIjoiYXQrand0In0.eyJuYmYiOjE3MjAwMTU5NzgsImV4cCI6MTc1MTU1MTk3OCwiaXNzIjoiaHR0cHM6Ly9kZW1vMS1yZWlzLmRldi5udHJueC5jb20iLCJhdWQiOiJSZWNvcmRlciIsImNsaWVudF9pZCI6IlJlY29yZGVyX1NQQSIsInN1YiI6IjQxNDUwYzQ2LTQ3ZmMtNDc1Yi1iNTRiLWE0Mzk2OWUyMzE0NiIsImF1dGhfdGltZSI6MTcyMDAxNTk3OCwiaWRwIjoibG9jYWwiLCJ0ZW5hbnRpZCI6Ijg0MWEyMjEzLTg4NWEtNGM5MS1iYzI3LWVhMzhmNjI3NDE0YiIsImVtYWlsIjoiYWRtaW5AZGVmYXVsdHRlbmFudC5jb20iLCJyb2xlIjpbImFkbWluIiwiUmVnaXN0ZXJlZFVzZXIiXSwicGhvbmVfbnVtYmVyX3ZlcmlmaWVkIjoiRmFsc2UiLCJlbWFpbF92ZXJpZmllZCI6IlRydWUiLCJuYW1lIjoiYWRtaW4iLCJpYXQiOjE3MjAwMTU5NzgsInNjb3BlIjpbIlJlY29yZGVyIl0sImFtciI6WyJwd2QiXX0.YFaSFpTEo4KvsbHDQoooYoDNcIbLFr3x09TT0jB_pzJpl_00oIrgmc2GTzk5XNsWEl-Bq5m8quIA9VVHtwfJrJlSDjWg2MIpt-WrPD9r68BbMQNFuNjthyzfkiUvr06yANjSMbtMY-395EGRKck3g7Ro97CW_0qXdrc5GspO8yAJMdBWN_OtdK1fyUYr1Ai7TvxrGAFllMHQdBmhFvni8VZDksgv9fCaFBLkyOFpMgMyMy1L8zHc68XxIOVTLYdxG7jr90HCy1EZjOvX6C5qtiJEXqXD_2l1sASrUjG_R6oELoB07m4Q-nJ6_WH9eSpUXVksfSP0w7MaZvfC8S3jTQ"
}
}
let url _input = document . getElementById ( "url_input" )
2024-07-07 15:20:19 +03:00
//url_input.value = "https://demo1-recorder-api.dev.ntrnx.com/api/recorder/settings/notifications?api-version=1.0"
2024-07-05 13:19:38 +03:00
//url_input.value = "https://192.168.1.113:8443/web/save_isap_config"
//url_input.value = "http://reconf-ad-2.dev.ntrnx.local/Ews/Exchange.asmx"
2024-07-07 15:20:19 +03:00
url _input . value = "https://192.168.1.113:8443/api/hw-monitor"
//url_input.value = "https://wrong.host.badssl.com/"
2024-07-05 13:19:38 +03:00
2024-07-07 15:20:19 +03:00
//let auth_type_select = document.getElementById("auth_type_select")
//auth_type_select.value = 'bearer'
//auth_type_select.value = 'basic'
2024-07-05 13:19:38 +03:00
}
// fetch a long response ("send and download")
// async function fetchData() {
// const url = "https://www.dl.dropboxusercontent.com/s/7d87jcsh0qodk78/fuel_64x64x64_uint8.raw?dl=1";
// const response = await fetch(url);
// const reader = response.body.getReader();
// let length = 0;
// let value, done;
// while ({value, done} = await reader.read(), !done) {
// length += value.length;
// }
// console.log(length);
// }
// fetchData()
async function test ( ) {
2024-07-07 15:20:19 +03:00
// try {
// const response = await app.fetch("https://192.168.1.113:8443/api/hw-monitor");
// console.trace("fff")
// console.log(response.status);
// console.log(response.statusText);
// const jsonData = await response.json();
// console.log(jsonData)
// } catch(err) {
// console.log(err)
// }
// console.log(await app.invoke("api_post_req", { url:"https://wrong.host.badssl.com/"}));
2024-07-05 13:19:38 +03:00
}