Last active
August 7, 2023 00:28
-
-
Save markx/ac520956c0bd48a174a700358c9194fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name DoubanRating | |
// @namespace http://tampermonkey.net/ | |
// @version 0.0.1 | |
// @description 在欧乐上显示豆瓣分数. | |
// @author ke | |
// @match https://*.pangzitv.com/* | |
// @match https://*.olevod.com/* | |
// @match https://*.xiaobaotv.net/* | |
// @grant GM.xmlHttpRequest | |
// @connect movie.douban.com | |
// @connect douban.com | |
// @connect douban.8610000.xyz | |
// @noframes | |
// ==/UserScript== | |
'use strict'; | |
function getURL_GM(url) { | |
return new Promise(resolve => GM.xmlHttpRequest({ | |
method: 'GET', | |
url: url, | |
onload: function (response) { | |
if (response.status >= 200 && response.status < 400) { | |
resolve(response.responseText); | |
} else { | |
console.error(`Error getting ${url}:`, response.status, response.statusText, response); | |
resolve(); | |
} | |
}, onerror: function (response) { | |
console.error(`Error during GM.xmlHttpRequest to ${url}:`, response.statusText); | |
resolve(); | |
} | |
})); | |
} | |
async function getJSON_GM(url) { | |
const data = await getURL_GM(url); | |
if (data) { | |
return JSON.parse(data); | |
} | |
} | |
async function getJSONP_GM(url) { | |
const data = await getURL_GM(url); | |
if (data) { | |
const end = data.lastIndexOf(')'); | |
const [, json] = data.substring(0, end).split('(', 2); | |
return JSON.parse(json); | |
} | |
} | |
async function getJSON(url) { | |
try { | |
const response = await fetch(url); | |
if (response.status >= 200 && response.status < 400) | |
return await response.json(); | |
console.error(`Error fetching ${url}:`, response.status, response.statusText, response); | |
} | |
catch (e) { | |
console.error(`Error fetching ${url}:`, e); | |
} | |
} | |
(async function () { | |
// const douban_search_url = "https://www.douban.com/search?&q="; | |
const douban_icon_url = "https://www.douban.com/favicon.ico"; | |
// seems broken. | |
// const douban_search_url = 'https://api.douban.com/v2/movie/search?count=10&q='; | |
const douban_search_url = 'https://movie.douban.com/j/subject_suggest?q='; | |
const douban_search_page_url = 'https://search.douban.com/movie/subject_search?search_text=' | |
function getRating(title_text, callback) { | |
title_text = JSON.stringify({"titles": title_text}); | |
GM.xmlHttpRequest({ | |
method: "GET", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
url: douban_search_url, | |
data: title_text, | |
onload: function (response) { | |
var responseRating = JSON.parse(response.responseText); | |
callback(responseRating); | |
} | |
}); | |
} | |
async function getDoubanInfo(title) { | |
// Fallback to search. | |
const search = await getJSON_GM(`https://movie.douban.com/j/subject_suggest?q=${title}`); | |
console.log(title, search) | |
if (!search || search.length == 0) {return;} | |
let id = search[0].id; | |
for (let item of search) { | |
if (item.title == title) { | |
id = item.id; | |
break; | |
} | |
} | |
if (!id) {return;} | |
const abstract = await getJSON_GM(`https://movie.douban.com/j/subject_abstract?subject_id=${id}`); | |
const average = abstract && abstract.subject && abstract.subject.rate ? abstract.subject.rate : '?'; | |
return { | |
url: `https://movie.douban.com/subject/${id}/`, | |
rating: {numRaters: '', max: 10, average}, | |
title: search[0].title, | |
}; | |
} | |
function parseDoubanData(movie_name, data) { | |
if (!data) { | |
return; | |
} | |
for (var i = 0; i < data.length; i++) { | |
if (data[i].title === movie_name && data[i].rating) { | |
return data[i].rating.value; | |
} | |
} | |
}; | |
async function getDoubanRating (title) { | |
const name = title.replace(/[/\:*?"<>|]/, '_') | |
console.log("requesting: " + name); | |
const data = await getJSON_GM(`https://douban.8610000.xyz/suggest/${name}.json`) | |
return parseDoubanData(name, data) | |
}; | |
function createAnchor(queryURL) { | |
const newIcon = document.createElement("a"); | |
newIcon.href = queryURL; | |
newIcon.target = "_blank"; | |
const newImg = document.createElement("img"); | |
newImg.src = douban_icon_url; | |
newImg.style.height = "16px"; | |
newImg.style.paddingLeft = "2px"; | |
newImg.style.verticalAlign= 'baseline'; | |
newIcon.appendChild(newImg); | |
return [newIcon, newImg]; | |
} | |
async function getPangzi() { | |
var titles = document.querySelectorAll(".name"); | |
var title_text = []; | |
titles.forEach(element => {title_text.push(element.innerText)}); | |
console.log(title_text); | |
for (let title_el of titles) { | |
let title = title_el.innerText; | |
let rate = await getDoubanRating(title); | |
if (!rate) { | |
const data = await getDoubanInfo(title); | |
console.log(title, data); | |
if (data) { | |
rate = data.rating.average; | |
} | |
} | |
if (rate) { | |
insertRatingText(title_el, rate); | |
} | |
} | |
} | |
async function getOlevod() { | |
let titles = document.querySelectorAll("ul > li.vodlist_item > div > p.vodlist_title > a > span"); | |
var title_text = []; | |
titles.forEach(element => {title_text.push(element.innerText)}); | |
console.log(title_text); | |
for (let title_el of titles) { | |
let title = title_el.innerText; | |
title = title.replace(/【.*】/, '') | |
let rate = await getDoubanRating(title); | |
if (!rate) { | |
const data = await getDoubanInfo(title); | |
console.log(title, data); | |
if (data) { | |
rate = data.rating.average; | |
} | |
} | |
if (rate) { | |
insertRatingText(title_el, rate); | |
} | |
} | |
} | |
async function getXiaobao() { | |
let titles = document.querySelectorAll(".myui-vodlist__detail a"); | |
var title_text = []; | |
titles.forEach(element => {title_text.push(element.innerText)}); | |
console.log(title_text); | |
for (let title_el of titles) { | |
let title = title_el.innerText; | |
title = title.replace(/【.*】/, '') | |
let rate = await getDoubanRating(title); | |
if (!rate) { | |
const data = await getDoubanInfo(title); | |
console.log(title, data); | |
if (data) { | |
rate = data.rating.average; | |
} | |
} | |
if (rate) { | |
insertRatingText(title_el, rate); | |
} | |
} | |
} | |
function insertRatingText(element, rating) { | |
let [newIcon, newImg] = createAnchor(douban_search_page_url + element.innerText); | |
element.style.display = "initial"; | |
newIcon.style.display = "initial"; | |
newIcon.style.verticalAlign = "middle"; | |
element.innerHTML = element.innerHTML + " " + rating; | |
element.parentNode.insertBefore(newIcon, element.nextSibling); | |
} | |
if (window.location.href.includes("pangzitv")) { | |
setTimeout(getPangzi, 2000); | |
} else if (window.location.href.includes("olevod")) { | |
setTimeout(getOlevod, 2000); | |
} else if (window.location.href.includes("xiaobao")) { | |
setTimeout(getXiaobao, 2000); | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment