import {BasePlugin} from '../core/base-plugin.js';
import {__publicField, isJavBus, isJavDb} from '../core/constants.js';
import {AsyncQueue} from '../core/utils.js';
class OtherSitePlugin extends BasePlugin {
constructor() {
super(...arguments);
__publicField(this, "okBackgroundColor", "#7bc73b");
__publicField(this, "errorBackgroundColor", "#de3333");
__publicField(this, "warnBackgroundColor", "#d7a80c");
__publicField(this, "domainErrorBackgroundColor", "#d7780c");
__publicField(this, "asyncQueue", new AsyncQueue);
__publicField(this, "siteConfigs", [
{
id: "MissAvBtn",
getBaseUrl: async () => await this.getMissAvUrl(),
itemSelector: ".text-secondary",
searchPath: (baseUrl, carNum2) => `${baseUrl}/search/${carNum2}`,
getDetailPageHref: $box2 => $box2.attr("href"),
findCarNumOrTitle: $box2 => $box2.text()
}, {
id: "123AvBtn",
getBaseUrl: async () => await this.getAv123Url() + "/ja",
itemSelector: ".box-item",
searchPath: (baseUrl, carNum2) => `${baseUrl}/search?keyword=${carNum2}`,
getDetailPageHref: $box2 => $box2.find(".detail a").attr("href"),
findCarNumOrTitle: $box2 => $box2.find("img").attr("title")
}, {
id: "JableBtn",
getBaseUrl: async () => await this.getjableUrl(),
itemSelector: "#list_videos_videos_list_search_result .detail .title a",
searchPath: (baseUrl, carNum2) => `${baseUrl}/search/${carNum2}/`,
getDetailPageHref: $box2 => $box2.attr("href"),
findCarNumOrTitle: $box2 => $box2.text()
}, {
id: "SupJavBtn",
getBaseUrl: async () => await this.getSupJavUrl(),
itemSelector: ".posts post",
searchPath: (baseUrl, carNum2) => `${baseUrl}/?s=${carNum2}`,
getDetailPageHref: ($box2, baseUrl, carNum2) => $box2.attr("href"),
findCarNumOrTitle: $box2 => $box2.attr("title")
},
{
id: "JavTrailersBtn",
getBaseUrl: async () => await this.getJavTrailersUrl(),
itemSelector: ".videos-list .video-link",
searchPath: (baseUrl, carNum2) => `${baseUrl}/search/${carNum2}`,
getDetailPageHref: $box2 => $box2.attr("href"),
findCarNumOrTitle: $box2 => $box2.find("p.card-text").text()
},
{
id: "JavDbBtn",
getBaseUrl: async () => await this.getJavDbUrl(),
itemSelector: ".movie-list .item",
searchPath: (baseUrl, carNum2) => `${baseUrl}/search?q=${carNum2}`,
getDetailPageHref: $box2 => $box2.find("a").attr("href"),
findCarNumOrTitle: $box2 => $box2.find(".video-title").text(),
condition: sourceCarNum => isJavBus
}, {
id: "JavBusBtn",
getBaseUrl: async () => await this.getJavBusUrl(),
itemSelector: ".container h3",
searchPath: (baseUrl, carNum2) => `${baseUrl}/${carNum2}`,
getDetailPageHref: ($box2, baseUrl, carNum2) => `${baseUrl}/${carNum2}`,
findCarNumOrTitle: $box2 => $box2.text(),
condition: sourceCarNum => isJavDb && sourceCarNum && !sourceCarNum.includes("FC2")
}]);
__publicField(this, "settingCache", null);
__publicField(this, "lastFetchTime", 0);
__publicField(this, "CACHE_DURATION", 1e4);
}
getName() {
return "OtherSitePlugin";
}
async initCss() {
return "\n \n ";
}
async handle() {
window.isDetailPage && this.loadOtherSite().then();
}
async loadOtherSite(carNum2, sourceCarNum) {
if ("yes" !== await storageManager.getSetting("enableLoadOtherSite", "yes")) return;
carNum2 || (carNum2 = this.getPageInfo().carNum);
const enabledSites = this.getEnabledSites(),
html = `\n
\n
\n ${this.siteConfigs.map((config => {
config.sourceCarNum = sourceCarNum;
if (config.condition && !1 === config.condition(config.sourceCarNum)) return "";
return `
${config.id.replace("Btn", "")}`;
})).join("")}\n
跳转设置\n
\n
\n \n \n `;
$(".movie-panel-info").append(html);
$(".container .info").append(html);
$("#javTrailersBtn").on("click", (async event => {
event.preventDefault();
let settingObj = await storageManager.getSetting();
const filterHotKey = settingObj.filterHotKey, favoriteHotKey = settingObj.favoriteHotKey,
speedVideoHotKey = settingObj.speedVideoHotKey;
let href = $("#javTrailersBtn").attr("href"),
url = href + `?handle=1&filterHotKey=${filterHotKey}&favoriteHotKey=${favoriteHotKey}&speedVideoHotKey=${speedVideoHotKey}`;
event && (event.ctrlKey || event.metaKey) && (url = href);
utils.openPage(url, carNum2, !1, event);
}));
await Promise.all(this.siteConfigs.map((async config => {
config.condition && !1 === config.condition(config.sourceCarNum) || await this.handleSite(carNum2, config);
})));
this.renderSettingsArea();
this.setupEventListeners();
}
async handleSite(carNum2, config) {
const $btn = $(`#${config.id}`);
try {
if ($btn.attr("href")) return;
if (utils.isHidden($btn)) return;
const cacheKey = "jhs_other_site",
cacheData = localStorage.getItem(cacheKey) ? JSON.parse(localStorage.getItem(cacheKey)) : {},
siteKey = carNum2 + "_" + config.id.replace("Btn", ""), cachedResult = cacheData[siteKey];
if (cachedResult) {
if ("single" === cachedResult.type) {
$btn.attr("href", cachedResult.url);
$btn.css("backgroundColor", this.okBackgroundColor);
} else if ("multiple" === cachedResult.type) {
$btn.attr("href", cachedResult.url);
$btn.append('多结果');
$btn.css("backgroundColor", this.okBackgroundColor);
}
return;
}
const baseUrl = await config.getBaseUrl(), searchUrl = config.searchPath(baseUrl, carNum2);
$btn.attr("href", searchUrl);
const html = await gmHttp.get(searchUrl, null, config.headers, !0), $dom = utils.htmlTo$dom(html),
resultUrlList = [];
$dom.find(config.itemSelector).each(((index, element) => {
const $box2 = $(element);
if (!config.findCarNumOrTitle($box2).toLowerCase().includes(carNum2.toLowerCase())) return;
let href = config.getDetailPageHref($box2, baseUrl, carNum2);
if (!href) throw new Error("解析href失败");
href.includes("http") || (href = baseUrl + (href.startsWith("/") ? href : "/" + href));
resultUrlList.push(href);
}));
let tagHtml = "", insertCacheData = null;
if (1 === resultUrlList.length) {
let resultUrl = resultUrlList[0];
$btn.attr("href", resultUrl);
$btn.css("backgroundColor", this.okBackgroundColor);
insertCacheData = {
type: "single",
url: resultUrl
};
} else if (resultUrlList.length > 1) {
$btn.attr("href", searchUrl);
tagHtml += '多结果';
$btn.css("backgroundColor", this.okBackgroundColor);
insertCacheData = {
type: "multiple",
url: searchUrl
};
} else {
$btn.attr("href", searchUrl);
$btn.attr("title", "未查询到, 点击前往搜索页");
$btn.css("backgroundColor", this.errorBackgroundColor);
}
insertCacheData && this.asyncQueue.addTask((() => {
const newCacheData = localStorage.getItem(cacheKey) ? JSON.parse(localStorage.getItem(cacheKey)) : {};
newCacheData[siteKey] = insertCacheData;
localStorage.setItem(cacheKey, JSON.stringify(newCacheData));
}));
tagHtml && $btn.append(tagHtml);
} catch (e) {
const errorString = String(e), siteName = config.id.replace("Btn", "");
if (errorString.includes("Just a moment")) {
$btn.attr("title", "请求失败:Cloudflare 安全检查。");
$btn.css("backgroundColor", this.warnBackgroundColor);
clog.warn(`检测第三方资源失败, ${siteName} 需Cloudflare安全检查`);
} else if (errorString.includes("重定向")) {
$btn.attr("title", "域名失效");
$btn.css("backgroundColor", this.domainErrorBackgroundColor);
clog.warn(`检测第三方资源失败, ${siteName} 域名被重定向`);
} else if (errorString.includes("404 Page Not Found")) {
$btn.attr("title", "未查询到, 点击前往搜索页");
$btn.css("backgroundColor", this.errorBackgroundColor);
} else {
console.error(e);
$btn.attr("title", "请求失败。");
$btn.css("backgroundColor", this.errorBackgroundColor);
clog.warn(`检测第三方资源失败, ${siteName}`);
}
}
}
async getSettingCache() {
const now = Date.now();
if (!this.settingCache || now - this.lastFetchTime > this.CACHE_DURATION) {
this.settingCache = await storageManager.getSetting();
this.lastFetchTime = now;
}
return this.settingCache;
}
async getMissAvUrl() {
return (await this.getSettingCache()).missAvUrl || "https://missav.live";
}
async getjableUrl() {
return (await this.getSettingCache()).jableUrl || "https://jable.tv";
}
async getJavTrailersUrl() {
return (await this.getSettingCache()).javTrailersUrl || "https://javtrailers.com";
}
async getAv123Url() {
return (await this.getSettingCache()).av123Url || "https://123av.com";
}
async getJavDbUrl() {
return (await this.getSettingCache()).javDbUrl || "https://javdb.com";
}
async getJavBusUrl() {
return (await this.getSettingCache()).javBusUrl || "https://www.javbus.com";
}
async getSupJavUrl() {
return (await this.getSettingCache()).supJavUrl || "https://supjav.com";
}
getEnabledSites() {
const enabledSites = localStorage.getItem("jhs_enabled_sites");
return enabledSites ? JSON.parse(enabledSites) : this.siteConfigs.map((c => c.id));
}
saveEnabledSites(sites) {
localStorage.setItem("jhs_enabled_sites", JSON.stringify(sites));
}
renderSettingsArea() {
const enabledSites = this.getEnabledSites(), checkboxesDiv = document.getElementById("siteCheckboxes");
checkboxesDiv && (checkboxesDiv.innerHTML = this.siteConfigs.map((config => {
const isEnabled = enabledSites.includes(config.id);
return `\n \n \n \n
\n `;
})).join(""));
}
setupEventListeners() {
const settingsArea = document.getElementById("settingsArea");
document.addEventListener("click", (event => {
if ("settingSiteBtn" === event.target.id || event.target.closest("#settingSiteBtn")) {
const isHidden = "none" === settingsArea.style.display || "" === settingsArea.style.display;
settingsArea.style.display = isHidden ? "block" : "none";
}
}));
settingsArea.addEventListener("change", (event => {
if ("checkbox" === event.target.type) {
const siteId = event.target.getAttribute("data-site-id");
if (event.target.checked) {
$(`#${siteId}`).show();
const carNum2 = this.getPageInfo().carNum,
config = this.siteConfigs.find((item => item.id === siteId));
this.handleSite(carNum2, config).then();
} else $(`#${siteId}`).hide();
const enabledSites = Array.from(settingsArea.querySelectorAll('input[type="checkbox"]:checked')).map((checkbox => checkbox.getAttribute("data-site-id")));
this.saveEnabledSites(enabledSites);
}
}));
}
}
export {OtherSitePlugin};