Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add other language #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions language/English/how-set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change the link in index.js No.151 to the following content: <br>
`https://tira-tw.github.io/english-url/"+config.theme+"/index.html`
110 changes: 110 additions & 0 deletions language/English/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="txt/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous">
<title>Shorter URL</title>
<style>
* {
margin: 0;
padding: 0;
cursor: default;
}
html, body {
height: 100%;
}
body {
display: -webkit-box;
display: flex;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
vertical-align: center;
flex-wrap: wrap;
align-content: center;

color: #2a2b2c;
background-color: #ebedee;
overflow: hidden;
}
.card {
background-color: transparent;
width: 768px;
}
.card-text {
text-align: center;
}
.card-text > a {
text-decoration: none;
color: #007bff;
}
.card-text > a {
cursor: pointer;
}
.form-control {
cursor: auto;
}
@media (max-width: 769px) {
.card {
width: 80%;
}
}
@media (max-width: 420px) {
.card {
width: 95%;
}
}
@media (prefers-color-scheme: dark) {
body {
color: #d9d9d9;
background: #1b1b1b;
}
.card {
background-color: #252d38;
}
}
</style>
</head>
<body>
<div class="card">
<h5 class="card-header">Shorten your Url</h5>
<div class="card-body">
<h5 class="card-title">Please enter the long URL to be shortened :</h5>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="https://github.com/tira-tw" id="text">
<div class="input-group-append">
<button class="btn btn-primary" type="button" onclick='shorturl()' id="searchbtn">Shorten it</button>
</div>
</div>
<div class="card-text">
<a>want make same?</a><br><br><a href="https://github.com/Tira-tw/cloudflare-host-url" target="_self">here</a>
<br><br><a>for English</a><br><br><a href="https://github.com/xyTom/" target="_self">-Check-</a>
</div>
<p id="notice"></p>
</div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Result</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body" id="result">No result</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick='copyurl("result")' data-toggle="popover" data-placement="bottom" data-content="Copied!">Copy</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://gcore.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8=" crossorigin="anonymous"></script>
<script src="https://gcore.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://gcore.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>
<script src="https://gcore.jsdelivr.net/npm/[email protected]/main.js" crossorigin="anonymous"></script>
</body>
</html>
211 changes: 211 additions & 0 deletions language/English/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
const config = {
no_ref: "off", //Control the HTTP referrer header, if you want to create an anonymous link that will hide the HTTP Referer header, please set to "on" .
theme:"",//Homepage theme, use the empty value for default theme. To use urlcool theme, please fill with "theme/urlcool" .
cors: "on",//Allow Cross-origin resource sharing for API requests.
unique_link:true,//If it is true, the same long url will be shorten into the same short url
custom_link:false,//Allow users to customize the short url.
safe_browsing_api_key: "" //Enter Google Safe Browsing API Key to enable url safety check before redirect.
}

const html404 = `<!DOCTYPE html>
<body>
<h1>404 Not Found.</h1>
<p>The url you visit is not found.</p>
<a>want host?<a><a href="https://github.com/Tira-tw/cloudflare-host-url" target="_self">come!</a>
</body>`

let response_header={
"content-type": "text/html;charset=UTF-8",
}

if (config.cors=="on"){
response_header={
"content-type": "text/html;charset=UTF-8",
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Methods": "POST",
}
}

async function randomString(len) {
  len = len || 6;
  let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  let maxPos = $chars.length;
  let result = '';
  for (i = 0; i < len; i++) {
    result += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return result;
}

async function sha512(url){
url = new TextEncoder().encode(url)

const url_digest = await crypto.subtle.digest(
{
name: "SHA-512",
},
url, // The data you want to hash as an ArrayBuffer
)
const hashArray = Array.from(new Uint8Array(url_digest)); // convert buffer to byte array
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
//console.log(hashHex)
return hashHex
}
async function checkURL(URL){
let str=URL;
let Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
let objExp=new RegExp(Expression);
if(objExp.test(str)==true){
if (str[0] == 'h')
return true;
else
return false;
}else{
return false;
}
}
async function save_url(URL){
let random_key=await randomString()
let is_exist=await LINKS.get(random_key)
console.log(is_exist)
if (is_exist == null)
return await LINKS.put(random_key, URL),random_key
else
save_url(URL)
}
async function is_url_exist(url_sha512){
let is_exist = await LINKS.get(url_sha512)
console.log(is_exist)
if (is_exist == null) {
return false
}else{
return is_exist
}
}
async function is_url_safe(url){

let raw = JSON.stringify({"client":{"clientId":"Url-Shorten-Worker","clientVersion":"1.0.7"},"threatInfo":{"threatTypes":["MALWARE","SOCIAL_ENGINEERING","POTENTIALLY_HARMFUL_APPLICATION","UNWANTED_SOFTWARE"],"platformTypes":["ANY_PLATFORM"],"threatEntryTypes":["URL"],"threatEntries":[{"url":url}]}});

let requestOptions = {
method: 'POST',
body: raw,
redirect: 'follow'
};

result = await fetch("https://safebrowsing.googleapis.com/v4/threatMatches:find?key="+config.safe_browsing_api_key, requestOptions)
result = await result.json()
console.log(result)
if (Object.keys(result).length === 0){
return true
}else{
return false
}
}
async function handleRequest(request) {
console.log(request)
if (request.method === "POST") {
let req=await request.json()
console.log(req["url"])
if(!await checkURL(req["url"])){
return new Response(`{"status":500,"key":": Error: Url info maybe is fish website or virus or not this url"}`, {
headers: response_header,
})}
let stat,random_key
if (config.unique_link){
let url_sha512 = await sha512(req["url"])
let url_key = await is_url_exist(url_sha512)
if(url_key){
random_key = url_key
}else{
stat,random_key=await save_url(req["url"])
if (typeof(stat) == "undefined"){
console.log(await LINKS.put(url_sha512,random_key))
}
}
}else{
stat,random_key=await save_url(req["url"])
}
console.log(stat)
if (typeof(stat) == "undefined"){
return new Response(`{"status":200,"key":"/`+random_key+`"}`, {
headers: response_header,
})
}else{
return new Response(`{"status":200,"key":": Error:Reach the KV write limitation."}`, {
headers: response_header,
})}
}else if(request.method === "OPTIONS"){
return new Response(``, {
headers: response_header,
})

}

const requestURL = new URL(request.url)
const path = requestURL.pathname.split("/")[1]
const params = requestURL.search;

console.log(path)
if(!path){

const html= await fetch("https://tira-tw.github.io/english-url/"+config.theme+"/index.html")

return new Response(await html.text(), {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}

const value = await LINKS.get(path);
let location ;

if(params) {
location = value + params
} else {
location = value
}
console.log(value)


if (location) {
if (config.safe_browsing_api_key){
if(!(await is_url_safe(location))){
let warning_page = await fetch("https://xytom.github.io/Url-Shorten-Worker/safe-browsing.html")
warning_page =await warning_page.text()
warning_page = warning_page.replace(/{Replace}/gm, location)
return new Response(warning_page, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}
}
if (config.no_ref=="on"){
let no_ref= await fetch("https://xytom.github.io/Url-Shorten-Worker/no-ref.html")
no_ref=await no_ref.text()
no_ref=no_ref.replace(/{Replace}/gm, location)
return new Response(no_ref, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}else{
return Response.redirect(location, 302)
}

}
// If request not in kv, return 404
return new Response(html404, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
status: 404
})
}



addEventListener("fetch", async event => {
event.respondWith(handleRequest(event.request))
})
2 changes: 2 additions & 0 deletions language/Indonesia/Bagaimana mengatur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ubah link di index.js No.151 menjadi konten berikut: <br>
`https://tira-tw.github.io/Indonesia-url/"+config.theme+"/index.html`
Loading