AnimeHay

Tự động lưu và khôi phục lịch sử xem phim khi AnimeHay đổi tên miền mới

// ==UserScript==
// @name        AnimeHay
// @namespace   https://greasyfork.org/en/scripts/492147-animehay
// @match       *://animehay.*/*
// @grant       GM_getValue
// @grant       GM_setValue
// @version     1.3
// @author      HVD
// @icon        https://cdn.discordapp.com/attachments/1166869099868070002/1249028685659377705/favicon.png?ex=6665d01c&is=66647e9c&hm=320bc2cdb4c8a329e1376b98f364cc19904fed435d0484088cee9b1112d16e99&
// @description Tự động lưu và khôi phục lịch sử xem phim khi AnimeHay đổi tên miền mới
// @license MIT
// ==/UserScript==

let prevDomain = GM_getValue('prevDomain', '');

if (prevDomain == window.location.host) {
    if (window.location.pathname.includes('/xem-phim/')) {
        let watchedAnime = GM_getValue('watchedAnime', '[]');
        watchedAnime = JSON.parse(watchedAnime);

        let newAnime = window.location.pathname.replace(/\/xem-phim\/|\.html/g, '');
        watchedAnime = new Set(watchedAnime);
        watchedAnime.add(newAnime);

        GM_setValue('watchedAnime', JSON.stringify([...watchedAnime]));
        GM_setValue('dataHistory', localStorage.getItem("data_history") ?? '[]');

        console.log('[AnimeHay] Đã lưu phim!');
    }

    return;
}

let dataHistory = GM_getValue('dataHistory', '[]');
if (dataHistory != '[]') {
    localStorage.setItem("data_history", dataHistory);
}

let watchedAnime = GM_getValue('watchedAnime', '[]');
watchedAnime = JSON.parse(watchedAnime);
if (watchedAnime.length > 0) {
    let currentHref = window.location.href;
    for (let i = 0; i < watchedAnime.length; i++) {
        history.replaceState({}, '', window.location.origin + '/xem-phim/' + watchedAnime[i] + '.html');
    }
    history.replaceState({}, '', currentHref);
}

GM_setValue('prevDomain', window.location.host);

console.log('[AnimeHay] Đã khôi phục lịch sử xem phim!');