Greasy Fork is available in English.
很喜欢IMDB APP上家长指导这个功能,下电影前可以参考一下,是否适合在投影上和家人一起看,所以给豆瓣做了这个扩展,请注意你所在的地区是否能正常访问imdb。
用AI重新编辑了下代码,就能兼容了,测试下:
// ==UserScript==
// @name 豆瓣电影分级
// @namespace http://tampermonkey.net/
// @version 3.1
// @description 给豆瓣添加IMDB的家长指导功能,适合家庭观看。
// @author BigKnife
// @icon https://img3.doubanio.com/favicon.ico
// @match *://movie.douban.com/subject/*
// @grant GM_xmlhttpRequest
// @connect www.imdb.com
// @downloadURL https://update.greasyfork.org/scripts/429162/%E8%B1%86%E7%93%A3%E7%94%B5%E5%BD%B1%E5%88%86%E7%BA%A7.user.js
// @updateURL https://update.greasyfork.org/scripts/429162/%E8%B1%86%E7%93%A3%E7%94%B5%E5%BD%B1%E5%88%86%E7%BA%A7.meta.js
// ==/UserScript==
(function () {
'use strict'; // 严格模式,避免一些潜在的错误
let gimdbid, gDoc = ""; // 初始化IMDB ID和文档变量
// 获取家长指导信息的函数
unsafeWindow.getPG = function () {
// 找到IMDB ID
let imdbid = [...document.querySelectorAll('span')].find(s => s.innerText == 'IMDb:').nextSibling.textContent.trim();
gimdbid = imdbid; // 存储IMDB ID
// 在页面中添加信息显示区域
document.querySelector('#info').insertAdjacentHTML('beforeend', '
');
// 发送请求获取IMDB的家长指导信息
GM_xmlhttpRequest({
method: "GET",
url: `https://www.imdb.com/title/${imdbid}/parentalguide`,
onloadstart: () => {
console.log("正在获取" + imdbid); // 控制台打印正在获取的信息
document.querySelector("#gpg").innerText = "正在获取"; // 更新按钮文本
},
onload: response => getPGHandleV2(response.responseText) // 处理返回的数据
});
}
// 获取指定字段信息的函数
unsafeWindow.getField = function (doc, mark) {
let tmp = doc.querySelector(`div[data-testid="sub-section-${mark}"]`); // 查找对应的字段
if (!tmp) return ""; // 如果未找到,返回空字符串
let s = tmp.previousElementSibling.firstChild.innerText; // 获取字段的文本内容
// 替换英文描述为中文
s = s.replace("None", "无").replace("Mild", "轻微").replace("Moderate", "中等").replace("Severe", "严重");
// 根据描述设置颜色
let color = { "无": "#d0d0d0", "轻微": "#c5e197", "中等": "#fbca8c", "严重": "#ffb3ad" }[s];
// 返回带颜色的HTML
return `${s}`;
}
// 获取评论数量的函数
unsafeWindow.getDetailNum = function (doc, mark) {
return doc.querySelector(`div[data-testid="sub-section-${mark}"]`) ? `${doc.querySelectorAll('div[data-testid="item-id"]').length}条评论` : 0; // 返回评论数量
}
// 隐藏详细信息的函数
unsafeWindow.detailHide = function () {
let detail = document.querySelector('#infodetail'); // 获取详细信息区域
if (detail) detail.innerHTML = ""; // 清空内容
}
// 获取详细评论的函数
unsafeWindow.getDetail = function (mark) {
let posts = gDoc.querySelector(`div[data-testid="sub-section-${mark}"]`); // 查找对应的评论区域
if (!posts) return 0; // 如果未找到,返回0
let detail = document.querySelector('#infodetail'); // 获取详细信息区域
// 拼接评论内容
detail.innerHTML = [...posts.querySelectorAll('div[data-testid="item-id"]')]
.map((post, i) => `${i + 1}. ${post.textContent}`).join("
") + `收起
`;
}
// 处理家长指导信息的函数
unsafeWindow.getPGHandleV2 = function (html) {
console.log("OK"); // 控制台确认获取成功
document.querySelector("#gpg").innerText = "查看分级"; // 更新按钮文本
gDoc = new DOMParser().parseFromString(html, "text/html"); // 解析HTML
// 获取各个分类的信息
let categories = ["nudity", "violence", "profanity", "alcohol", "frightening"].map(mark => {
return `${getField(gDoc, mark)}(${getDetailNum(gDoc, mark)})`;
});
// 更新页面中的家长指导信息
document.querySelector('#pginfo').innerHTML = categories.map((cat, i) => `${["性爱和裸体", "暴力和血腥", "粗言俗语", "酒精毒品和烟草", "恐怖和紧张场景"][i]}: ${cat}`).join("
");
}
// 在页面中添加IMDB链接
let imdbel = [...document.querySelectorAll('span')].find(s => s.innerText == 'IMDb:');
let imdbid = [...document.querySelectorAll('#info > span.pl')].find(s => s.innerText == 'IMDb:').nextSibling.textContent.trim();
let a = document.createElement('a');
a.href = `https://www.imdb.com/title/${imdbid}`; // 设置链接
a.target = '_blank'; // 在新标签页打开
a.textContent = imdbid; // 显示IMDB ID
// 在页面中插入“查看分级”按钮
imdbel.nextSibling.nextSibling.insertAdjacentHTML('beforebegin', `查看分级`);
getPG(); // 初始化调用获取家长指导信息
})();
有用,但跟
https://greasyfork.org/scripts/329484
有冲突,二者同时使用时,只有分级脚本能显示。