Skip to content

Commit

Permalink
query1KV 用指定的key读一个key-value
Browse files Browse the repository at this point in the history
function query1KV() {
  • Loading branch information
crazypeace committed Feb 4, 2024
1 parent 8b2c306 commit 07ea7a7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 52 deletions.
39 changes: 35 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let res

let apiSrv = window.location.pathname
// let apiSrv = "https://pastebin.icdyct.cloudns.asia"
let password_value = document.querySelector("#passwordText").value
// let password_value = "tieludasiliqiuweiyue"
// let apiSrv = window.location.pathname
// let password_value = document.querySelector("#passwordText").value
let apiSrv = "https://journal.crazypeace.workers.dev"
let password_value = "journaljournal"

// 这是默认行为, 在不同的index.html中可以设置为不同的行为
let buildValueItemFunc = buildValueTxt
Expand Down Expand Up @@ -232,6 +232,37 @@ function queryVisitCount(qryKeyPhrase) {
})
}

function query1KV() {
let qryKeyPhrase = document.getElementById("keyForQuery").value;
// console.log(qryKeyPhrase);

// 从KV中查询 Query from KV
fetch(apiSrv, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cmd: "qry", key: qryKeyPhrase, password: password_value })
}).then(function (response) {
return response.json();
}).then(function (myJson) {
res = myJson;

// 成功查询 Succeed
if (res.status == "200") {
document.getElementById("longURL").value = res.url;
document.getElementById("keyPhrase").value = qryKeyPhrase;
} else {
document.getElementById("result").innerHTML = res.error;
// 弹出消息窗口 Popup the result
var modal = new bootstrap.Modal(document.getElementById('resultModal'));
modal.show();
}

}).catch(function (err) {
alert("Unknow error. Please retry!");
console.log(err);
})
}

function loadKV() {
//清空本地存储
clearLocalStorage();
Expand Down
110 changes: 64 additions & 46 deletions theme/pastebin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,83 @@
</head>
<body>
<div class="d-flex align-items-start flex-wrap">
<div class="card shadow m-3">
<h5 class="card-header">短链系统变身网络记事本PasteBin</h5>
<div class="card-body">
<h5 class="card-title"> </h5>

<div class="input-group mb-3">
<label>记事本内容</label>
<form class="input-group mb-3">
<textarea class="form-control" id="longURL" placeholder="输入文本" oninput="loadUrlList()"></textarea>
</form>
</div>
<div class="card shadow m-3">
<h5 class="card-header">Load 1 KV</h5>
<div class="card-body">
<h5 class="card-title"> </h5>

<div class="input-group mb-3">
<label>Key</label>
<form class="input-group mb-3">
<input type="text" class="form-control" placeholder="Input 1 key" id="keyForQuery">
<button class="btn btn-info" type="button" onclick='query1KV()' id="qryBtn">Load</button>
</form>
</div>

</div>
</div>

<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon2">记事本密码</span>
<input type="text" class="form-control" id="keyPhrase" aria-describedby="basic-addon2" placeholder="">
</div>
<div class="card shadow m-3">
<h5 class="card-header">短链系统变身网络记事本PasteBin</h5>
<div class="card-body">
<h5 class="card-title"> </h5>

<div class="input-group mb-3">
<button class="btn btn-primary" type="button" onclick='shorturl()' id="addBtn">保存</button>
</div>
<div class="input-group mb-3">
<label>记事本内容</label>
<form class="input-group mb-3">
<textarea class="form-control" id="longURL" placeholder="输入文本" oninput="loadUrlList()"></textarea>
</form>
</div>

<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon3">本系统密码</span>
<input class="form-control" type="text" value="__PASSWORD__" readonly="true" id="passwordText" aria-describedby="basic-addon3">
</div>
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon2">记事本密码</span>
<input type="text" class="form-control" id="keyPhrase" aria-describedby="basic-addon2" placeholder="">
</div>

</div>

<div class="card-footer">
<span> <a href="https://github.com/crazypeace/Url-Shorten-Worker/" target="_self">Fork me on GitHub</a> </span>
</div>
</div>
<div class="input-group mb-3">
<button class="btn btn-primary" type="button" onclick='shorturl()' id="addBtn">保存</button>
</div>

<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon3">本系统密码</span>
<input class="form-control" type="text" value="__PASSWORD__" readonly="true" id="passwordText" aria-describedby="basic-addon3">
</div>

<div class="card shadow m-3">
<h5 class="card-header">LocalStorage List</h5>
<div class="card-body">
<h5 class="card-title"> </h5>
<div class="input-group mb-3">
<button class="btn btn-success" type="button" onclick='loadUrlList()' id="loadListBtn">load localStorage</button>
<button class="btn btn-danger" type="button" onclick='clearLocalStorage()' id="clearlocalStgBtn">clear localStorage</button>
<button class="btn btn-info" type="button" onclick='loadKV()' id="loadKV2localStgBtn">load KV to localStorage</button>
</div>

<div class="card-footer">
<span> <a href="https://github.com/crazypeace/Url-Shorten-Worker/" target="_self">Fork me on GitHub</a> </span>
</div>
</div>

<div class="card-text">
<div classs="list-group" id="urlList">
<div class="mb-3 list-group-item">
<div class="input-group">
<button type="button" class="btn btn-danger" onclick='deleteShortUrl("4sure")' id="delBtn-4sure">X</button>
<button type="button" class="btn btn-info" onclick='queryVisitCount("4sure")' id="qryCntBtn-4sure">?</button>
<span class="form-control">https://1way.eu.org/4sure</span>
</div>
<div class="form-control">
https://en.y2mate.is/s36/youtube-to-mp3.html
<div class="card shadow m-3">
<h5 class="card-header">LocalStorage List</h5>
<div class="card-body">
<h5 class="card-title"> </h5>
<div class="input-group mb-3">
<button class="btn btn-success" type="button" onclick='loadUrlList()' id="loadListBtn">load localStorage</button>
<button class="btn btn-danger" type="button" onclick='clearLocalStorage()' id="clearlocalStgBtn">clear localStorage</button>
<button class="btn btn-info" type="button" onclick='loadKV()' id="loadKV2localStgBtn">load KV to localStorage</button>
</div>

<div class="card-text">
<div classs="list-group" id="urlList">
<div class="mb-3 list-group-item">
<div class="input-group">
<button type="button" class="btn btn-danger" onclick='deleteShortUrl("4sure")' id="delBtn-4sure">X</button>
<button type="button" class="btn btn-info" onclick='queryVisitCount("4sure")' id="qryCntBtn-4sure">?</button>
<span class="form-control">https://1way.eu.org/4sure</span>
</div>
<div class="form-control">
https://en.y2mate.is/s36/youtube-to-mp3.html
</div>
</div>
</div>
</div>
</div>
</div>
</div>

</div>

<div class="modal fade" id="resultModal" tabindex="-1" role="dialog" aria-labelledby="resultModalLabel" aria-hidden="true">
Expand Down
5 changes: 3 additions & 2 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const config = {
cors: "on",//Allow Cross-origin resource sharing for API requests.
unique_link: false,//If it is true, the same long url will be shorten into the same short url
custom_link: true,//Allow users to customize the short url.
overwrite_kv: false, // Allow user to overwrite an existed key.
snapchat_mode: false,//The link will be distroyed after access.
visit_count: false,//Count visit times.
load_kv: false,//Load all from Cloudflare KV
Expand Down Expand Up @@ -161,8 +162,8 @@ async function handleRequest(request) {
})
}

let is_exist = await LINKS.get(req_key)
if (is_exist != null) {
let is_exist = await is_url_exist(req_key)
if ((!config.overwrite_kv) && (is_exist)) {
return new Response(`{"status":500,"key": "` + req_key + `", "error":"Error: Specific key existed."}`, {
headers: response_header,
})
Expand Down

0 comments on commit 07ea7a7

Please sign in to comment.