2023-01-01 03:38:37 +00:00
|
|
|
const IMAGE_FORMATS = document.getElementById('IMAGE_FORMATS').value.split(',')
|
2022-12-29 14:20:27 +00:00
|
|
|
|
2022-08-15 06:42:59 +00:00
|
|
|
document.getElementById('post-title').value = localStorage.getItem("post-title")
|
|
|
|
document.getElementById('post-text').value = localStorage.getItem("post-text")
|
|
|
|
document.getElementById('post-url').value = localStorage.getItem("post-url")
|
|
|
|
|
2022-11-09 19:25:52 +00:00
|
|
|
document.getElementById('post-notify').checked = localStorage.getItem("post-notify") == 'true'
|
2022-08-15 06:42:59 +00:00
|
|
|
document.getElementById('post-new').checked = localStorage.getItem("post-new") == 'true'
|
2023-02-08 06:22:11 +00:00
|
|
|
const postnsfw = document.getElementById('post-nsfw')
|
|
|
|
if (postnsfw) {
|
2023-02-08 06:23:03 +00:00
|
|
|
postnsfw.checked = localStorage.getItem("post-nsfw") == 'true'
|
2023-02-08 06:22:11 +00:00
|
|
|
}
|
2022-08-15 06:42:59 +00:00
|
|
|
document.getElementById('post-private').checked = localStorage.getItem("post-private") == 'true'
|
2022-11-09 19:25:52 +00:00
|
|
|
document.getElementById('post-ghost').checked = localStorage.getItem("post-ghost") == 'true'
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-08-14 03:20:43 +00:00
|
|
|
markdown(document.getElementById("post-text"));
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-08-11 17:18:53 +00:00
|
|
|
function checkForRequired() {
|
2022-08-14 03:20:43 +00:00
|
|
|
const title = document.getElementById("post-title");
|
|
|
|
const url = document.getElementById("post-url");
|
|
|
|
const text = document.getElementById("post-text");
|
|
|
|
const button = document.getElementById("create_button");
|
|
|
|
const image = document.getElementById("file-upload");
|
|
|
|
const image2 = document.getElementById("file-upload-submit");
|
|
|
|
|
2022-07-16 21:00:02 +00:00
|
|
|
if (url.value.length > 0 || image.files.length > 0 || image2.files.length > 0) {
|
|
|
|
text.required = false;
|
|
|
|
url.required=false;
|
|
|
|
} else if (text.value.length > 0 || image.files.length > 0 || image2.files.length > 0) {
|
|
|
|
url.required = false;
|
|
|
|
} else {
|
|
|
|
text.required = true;
|
|
|
|
url.required = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isValidTitle = title.checkValidity();
|
|
|
|
const isValidURL = url.checkValidity();
|
|
|
|
const isValidText = text.checkValidity();
|
|
|
|
|
|
|
|
if (isValidTitle && (isValidURL || image.files.length > 0 || image2.files.length > 0)) {
|
|
|
|
button.disabled = false;
|
|
|
|
} else if (isValidTitle && isValidText) {
|
|
|
|
button.disabled = false;
|
|
|
|
} else {
|
|
|
|
button.disabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkForRequired();
|
|
|
|
|
|
|
|
function hide_image() {
|
2022-08-14 03:20:43 +00:00
|
|
|
x=document.getElementById('image-upload-block');
|
|
|
|
url=document.getElementById('post-url').value;
|
2022-07-16 21:00:02 +00:00
|
|
|
if (url.length>=1){
|
|
|
|
x.classList.add('d-none');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.classList.remove('d-none');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.onpaste = function(event) {
|
2022-11-13 14:16:42 +00:00
|
|
|
files = structuredClone(event.clipboardData.files);
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-10-11 17:13:50 +00:00
|
|
|
if (files.length > 4)
|
|
|
|
{
|
|
|
|
alert("You can't upload more than 4 files at one time!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-16 21:00:02 +00:00
|
|
|
filename = files[0]
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
filename = filename.name.toLowerCase()
|
|
|
|
if (document.activeElement.id == 'post-text') {
|
|
|
|
let filename = ''
|
|
|
|
for (const file of files)
|
|
|
|
filename += file.name + ', '
|
2022-11-07 22:21:03 +00:00
|
|
|
filename = filename.toLowerCase().slice(0, -2)
|
2022-12-07 16:51:51 +00:00
|
|
|
document.getElementById('file-upload-submit').value = files;
|
2022-07-16 21:00:02 +00:00
|
|
|
document.getElementById('filename-show-submit').textContent = filename;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f=document.getElementById('file-upload');
|
|
|
|
f.files = files;
|
|
|
|
document.getElementById('filename-show').textContent = filename;
|
|
|
|
document.getElementById('urlblock').classList.add('d-none');
|
2022-11-05 21:01:23 +00:00
|
|
|
if (IMAGE_FORMATS.some(s => filename.endsWith(s)))
|
2022-07-16 21:00:02 +00:00
|
|
|
{
|
2022-12-04 15:40:32 +00:00
|
|
|
const fileReader = new FileReader();
|
2022-07-16 21:00:02 +00:00
|
|
|
fileReader.readAsDataURL(f.files[0]);
|
|
|
|
fileReader.addEventListener("load", function () {document.getElementById('image-preview').setAttribute('src', this.result);});
|
|
|
|
}
|
2022-10-24 22:00:19 +00:00
|
|
|
document.getElementById('post-url').value = null;
|
2022-10-24 22:02:44 +00:00
|
|
|
localStorage.setItem("post-url", "")
|
|
|
|
document.getElementById('image-upload-block').classList.remove('d-none')
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
checkForRequired();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('file-upload').addEventListener('change', function(){
|
2023-02-08 07:14:01 +00:00
|
|
|
const f = document.getElementById('file-upload');
|
|
|
|
if (f.files)
|
2022-07-16 21:00:02 +00:00
|
|
|
{
|
2023-02-08 07:14:01 +00:00
|
|
|
document.getElementById('urlblock').classList.add('d-none');
|
|
|
|
const filename = f.files[0].name
|
|
|
|
document.getElementById('filename-show').textContent = filename.substr(0, 20);
|
|
|
|
if (IMAGE_FORMATS.some(s => filename.toLowerCase().endsWith(s)))
|
|
|
|
{
|
|
|
|
const fileReader = new FileReader();
|
|
|
|
fileReader.readAsDataURL(f.files[0]);
|
|
|
|
fileReader.addEventListener("load", function () {document.getElementById('image-preview').setAttribute('src', this.result);});
|
|
|
|
}
|
|
|
|
checkForRequired();
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function savetext() {
|
2022-08-15 06:42:59 +00:00
|
|
|
localStorage.setItem("post-title", document.getElementById('post-title').value)
|
|
|
|
localStorage.setItem("post-text", document.getElementById('post-text').value)
|
|
|
|
localStorage.setItem("post-url", document.getElementById('post-url').value)
|
|
|
|
|
2022-07-16 21:00:02 +00:00
|
|
|
let sub = document.getElementById('sub')
|
|
|
|
if (sub) localStorage.setItem("sub", sub.value)
|
2022-08-15 06:42:59 +00:00
|
|
|
|
2022-11-09 19:25:52 +00:00
|
|
|
localStorage.setItem("post-notify", document.getElementById('post-notify').checked)
|
2022-08-15 06:42:59 +00:00
|
|
|
localStorage.setItem("post-new", document.getElementById('post-new').checked)
|
2023-02-08 06:23:03 +00:00
|
|
|
if (postnsfw) {
|
|
|
|
localStorage.setItem("post-nsfw", document.getElementById('post-nsfw').checked)
|
|
|
|
}
|
2022-08-15 06:42:59 +00:00
|
|
|
localStorage.setItem("post-private", document.getElementById('post-private').checked)
|
2022-11-09 19:25:52 +00:00
|
|
|
localStorage.setItem("post-ghost", document.getElementById('post-ghost').checked)
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function autoSuggestTitle() {
|
|
|
|
|
2022-12-04 15:40:32 +00:00
|
|
|
const urlField = document.getElementById("post-url");
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-12-04 15:40:32 +00:00
|
|
|
const titleField = document.getElementById("post-title");
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-12-04 15:40:32 +00:00
|
|
|
const isValidURL = urlField.checkValidity();
|
2022-07-16 21:00:02 +00:00
|
|
|
|
|
|
|
if (isValidURL && urlField.value.length > 0 && titleField.value === "") {
|
|
|
|
|
2022-12-04 15:40:32 +00:00
|
|
|
const x = new XMLHttpRequest();
|
2022-07-16 21:00:02 +00:00
|
|
|
x.withCredentials=true;
|
|
|
|
x.onreadystatechange = function() {
|
|
|
|
if (x.readyState == 4 && x.status == 200 && !titleField.value) {
|
|
|
|
|
|
|
|
title=JSON.parse(x.responseText)["title"];
|
|
|
|
titleField.value=title;
|
|
|
|
checkForRequired()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x.open('get','/submit/title?url=' + urlField.value);
|
2022-11-12 05:46:21 +00:00
|
|
|
x.setRequestHeader('xhr', 'xhr');
|
2022-07-16 21:00:02 +00:00
|
|
|
x.send(null);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-11-09 19:26:38 +00:00
|
|
|
function ghost_toggle(t) {
|
|
|
|
const followers = document.getElementById("post-notify")
|
2022-07-16 21:00:02 +00:00
|
|
|
if (t.checked == true) {
|
|
|
|
followers.checked = false;
|
|
|
|
followers.disabled = true;
|
|
|
|
} else {
|
|
|
|
followers.disabled = false;
|
2022-09-04 23:15:37 +00:00
|
|
|
}
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkRepost() {
|
|
|
|
const system = document.getElementById('system')
|
2022-11-20 16:55:26 +00:00
|
|
|
system.innerHTML = "";
|
2022-07-16 21:00:02 +00:00
|
|
|
const url = document.getElementById('post-url').value
|
2022-10-14 10:26:48 +00:00
|
|
|
const min_repost_check = 9;
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-10-14 10:26:48 +00:00
|
|
|
if (url && url.length >= min_repost_check) {
|
2022-07-16 21:00:02 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("post", "/is_repost");
|
|
|
|
xhr.setRequestHeader('xhr', 'xhr');
|
2022-07-23 08:57:53 +00:00
|
|
|
const form = new FormData()
|
2022-07-16 21:00:02 +00:00
|
|
|
form.append("url", url);
|
|
|
|
|
|
|
|
xhr.onload=function(){
|
|
|
|
try {data = JSON.parse(xhr.response)}
|
|
|
|
catch(e) {console.log(e)}
|
2022-09-04 23:15:37 +00:00
|
|
|
|
2022-07-16 21:00:02 +00:00
|
|
|
if (data && data["permalink"]) {
|
2022-11-16 00:28:21 +00:00
|
|
|
const permalinkText = escapeHTML(data["permalink"]);
|
|
|
|
const permalinkURI = encodeURI(data["permalink"]);
|
|
|
|
if (permalinkText) {
|
2022-11-20 16:55:26 +00:00
|
|
|
system.innerHTML = `This is a repost of <a href="${permalinkURI}">${permalinkText}</a>`;
|
2022-07-16 21:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.send(form)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('keydown', (e) => {
|
2022-09-04 23:15:37 +00:00
|
|
|
if(!((e.ctrlKey || e.metaKey) && e.key === "Enter"))
|
|
|
|
return;
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
const submitButton = document.getElementById('create_button')
|
2022-07-16 21:00:02 +00:00
|
|
|
|
2022-09-04 23:15:37 +00:00
|
|
|
submitButton.click();
|
2022-07-16 21:00:02 +00:00
|
|
|
});
|
|
|
|
|
2022-10-11 17:13:50 +00:00
|
|
|
checkRepost();
|
2022-12-29 14:20:27 +00:00
|
|
|
|
|
|
|
if (location.href == '/submit') {
|
|
|
|
const sub = document.getElementById('sub')
|
|
|
|
if (sub) sub.value = localStorage.getItem("sub")
|
|
|
|
}
|
2023-02-08 07:15:37 +00:00
|
|
|
|
|
|
|
const uploadfilelist = document.getElementById('upload-filelist');
|
|
|
|
const bar = document.getElementById('file-progress');
|
|
|
|
const percentIndicator = document.getElementById('progress-percent');
|
|
|
|
|
|
|
|
function handleUploadProgress(evt) {
|
|
|
|
uploadfilelist.classList.remove("d-none")
|
|
|
|
if (evt.lengthComputable) {
|
|
|
|
const progressPercent = Math.floor((evt.loaded / evt.total) * 100);
|
|
|
|
bar.setAttribute('value', progressPercent);
|
|
|
|
percentIndicator.textContent = progressPercent + '%';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function submit(form) {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
formData = new FormData(form);
|
|
|
|
|
|
|
|
formData.append("formkey", formkey());
|
|
|
|
actionPath = form.getAttribute("action");
|
|
|
|
|
|
|
|
xhr.open("POST", actionPath);
|
|
|
|
xhr.upload.onprogress = handleUploadProgress;
|
|
|
|
xhr.setRequestHeader('xhr', 'xhr');
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
uploadfilelist.classList.add("d-none")
|
|
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
|
|
const post_id = JSON.parse(xhr.response)['post_id'];
|
|
|
|
location.href = "/post/" + post_id
|
|
|
|
} else {
|
|
|
|
document.getElementById('toast-post-error-text').innerText = "Error, please try again later."
|
|
|
|
try {
|
|
|
|
let data=JSON.parse(xhr.response);
|
|
|
|
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show();
|
|
|
|
document.getElementById('toast-post-error-text').innerText = data["error"];
|
|
|
|
if (data && data["details"]) document.getElementById('toast-post-error-text').innerText = data["details"];
|
|
|
|
} catch(e) {
|
|
|
|
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).hide();
|
|
|
|
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formData);
|
|
|
|
}
|