// ==UserScript==
// @name 豆瓣随机漫步者 - 手气不错 - 拯救选择困难症!
// @version 0.1.3
// @namespace Black Rabbit
// @author Black Rabbit
// @description 豆瓣随机漫步者脚本,为电影、读书、音乐的wish、do、collect添加了随机按钮和高亮显示,节省决策资源,拯救选择困难症!目前仅适配栅格视图,列表视图待适配。
// @match http*://book.douban.com/*
// @match http*://movie.douban.com/*
// @match http*://music.douban.com/*
// @match http*://*.douban.com/mine*
// @match http*://*.douban.com/people/*/wish*
// @match http*://*.douban.com/people/*/do*
// @match http*://*.douban.com/people/*/collect*
// @icon https://www.google.com/s2/favicons?sz=64&domain=douban.com
// @original_icon https://img1.doubanio.com/favicon.ico
// @supportURL https://gf.qytechs.cn/scripts/476110
// @grant none
// ==/UserScript==
// Domain Process
var domain = window.location.hostname;
var url = window.location.href;
var hlcolor;
if (domain.includes("book.douban")){ hlcolor = "rgb(40,153,160)"; }//rgb(38,143,149) rgb(234,161,100)
else if (domain.includes("movie.douban")) { hlcolor = "rgb(0,144,217)";} //rgb(40,156,229)
else if (domain.includes("music.douban")){ hlcolor = "rgb(249,92,68)";} //rgb(250,128,0)
// ADD Big Button
(function () {
$("div.inp-btn").css("display","inline-block");
var input = $("div.inp-btn");
var button = $('<button class="feelinglucky"></button>');
button.css({
"width": "83px",
"height": "33px",
"border": "none",
"margin-left":"14px",
"background-color": hlcolor,
"border-radius": "4px",
"font-size": "12px",
"color": "white",
"cursor":"pointer",
"display": "inline-block",
"vertical-align": "top"
});
button.hover(function() {
$( this ).css("opacity","0.88");
}, function() {
$( this ).css("opacity","1");
});
button.text("🎲 手气不错");
button.on("click", function () {
var randomsj = 1; // roll a random subject
// window.open("https://" + domain + "/subject/" + randomsj + "/", "_self");
return false;
});
input.after(button);
})();
// TUNE the Annual position
(function () {
var anltarget = $("fieldset");
if (domain.includes("book.douban")){
$(".bookannual").appendTo(anltarget);
$(".bookannual").attr("style","margin-left:200px !important; top:-11px;");
}
else if (domain.includes("movie.douban")) {
$(".movieannual").appendTo(anltarget);
$(".movieannual").attr("style","margin-left:200px !important; top:-11px;");
}
else if (domain.includes("music.douban")){
$(".musicannual").appendTo(anltarget);
$(".musicannual").attr("style","margin-left:270px !important; top:-11px;");
}
})();
// grid-view list-view Process
var urlkw = ["/wish", "/do", "/collect", "/mine"];
if (urlkw.some(function (substring) { // includes keyword then run
return url.includes(substring);
})) {
// Get link reference
var pageslc;
var href;
var range = 15;
var paginator = document.getElementsByClassName("paginator");
var pexist = Array.from(paginator).find(function(element) {
return element.className === "paginator";
});
if (pexist) {
pageslc = pexist.getElementsByTagName("a")[0];
href = pageslc.href;
} else { // paginator is not exist
href = window.location.href.split("#")[0]; // use default url
}
// calculate max PAGE nums
var sjnumspan = document.getElementsByClassName ("subject-num")[0];
var parts = sjnumspan.textContent.split ("/");
var total = Number(parts[1].trim());
if (total <= 15) {
range = total;
}
var pagemax = Math.ceil (total / 15);
//var pagemax = Number(document.getElementsByClassName("thispage")[0].getAttribute("data-total-page")) || sjnum;
// Function: Add fixed RANDOM links in Grid mode
(function() {
'use strict';
if (domain.includes("book.douban")){
$('div.opt-bar.clearfix').after('<div class="nicetry" style="width:675px; height:24px ;"><a id="random" href="#" style="float: right; margin-right: 0px;">🎲 手气不错! </a></div>');
}
else {
$('div.opt-bar.mb30.clearfix').css("margin-bottom","0px");
$('div.opt-bar.mb30.clearfix').after('<div class="nicetry" style="width:675px; height:24px ;"><a id="random" href="#" style="float: right; margin-right: 0px;">🎲 手气不错! </a></div>');
$('div.grid-view > div.item.comment-item:first-child').css({"padding-top":"20px","border-top-width":"1px","border-top-style":"dashed","border-top-color":"rgb(221,221,221)","margin-top":"5px"});
$('div.grid-view').css({"border-top-width":"0px","padding-top":"0px"});
}
//$('div.mode').after('<div class="nicetry" style="width:200px; height:21px ;"><a id="random" href="#" style="float: right; margin-right: 0px;">🎲 手气不错! </a></div>');
$('#random').on({
click: function () {
amble();
return false;//阻止默认行为
}
});
// $('#random').after('<span class="gray-dot">·</span>');
})();
// Function: Locate to RANDOM subject then highlight it -- according the #num at the end of Url
var hash = window.location.hash;
// 如果有 #
if (hash) {
// 去掉 # 号并转换为数字
var index = Number(hash.slice(1));
// 如果是有效的数字
if (index >= 1 && index <= 15) {
// 根据 CSS 选择器获取特定的元素
var element;
if (domain.includes("book.douban")){
var interest = document.getElementsByClassName("interest-list")[0];
var icount = interest.children.length;
if (icount<15) { index = index % icount + 1; }
element = document.querySelector(
"ul.interest-list > li.subject-item:nth-child(" + (index) + ")"
);
} else {
var grid = document.getElementsByClassName("grid-view")[0];
var count = grid.children.length;
if (count<15) { index = index % count + 1; } // % 求余
element = document.querySelector(
"div.grid-view > div.item.comment-item:nth-child(" + (index) + ")"
);
}
// 如果找到元素
if (element) {
// 给元素添加一个样式类
element.classList.add("highlighted");
//element.style.border = "5px solid yellow"; // 添加一个 5px 的黄色边框
element.style.setProperty("border", "5px solid "+ hlcolor, "important"); // 添加一个带有!important标记的5px黄色边框
element.style.setProperty("border-radius", "8px");
if (domain.includes("book.douban")){
$('li.highlighted > div.info > div.short-note > div.opt-l > a.d_link').after('<a id="again" href="#" style="float: right; margin-right: 0px;">🎲 再来一次! </a>');
} else {
$('div.highlighted > div.info > ul > li > span.date').after('<a id="again" href="#" style="float: right; margin-right: 10px;">🎲 再来一次! </a>');
}
$('#again').on({
click: function () {
amble();
return false;
}
});
// 让元素滚动到可视区域
element.scrollIntoView({
behavior: "smooth", // 平滑滚动
block: "center", // 垂直方向居中对齐
inline: "center" // 水平方向居中对齐
});
}
}
}
// Function: wish do collect random method
function amble(){
var randomsj = Math.floor(Math.random() * total ) + 1; // 根据 total 生成一个随机项目 1 ~ total
var pg = Math.floor(randomsj / 15) * 15; // 随机项目位于第几页 0 ~ pagemax-1
var sj = randomsj % 15 + 1; // randomsj / 15 的余数,显示在 pg 的第 1 ~ 15 项
var oldHref = location.href.split("#")[0]; // storage opening href
var newHref = href.replace(/start=\d+/, "start=" + pg); // 替换 start 参数
window.open(newHref + "#" + sj,"_self");
//location.replace(newHref + "#" + sj);
if (total<=15 || newHref==oldHref){
location.reload();
}
}
}
// Function: Add RANDOM links in List mode