Skip to content

Commit

Permalink
fix: update urls, more error resistant
Browse files Browse the repository at this point in the history
  • Loading branch information
7nik committed Dec 18, 2023
1 parent 748cc56 commit 31476e2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ap-vk-poster",
"version": "2.6.2",
"version": "2.6.3",
"private": true,
"scripts": {
"build": "rollup -c --environment PROD",
Expand Down
30 changes: 22 additions & 8 deletions src/PostMaker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,36 @@
let source = "";
let error = "";
let picture: Promise<File> = getPostInfo().then((post) => {
let picture: Promise<File> = getPostInfo().then(async (post) => {
({ message, previewUrl, source, error } = post);
}).then(() => {
return GM.xmlHttpRequest({
let resp: GM.Response<void> = await GM.xmlHttpRequest({
method: "GET",
url: previewUrl,
// @ts-ignore - it's supported in TM
responseType: "arraybuffer",
})
// @ts-ignore - TM returns response
}).then(({ response }) => {
const file = new File([response], "photo.jpg", { type: "image/jpeg" });
}) as any;
if (resp.status < 400) {
const file = new File([resp.response], "photo.jpg", { type: "image/jpeg" });
if (SETTINGS.imgSize === "orig" && SETTINGS.imgScale) {
return downscale(file, SETTINGS.imgScale);
}
return file;
}
resp = await GM.xmlHttpRequest({
method: "GET",
url: `${previewUrl}.avif`,
// @ts-ignore - it's supported in TM
responseType: "arraybuffer",
}) as any;
if (resp.status < 400) {
error = "Ошибка скачивания картинки";
}
const file = new File([resp.response], "photo.avif", { type: "image/jpeg" });
if (SETTINGS.imgSize === "orig" && SETTINGS.imgScale) {
return downscale(file, SETTINGS.imgScale);
}
return file;
// VK doesn't understands avif so convert it
return downscale(file);
});
const Vk = new VkApi(SETTINGS.APP_ID, ["photos", "wall"]);
Expand Down
10 changes: 5 additions & 5 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function getPostInfo () {
relation: { removetime: string|null },
}>,
file_url: string,
} = await fetch(`/api/v3/posts/${postId}`).then((resp) => resp.json());
} = await fetch(`https://api.anime-pictures.net/api/v3/posts/${postId}`).then((resp) => resp.json());

const artists = Array.from(document.querySelectorAll(".tags li.orange a"))
.map((a) => a.textContent ?? "")
Expand Down Expand Up @@ -122,15 +122,15 @@ async function downscale (imgFile: File, size = 1000) {
const src = URL.createObjectURL(imgFile);
const img = await new Promise<HTMLImageElement>((resolve, reject) => {
const img = new Image();
img.addEventListener("error", (ev) => reject(ev.message));
img.addEventListener("error", (ev) => reject(ev.message ?? "Failed to parse the image"));
img.addEventListener("load", () => resolve(img));
img.src = src;
});
URL.revokeObjectURL(src);
const height = img.naturalHeight || img.offsetHeight || img.height;
const width = img.naturalWidth || img.offsetWidth || img.width;

if (height <= size && width <= size) {
if (height <= size && width <= size && !imgFile.name.endsWith(".avif")) {
return imgFile;
}

Expand Down Expand Up @@ -176,7 +176,7 @@ async function getTag (id: number): Promise<{
parent: number|null,
views: number,
}> {
const json = await fetch(`/api/v3/tags/${id}`).then((resp) => resp.json());
const json = await fetch(`https://api.anime-pictures.net/api/v3/tags/${id}`).then((resp) => resp.json());
return json.tag;
}

Expand All @@ -188,7 +188,7 @@ async function findTag (tag: string): Promise<{
}[]> {
const form = new FormData();
form.append("tag", tag);
const json = await fetch("/pictures/autocomplete_tag", {
const json = await fetch("https://api.anime-pictures.net/pictures/autocomplete_tag", {
method: "POST",
body: form,
}).then((resp) => resp.json());
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const container = document.querySelector("span#rating");
if (container) {
container.append(btn);
// re-add button after it was removed at hydration
new MutationObserver((records) => records.forEach((record) => {
if (record.removedNodes.length > 0) {
new MutationObserver((records) => records.forEach(() => {
if (!document.body.contains(btn)) {
container.append(btn);
}
})).observe(container, { childList: true });
Expand Down
2 changes: 2 additions & 0 deletions src/meta.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// @connect login.vk.com
// @connect api.vk.com
// @connect pu.vk.com
// @connect id.vk.com
// @connect pu5-1.vk-cdn.net
// @connect vk-cdn.net
// @connect cpreview.anime-pictures.net
// @connect images.anime-pictures.net
// @connect cdn.anime-pictures.net
// ==/UserScript==
2 changes: 1 addition & 1 deletion src/vkApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ class VkApi {
data: form,
});

if (photoData.photo == "null" || photoData.photo == "[]") {
if (!photoData.photo || photoData.photo == "null" || photoData.photo == "[]") {
throw "Image uploading error";
}

Expand Down

0 comments on commit 31476e2

Please sign in to comment.