Last active
December 7, 2023 10:57
-
-
Save akira02/3ebf0d435ad213cae2c2d207ef903214 to your computer and use it in GitHub Desktop.
Grow up MERRY TREE image uploader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 上傳自訂禮物 | |
// 推薦尺寸32x32 | |
let inputElement = document.createElement("input"); | |
inputElement.type = "file"; | |
inputElement.id = "imageInput"; | |
inputElement.accept = "image/*"; | |
let uploadButton = document.createElement("button"); | |
uploadButton.textContent = "上傳自訂禮物"; | |
uploadButton.style = "color: white;" | |
uploadButton.onclick = uploadImage; | |
document.querySelector('#root button').parentNode.parentNode.parentNode.appendChild(inputElement) | |
document.querySelector('#root button').parentNode.parentNode.parentNode.appendChild(uploadButton) | |
function uploadImage() { | |
let inputElement = document.getElementById("imageInput"); | |
if (inputElement.files.length > 0) { | |
let file = inputElement.files[0]; | |
if (file.size > 1024 * 1024) { | |
alert("圖片過大"); | |
return; | |
} | |
const storage = JSON.parse(localStorage.getItem("recoil-persist")); | |
if (!storage?.userTokenAtom) { | |
alert("Not logined"); | |
return; | |
} | |
const formData = new FormData(); | |
formData.append("name", "custom"); | |
formData.append( | |
"image", | |
file, | |
"image-" + storage.userAtom.uuid + Date.now() + file.name | |
); | |
fetch("https://tree-api.joody.day/v1/gift/item/", { | |
headers: { | |
accept: "*/*", | |
authorization: "Token " + storage.userTokenAtom, | |
}, | |
referrer: "https://tree.joody.day/", | |
body: formData, | |
method: "POST", | |
}) | |
.then((res) => res.json()) | |
.then((data) => { | |
console.log(data); | |
if (data.message === "더이상 아이템을 그릴 수 없어요") { | |
alert("自訂圖片已達上限"); | |
window.location.href = 'https://tree.joody.day/bag' | |
} else { | |
// redirect to gift page | |
window.location.href = 'https://tree.joody.day/bag' | |
} | |
}); | |
} else { | |
alert("請選擇一個檔案"); | |
} | |
} | |
// Made by https://www.plurk.com/akira02 | |
// Released under MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 上傳樹頂圖片 | |
// 推薦尺寸54x54 | |
let inputElement = document.createElement("input"); | |
inputElement.type = "file"; | |
inputElement.id = "imageInput"; | |
inputElement.accept = "image/*"; | |
let uploadButton = document.createElement("button"); | |
uploadButton.textContent = "上傳樹頂圖片"; | |
uploadButton.style = "color: white;"; | |
uploadButton.onclick = uploadImage; | |
document.querySelector('#root button').parentNode.parentNode.parentNode.appendChild(inputElement) | |
document.querySelector('#root button').parentNode.parentNode.parentNode.appendChild(uploadButton) | |
function uploadImage() { | |
let inputElement = document.getElementById("imageInput"); | |
if (inputElement.files.length > 0) { | |
let file = inputElement.files[0]; | |
if (file.size > 1024 * 1024) { | |
alert("圖片過大"); | |
return; | |
} | |
const storage = JSON.parse(localStorage.getItem("recoil-persist")); | |
if (!storage?.userTokenAtom) { | |
alert("Not logined"); | |
return; | |
} | |
const formData = new FormData(); | |
formData.append( | |
"top_deco_image", | |
file, | |
"image-" + storage.userAtom.uuid + Date.now() + file.name | |
); | |
fetch("https://tree-api.joody.day/v1/user/", { | |
headers: { | |
accept: "*/*", | |
authorization: "Token " + storage.userTokenAtom, | |
}, | |
referrer: "https://tree.joody.day/", | |
referrerPolicy: "strict-origin-when-cross-origin", | |
body: formData, | |
method: "PUT", | |
}) | |
.then(() => { | |
window.location.href = 'https://tree.joody.day/' | |
}) | |
.catch(() => { | |
alert("上傳失敗"); | |
window.location.href = 'https://tree.joody.day/' | |
}); | |
} else { | |
alert("請選擇一個檔案"); | |
} | |
} | |
// Made by https://www.plurk.com/akira02 | |
// Released under MIT License |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment