// ==UserScript==
// @name moviepilotNameTest
// @namespace http://tampermonkey.net/
// @version 1.1
// @description moviepilots名称测试
// @author yubanmeiqin9048
// @match https://*/details.php?id=*
// @match https://*/details_movie.php?id=*
// @match https://*/details_tv.php?id=*
// @match https://*/details_animate.php?id=*
// @match https://bangumi.moe/*
// @match https://*.acgnx.se/*
// @match https://*.dmhy.org/*
// @match https://nyaa.si/*
// @match https://mikanani.me/*
// @match https://*.skyey2.com/*
// @grant GM_log
// @grant GM_xmlhttpRequest
// @connect *
// @license MIT
// @downloadURL none
// ==/UserScript==
const moviepilotUrl = 'http://localhost:3000';
const moviepilotUser = 'admin';
const moviepilotPassword = 'password';
function renderTag(string, background_color) {
return `${string}`
}
function renderMoviepilotTag(tag) {
return `
MoviePilot | ${tag} | `;
}
function login () {
return new Promise(function(resolve, reject){
GM_xmlhttpRequest({
method: 'POST',
responseType: 'json',
url: moviepilotUrl + '/api/v1/login/access-token',
data: `username=${moviepilotUser}&password=${moviepilotPassword}`,
headers: {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
},
onload: (res) => {
resolve(res.response.access_token);
},
onerror: (err) => {
reject(err);
}
});
});
}
function recognize (token,title,subtitle) {
return new Promise(function(resolve, reject){
GM_xmlhttpRequest({
url: moviepilotUrl + `/api/v1/media/recognize?title=${title}&subtitle=${subtitle}`,
method: "GET",
headers: {
"user-agent": navigator.userAgent,
"content-type": "application/json",
"Authorization": `bearer ${token}`
},
responseType: "json",
onload: (res) => {
resolve(res.response);
},
onerror: (err) => {
reject(err);
}
});
});
}
function creatRecognizeRow (row,title,subtitle) {
login().then(token => {
recognize(token,title,subtitle).then(data => {
if (data.media_info) {
let html = '';
html += data.media_info.type ? renderTag(data.media_info.type, '#2775b6') : '';
html += data.media_info.category ? renderTag(data.media_info.category, '#2775b6') : '';
html += data.media_info.title ? renderTag(data.media_info.title, '#c54640') : '';
html += data.meta_info.season_episode ? renderTag(data.meta_info.season_episode, '#e6702e') : '';
html += data.meta_info.year ? renderTag(data.meta_info.year, '#e6702e') : '';
html += data.media_info.tmdb_id ? '' + renderTag(data.media_info.tmdb_id, '#5bb053') + '': '';
html += data.meta_info.resource_type ? renderTag(data.meta_info.resource_type, '#677489') : '';
html += data.meta_info.resource_pix ? renderTag(data.meta_info.resource_pix, '#677489') : '';
html += data.meta_info.video_encode ? renderTag(data.meta_info.video_encode, '#677489') : '';
html += data.meta_info.audio_encode ? renderTag(data.meta_info.audio_encode, '#677489') : '';
html += data.meta_info.resource_team ? renderTag(data.meta_info.resource_team, '#701eeb') : '';
row.innerHTML = renderMoviepilotTag(html);
}
}).catch(error => {
row.innerHTML = renderMoviepilotTag(`识别失败:${error}`);
});
}).catch(error => {
row.innerHTML = renderMoviepilotTag(`登录失败:${error}`);
});
}
function creatRecognizeTip(tip,text) {
tip.showText(`识别中`);
login().then(token => {
GM_log(text);
let subtitle = ''
recognize(token,text,subtitle).then(data => {
let html = '';
html += data.media_info.type ? `类型:${data.media_info.type}
` : '';
html += data.media_info.category ? `分类:${data.media_info.category}
` : '';
html += data.media_info.title ? `标题:${data.media_info.title}
` : '';
html += data.meta_info.season_episode ? `季集:${data.media_info.season_episode}
` : '';
html += data.meta_info.year ? `年份:${data.media_info.year}
` : '';
html += data.meta_info.resource_team ? `制作:${data.meta_info.resource_team}
` : '';
html += data.media_info.tmdb_id ? 'tmdb:' + data.media_info.tmdb_id + '' : 'tmdb:未识别';
tip.showText(html);
}).catch(error => {
tip.showText(`识别失败:${error}`);
})
}).catch(error => {
tip.showText(`登录失败:${error}`);
});
}
(function () {
'use strict';
// 结果面板
class RecognizeTip {
constructor() {
const div = document.createElement('div');
div.hidden = true;
div.setAttribute('style',
`position:absolute!important;
font-size:13px!important;
overflow:auto!important;
background:#fff!important;
font-family:sans-serif,Arial!important;
font-weight:normal!important;
text-align:left!important;
color:#000!important;
padding:0.5em 1em!important;
line-height:1.5em!important;
border-radius:5px!important;
border:1px solid #ccc!important;
box-shadow:4px 4px 8px #888!important;
max-width:350px!important;
max-height:216px!important;
z-index:2147483647!important;`
);
document.documentElement.appendChild(div);
//点击了内容面板,不再创建图标
div.addEventListener('mouseup', e => e.stopPropagation());
this._tip = div;
}
showText(text) { //显示测试结果
this._tip.innerHTML = text;
this._tip.hidden = !1;
}
hide() {
this._tip.innerHTML = '';
this._tip.hidden = true;
}
pop(ev) {
this._tip.style.top = ev.pageY + 'px';
//面板最大宽度为350px
this._tip.style.left = (ev.pageX + 350 <= document.body.clientWidth ?
ev.pageX : document.body.clientWidth - 350) + 'px';
}
}
const tip = new RecognizeTip();
class Icon {
constructor() {
const icon = document.createElement('span');
icon.hidden = true;
icon.innerHTML = ``;
icon.setAttribute('style',
`width:24px!important;
height:24px!important;
background:#fff!important;
border-radius:50%!important;
box-shadow:4px 4px 8px #888!important;
position:absolute!important;
z-index:2147483647!important;`
);
document.documentElement.appendChild(icon);
//拦截二个鼠标事件,以防止选中的文本消失
icon.addEventListener('mousedown', e => e.preventDefault(), true);
icon.addEventListener('mouseup', ev => ev.preventDefault(), true);
icon.addEventListener('click', ev => {
if (ev.ctrlKey) navigator.clipboard.readText()
.then(text => {
this.queryText(text.trim(), ev);
})
.catch(err => {
console.error('Failed to read contents: ', err);
});
else {
const text = window.getSelection().toString().trim().replace(/\s{2,}/g, ' ');
this.queryText(text, ev);
}
});
this._icon = icon;
}
pop(ev) {
const icon = this._icon;
icon.style.top = ev.pageY + 9 + 'px';
icon.style.left = ev.pageX + -18 + 'px';
icon.hidden = !1;
setTimeout(this.hide.bind(this), 2e3);
}
hide() {
this._icon.hidden = true;
}
queryText(text, ev) {
if (text) {
this._icon.hidden = true;
tip.pop(ev);
creatRecognizeTip(tip,text);
}
}
}
const icon = new Icon();
document.addEventListener('mouseup', function (e) {
var text = window.getSelection().toString().trim();
GM_log(text);
if (!text) {
icon.hide();
tip.hide();
}
else icon.pop(e);
});
let rows = document.getElementsByClassName('rowhead');
if (rows.length) {
let first_row = rows[0];
let rd_row = rows[1]
let torrent_name = first_row.nextElementSibling.firstElementChild.text;
let subtitle = rd_row.nextElementSibling.innerText
// let download_link = first_row.nextElementSibling.firstElementChild.href;
// GM_log(download_link);
GM_log(torrent_name);
let table = first_row.parentNode.parentNode.parentNode;
let row = table.insertRow(2);
creatRecognizeRow(row,torrent_name,subtitle)
}
})();