Greasy Fork is available in English.
Parse Douban Info
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/438042/1005179/Douban%20Info%20Class.js
// ==UserScript==
// @name Douban Info Class
// @description parse douban info
// @version 0.0.1
// @author Secant(TYT@NexusHD)
// @icon https://movie.douban.com/favicon.ico
// @require http://greasyfork.icu/scripts/438040-dirty-json/code/dirty-json.js?version=1005176
// @contributionURL https://i.loli.net/2020/02/28/JPGgHc3UMwXedhv.jpg
// @contributionAmount 10
// @namespace http://greasyfork.icu/users/152136
// @grant GM_xmlhttpRequest
// @connect movie.douban.com
class DoubanInfo {
static origin = "https://movie.douban.com";
static subjectPathName = "/subject/";
static timeout = 6000;
constructor(id) {
this.id = (async () => id)();
}
get subjectDoc() {
return (async () => {
const currentURL = new URL(window.location.href);
let doc;
if (
currentURL.origin === DoubanInfo.origin &&
currentURL.pathname ===
DoubanInfo.subjectPathName + (await this.id) + "/"
) {
doc = document;
} else {
doc = new Promise(async (resolve) => {
GM_xmlhttpRequest({
method: "GET",
url: new URL(
DoubanInfo.subjectPathName,
DoubanInfo.origin
).toString(),
headers: {
referrer: "https://movie.douban.com/",
},
timout: DoubanInfo.timeout,
onload: (resp) => {
resolve(resp.responseXML);
},
ontimeout: (e) => {
console.warn(e);
resolve(null);
},
onerror: (e) => {
console.warn(e);
resolve(null);
},
});
});
}
Object.defineProperty(this, "subjectDoc", {
value: (async () => doc)(),
writable: false,
});
return this.subjectDoc;
})();
}
get linkingData() {
return (async () => {
const doc = await this.subjectDoc;
const ldJSON =
dJSON.parse(
doc?.querySelector("head>script[type='application/ld+json']")
?.textContent
) || null;
Object.defineProperty(this, "linkingData", {
value: (async () => ldJSON)(),
writable: false,
});
return this.linkingData;
})();
}
get title() {
return (async () => {
const doc = await this.subjectDoc;
const ld = await this.linkingData;
const titleFromPage =
doc?.querySelector("body #content h1>span[property]")?.textContent ||
null;
const titleFromMeta =
doc?.querySelector("head>meta[property='og:title']")?.content || null;
const titleFromLD = ld?.name || null;
const title = titleFromPage || titleFromMeta || titleFromLD;
Object.defineProperty(this, "title", {
value: (async () => title)(),
writable: false,
});
return this.title;
})();
}
get type() {
return (async () => {
const doc = await this.subjectDoc;
const type =
doc?.querySelector("head>meta[property='og:type']")?.content || null;
Object.defineProperty(this, "type", {
value: (async () => type)(),
writable: false,
});
return this.type;
})();
}
get poster() {
return (async () => {
const doc = await this.subjectDoc;
const poster =
doc
?.querySelector("head>meta[property='og:image']")
?.content.replace("s_ratio_poster", "l_ratio_poster") || null;
Object.defineProperty(this, "poster", {
value: (async () => poster)(),
writable: false,
});
return this.poster;
})();
}
get duration() {
return (async () => {
const doc = await this.subjectDoc;
const duration =
parseInt(
doc?.querySelector("head>meta[property='og:duration']")?.content,
10
) || null;
Object.defineProperty(this, "duration", {
value: (async () => duration)(),
writable: false,
});
return this.duration;
})();
}
}