Skip to content

Instantly share code, notes, and snippets.

@Lorg0n
Last active April 30, 2024 20:37
Show Gist options
  • Select an option

  • Save Lorg0n/9d448238256e8adc9d16ae6bd1ce1549 to your computer and use it in GitHub Desktop.

Select an option

Save Lorg0n/9d448238256e8adc9d16ae6bd1ce1549 to your computer and use it in GitHub Desktop.
Adding a button to the hikka.io website that, if there is a voiceover by Amanogawa, will take you to a page with him on click.
// ==UserScript==
// @name Amanogawa Button
// @namespace Violentmonkey Scripts
// @match https://hikka.io/*
// @grant none
// @version 1.1
// @author -
// @downloadURL https://gist.github.com/Lorg0n/9d448238256e8adc9d16ae6bd1ce1549/raw/
// @description original: https://gist.github.com/rosset-nocpes/f251942d47662b725329772533769399/raw/
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/url
// ==/UserScript==
const threshold = 0.8 // Значення -> [0; 1]. Це рівень перевірки схожості назви аніме, тому що пошук на amanogawa працює дуже дивно й іноді видає аніме, які взагалі не потрібні були.
function findMostSimilarEnJpName(input, rawInputArray, array, similarityThreshold) {
function levenshteinDistance(a, b) {
const matrix = [];
for (let i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
for (let j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j] + 1
);
}
}
}
return matrix[b.length][a.length];
}
let mostSimilarObject = null;
let highestSimilarity = 0;
array.forEach(obj => {
const enJpNameSimilarity = (input.length - levenshteinDistance(input, obj.en_jp_name)) / input.length;
if (enJpNameSimilarity > highestSimilarity && enJpNameSimilarity >= similarityThreshold && rawInputArray.year == obj.year) {
highestSimilarity = enJpNameSimilarity;
mostSimilarObject = obj;
}
});
return mostSimilarObject;
}
VM.onNavigate(async () => {
const split_path = document.location.pathname.split("/")
if (split_path.length == 3 && split_path[1] == "anime") {
const anime_slug = split_path[2]
const anime_data = await (await fetch(`https://api.hikka.io/anime/${anime_slug}`)).json()
const title_ja = anime_data["title_ja"]
const url_cors_proxy_amanogawa = 'https://corsproxy.io/?' + encodeURIComponent(`https://amanogawa.space/api/search?s="${encodeURIComponent(title_ja)}"`);
const amanogawa_data = await (await fetch(url_cors_proxy_amanogawa)).json();
const anime = findMostSimilarEnJpName(title_ja, anime_data, amanogawa_data, threshold)
if (anime == null) {
return
}
const info_block = document.querySelector("body main > .grid > .flex:nth-child(2) > .grid > div:nth-child(3) > .flex > .flex:nth-child(2)")
info_block.insertAdjacentHTML("beforeend", `<button id="amanogawa-button">На сайті Amanogawa</button>`)
const amanogawa_button = document.getElementById("amanogawa-button")
amanogawa_button.addEventListener("click", async () => {
window.open(`https://amanogawa.space/anime/${anime.id}`, '_blank');
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment