给哔哩网页添加推荐

抛弃哔哩哔哩App的第二步~

目前為 2023-06-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name         给哔哩网页添加推荐
// @namespace    https://palhube666.wodemo.com/
// @version      0.1
// @description  抛弃哔哩哔哩App的第二步~
// @author       呆毛飘啊飘2171802813
// @run-at       document-end
// @match        *.bilibili.com/*
// @exclude-match  *.bilibili.com/video/*
// @grant        none
// @license GPL
// ==/UserScript==



var a=document.getElementById('zhu');
/*防止多次加载,比如在via时会加载两次..*/
if(a==null){

/*添加按钮css样式*/
Vie_addStyle('.tplb-text{display: block; width: 100%; position: absolute; bottom: 0; left: 0; padding:5px 10px; font-size: 12px; overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellipsis; white-space:nowrap;}.tplb-tag{top: 5px; left: 5px;display: block; position: absolute; padding: 2px 0; width: 100%;}.tplb-tag .tag{background-color: rgba(0,0,0,0.6); color: #fff;display: inline-block; margin: 0 4px; padding: 0 5px; font-size: 12px; border-radius: 2px;}.tplb__detail .title{ margin: 10px 0 0; font-size: 15px; font-weight: 700;}.tplb__thumb{border-radius:5px;display: block; position: relative; background-position: 50% 50% !important; background-size: cover !important; overflow: hidden; box-shadow: 0 1px 2px rgba(0,0,0,.1);}.tplb__thumb img{width:100%}#mian:before,#mian:after{ display: table; content: " "; clear: both;}#mian{width:100%;padding:0;margin-bottom: 50px;}#mian li{list-style:none;}.tplb-text,.pic-title-bottom{background-repeat: no-repeat; background-image: linear-gradient(transparent,rgba(0,0,0,.5)); color: #fff;}#mian p{text-overflow:ellipsis;white-space:nowrap;overflow:hidden;}#mian a{TEXT-DECORATION:none;font-size:12px;color:#000000;}.item{background-color: #FFFFFF;display: flex;flex-direction: column;padding-left: 20px;padding-right: 15px;padding-top: 6px;padding-bottom: 15px;}.item:active{background-color: #ededed;}.vieh3{display: -webkit-box!important;-webkit-box-orient: vertical;-webkit-line-clamp: 2;overflow: hidden;font-size: 16.5px;font-weight: 400;height:33px;margin: 0;margin-top: 4px;}.viespan{font-size: 10px;height:20px;color: #999999;}.an,.an:hover{text-align:center;border:1px solid #000;color:#000;background:0 0;border-radius:2px;padding:8px 12px;margin:2px 10px 10px 2px;cursor:pointer;display:inline-block;font-size:14px;min-width:72px;text-decoration:none}.an:hover{background:rgba(0,0,0,.5)}');


/*添加推荐视频列表的底层布局*/
var a=document.getElementsByClassName('video-list-box')[0];
var divv = document.createElement('div');
divv.id = 'zhu';
divv.style = 'word-wrap:break-word;overflow:auto;background:white;position:fixed;left:0px;top:80.5px;z-index:999;width:100%;height:90%';
a.appendChild(divv);

var a=document.getElementById('zhu');
var pp = document.createElement('a');
pp.onclick = function(){Vie_getA()};
pp.innerHTML = '<p aria-hidden="true" class="an">按分类推荐</p>';
a.appendChild(pp);

var a=document.getElementById('zhu');
var pp = document.createElement('a');
pp.onclick = function(){Vie_getB()};
pp.innerHTML = '<p aria-hidden="true" class="an">APP主页推荐</p>';
a.appendChild(pp);

var a=document.getElementById('zhu');
var pp = document.createElement('a');
pp.onclick = function(){Vie_close()};
pp.innerHTML = '<p aria-hidden="true" class="an">关闭</p>';
a.appendChild(pp);

var a=document.getElementById('zhu');
var divv = document.createElement('ul');
divv.id = 'mian';
a.appendChild(divv);

Vie_close();

/*添加推荐按钮*/
var b=document.getElementsByClassName('v-switcher__header__tabs__list')[0];
var bn=b.innerHTML;
var wyi=Number(Vie_cutText(bn,'translateX(','px'))+57.6;
var kwyi='translateX('+wyi.toString()+'px)';
var nwyi='translateX('+Vie_cutText(bn,'translateX(','px')+'px)';
bn='<a style="color:#00ffdd" href="javascript:Vie_ann();" class="v-switcher__header__tabs__item"><!----> <span>推荐</span></a>'+bn;
var nw='width: '+Vie_cutText(bn,'style="width: ','px')+'px';
bn=bn.replace(nwyi,kwyi).replace(nw,'width: 25px');
document.getElementsByClassName('v-switcher__header__tabs__list')[0].innerHTML = bn;
/*添加按钮到下拉菜单(似乎不奏效)*/
var c=document.getElementsByClassName('drawer-box')[0];
var cn='<a href="javascript:Vie_ann();" style="color:#00ffdd" class="link-item">推荐</a>'+c.innerHTML;
document.getElementsByClassName('drawer-box')[0].innerHTML=cn;
}
Vie_ann=()=>{
Vie_show();
}

console.log('over');


/*下面是内部函数,建议不要改动!*/

function Vie_addStyle(txt){
var viestyle=document.createElement('style');
viestyle.type='text/css';
viestyle.innerHTML=txt;
document.getElementsByTagName('head')[0].appendChild(viestyle);
return viestyle;
};

function Vie_cutText(obj,gjc,gc){
var res = obj.substring(obj.lastIndexOf(gjc)+gjc.length,obj.length);
res = res.substring(0,res.indexOf(gc));
return res;
}

function Vie_show(){
document.getElementById('zhu').hidden=false;
Vie_getA();
document.body.style.position = 'fixed';
/*移动指示器*/
var b=document.getElementsByClassName('v-switcher__header__tabs__list')[0];
var bn=b.innerHTML;
var kwyi='translateX(16px)';
window.localStorage.setItem('Vie_zhisq',Vie_cutText(bn,'translateX(','px'));
var nwyi='translateX('+Vie_cutText(bn,'translateX(','px')+'px)';
var nw='width: '+Vie_cutText(bn,'style="width: ','px')+'px';
bn=bn.replace(nwyi,kwyi).replace(nw,'width: 25px');
document.getElementsByClassName('v-switcher__header__tabs__list')[0].innerHTML = bn;
}

function Vie_close(){
document.getElementById('zhu').hidden=true;
document.body.style.position = 'static';
}

function Vie_getB(){
alert('正在完善...');
/*
document.getElementById('mian').innerText='';
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://app.bilibili.com/x/v2/feed/index');
xhr.send(); 
xhr.onreadystatechange = function(){
var txt = xhr.responseText;
if(txt == null || txt == '' || txt == undefined)
{
}
else
{
if ( xhr.readyState == 4 && xhr.status == 200 )
{
var obj=JSON.parse(txt);
alert(txt);
}
}
}
*/
}



function Vie_getA(){
document.getElementById('mian').innerText='';
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.bilibili.com/index/ding.json');
xhr.send(); 
xhr.onreadystatechange = function(){
var txt = xhr.responseText;
if(txt == null || txt == '' || txt == undefined)
{
}
else
{
if ( xhr.readyState == 4 && xhr.status == 200 )
{
var obj=JSON.parse(txt);

for (var key in obj) {
if(key=='list' || key=='results' || key=='pages' || key=='code'){
}
else{
for (var i in obj[key]) {
var div = document.createElement("li");
div.className = "tplb-sm-2";
div.style = 'width:45%;margin:2.5%;padding:0;border: 0;float:left;position:relative;min-height:1px;';

var spn = document.createElement("span");
spn.className = "viespan";
spn.innerText = obj[key][i].short_link_v2;
spn.style.display="none";
div.appendChild(spn);

var pic = document.createElement("img");
pic.src=obj[key][i].pic;
pic.style='height:100px;border-radius:10px;margin: 0;padding:0;border: 0;';
div.appendChild(pic);

div.className = "item";
var h3 = document.createElement("h3");
h3.innerText = obj[key][i].title;
h3.className = "vieh3";

div.appendChild(h3);
var span = document.createElement("span");
span.className = "viespan";
span.innerText = '浏览:'+obj[key][i].stat.view+';评论:'+obj[key][i].stat.danmaku+';分类:'+key;
div.appendChild(span);
document.getElementById("mian").appendChild(div);

div.onclick=function(){ window.open(this.firstElementChild.innerText,"_blank"); };
}
}
}
var div = document.createElement("li");
div.className = "item";
var h3 = document.createElement("h3");
h3.innerText = '没有啦,嘤嘤嘤,点我刷新吧';
h3.className = "vieh3";
div.appendChild(h3);
var span = document.createElement("span");
span.className = "viespan";
span.innerText = '嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤';
div.appendChild(span);
document.getElementById("mian").appendChild(div);
div.onclick=function(){Vie_getA()};
}
}
}
}