10
0
mirror of https://github.com/LCPQ/QUESTDB_website.git synced 2024-11-04 13:13:55 +01:00

Use URLSearchParams API instead of manual or parseQueryStringToDictionary use

This commit is contained in:
Mickaël Véril 2019-10-30 13:28:32 +01:00
parent 02eb8d545a
commit f5d2df9811
3 changed files with 16 additions and 39 deletions

View File

@ -5,7 +5,6 @@ draft: false
---
<script src="/js/getFullDataPath.js" type="text/javascript"></script>
<script src="/js/getTextFromFile.js" type="text/javascript"></script>
<script src="/js/parseQueryStringToDictionary.js" type="text/javascript"></script>
<script>
window.onload=async function(){
@ -23,10 +22,10 @@ draft: false
}
console.warn(mystr);
}
var queryDic=parseQueryStringToDictionary()
var params=new URL(window.location.href).searchParams
var key=["file","fileBase64"]
if(key[0] in queryDic){
var filename=queryDic[key[0]];
if(params.has(key[0])){
var filename=params.get(key[0]);
var textPromise=getTextFromFileUrlAsync(filename)
$("#btn_download").click(function(){
window.open(filename,"_self");
@ -35,8 +34,9 @@ draft: false
document.getElementById('fileContent').innerText=await textPromise;
document.getElementById('file').hidden=false;
document.getElementById("div_btn").hidden=false;
} else if(key[1] in queryDic){
var base64=queryDic[key[1]];
} else if(params.has(key[1])){
var base64=params.get(key[1]);
$("#btn_download").click(function(){
window.open(String.raw`data:text/plain;base64,${base64}`,"_self")
})

View File

@ -1,31 +0,0 @@
function parseQueryStringToDictionary(queryString=window.location.search) {
var dictionary = {};
// remove the '?' from the beginning of the
// if it exists
if (queryString.indexOf('?') === 0) {
queryString = queryString.substr(1);
}
// Step 1: separate out each key/value pair
var parts = queryString.split('&amp;');
for (var i = 0; i < parts.length; i++) {
var p = parts[i];
// Step 2: Split Key/Value pair
var keyValuePair = p.split('=');
// Step 3: Add Key/Value pair to Dictionary object
var key = keyValuePair[0];
var value = keyValuePair[1];
// decode URI encoded string
value = decodeURIComponent(value);
value = value.replace(/\+/g, ' ');
dictionary[key] = value;
}
// Step 4: Return Dictionary Object
return dictionary;
}

View File

@ -4,15 +4,23 @@ class websiteFile{
}
async getViewerURL(){
params=new URLSearchParams()
var urlbase="/view"
switch (trueTypeOf(this.file)) {
case "string":
return String.raw`${urlbase}?file=${this.file}`
params.append("file",this.file)
break
case "File":
var base64=btoa(await getTextFromUploadedFileAsync(this.file))
return String.raw`${urlbase}?fileBase64=${base64}`;
params.append("fileBase64",base64);
break
}
if ([...params].length>0){
return urlbase+"?"+params.tostring()
}
else{
return urlbase
}
}
}