// ==UserScript==
// @name Better Luogu!
// @namespace http://tampermonkey.net/
// @version 1.8.1
// @description:zh 洛谷扩展
// @description Luogu Expansion
// @author volatile
// @match *://*.luogu.com.cn/*
// @match *://*.luogu.com/*
// @icon https://fecdn.luogu.com.cn/luogu/logo.png?0fdd294ff62e331d2f70e1a37ba4ee02
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// @grant GM_getResourceText
// @grant unsafeWindow
// @connect baidu.com
// @connect greasyfork.org
// @require https://unpkg.com/sweetalert/dist/sweetalert.min.js
// @license MIT
// ==/UserScript==
(function() {
'use strict'
var css = ".swal-overlay {background-color: rgba(0, 0, 0, 0.5);}";
var style = document.createElement("style");
style.innerHTML = css;
document.head.appendChild(style);
let br = document.createElement('br');
let nowurl = window.location.href;
let nowcolor1;
let nowcolor="";
if(nowurl == 'https://www.luogu.com.cn/'){
nowcolor1 = document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').className;
for(let i=0;i<nowcolor1.length;i++){
if(nowcolor1[i]==' ') break;
nowcolor = nowcolor + nowcolor1[i];
}
let uid = document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').href;
let _uid = "";
for(let i=uid.length-1;i>=0;i--){
if(!(uid[i]>='0'&&uid[i]<='9')) break;
_uid = uid[i] + _uid;
}
setcookie('uid',_uid,114514,'/','luogu.com.cn',true);
}
function setcookie(name,value,days,path,domain,secure){
let expires = "";
if(days){
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
var cookieString = name + "=" + (value || "") + expires;
if(path) cookieString += "; path=" + path;
if(domain) cookieString += "; domain=" + domain;
if(secure && window.location.protocol === "https:") cookieString += "; secure";
document.cookie = cookieString;
}
function getcookie(name){
const cookiestring = document.cookie;
const cookies = cookiestring.split('; ');
for(const cookie of cookies){
const [cookiename, cookievalue] = cookie.split('=');
if(cookiename === name) return cookievalue;
}
if(name == 'color'){
setcookie('color',nowcolor,114514,'/','luogu.com.cn',true);
return nowcolor;
}
else if(name == 'hanger'){
setcookie('hanger','null',114514,'/','luogu.com.cn',true);
return "null";
}
else if(name == 'version'){
setcookie('version','1.8.1',114514,'/','luogu.com.cn',true);
return "1.8.1";
}
else if(name == 'update'){
setcookie('update','true',114514,'/','luogu.com.cn',true);
return "true";
}
else if(name == 'uid'){
setcookie('null','true',114514,'/','luogu.com.cn',true);
return "null";
}
}
function deletecookie(name){
document.cookie = name+ '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
}
async function sendMessage(uid,content){
await fetch("https://www.luogu.com.cn/api/chat/new", {
headers: [
["content-type", "application/json"],
["referer", "https://www.luogu.com.cn/"],
["x-csrf-token", document.querySelector("meta[name=csrf-token]").content],
],
body: JSON.stringify({
user: uid,
content: content,
}),
method: "POST",
});
}
async function sendBenBen(content){
await fetch("https://www.luogu.com.cn/api/feed/postBenben", {
headers: [
["content-type", "application/json"],
["referer", "https://www.luogu.com.cn/"],
["x-csrf-token", document.querySelector("meta[name=csrf-token]").content],
],
body: JSON.stringify({
content: content
}),
method: "POST",
});
}
GM_xmlhttpRequest({
method: "GET",
url: 'https://greasyfork.org/zh-CN/scripts/487511-better-luogu',
onload: function(response){
let parser = new DOMParser();
let doc = parser.parseFromString(response.responseText, "text/html");
let version = doc.querySelector('dd.script-show-version');
if(version.textContent != '1.8.1' && nowurl == 'https://www.luogu.com.cn/'){
if(getcookie('update')=='true'){
swal("Better Luogu!","有新版本",{
buttons: {
no: {
text: "7天内不再提醒",
value: "false",
},
yes: {
text: "更新",
value: "true",
}
},
})
.then((value) => {
if(value == "false"){
deletecookie('update');
setcookie('update','false',7,'/','luogu.com.cn',true);
swal("Better Luogu!","好的,你可以在 NEWLG 菜单中点击更新来更新");
}
else window.open('https://greasyfork.org/zh-CN/scripts/487511-better-luogu','_blank');
});
}
}
}
});
function colorschange(changecolor){//emm,这个函数,额,可能要用吧
if(changecolor == 'lg-fg-gray') return 'rgb\(191, 191, 191\)';
else if(changecolor == 'lg-fg-blue') return 'rgb\(14, 144, 210\)';
else if(changecolor == 'lg-fg-green') return 'rgb\(94, 185, 94\)';
else if(changecolor == 'lg-fg-orange') return 'rgb\(230, 126 34\)';
else if(changecolor == 'lg-fg-red') return 'rgb\(231, 76, 60\)';
else if(changecolor == 'lg-fg-purple') return 'rgb\(142, 68, 173\)';
else if(changecolor == 'lg-fg-brown') return 'rgb\(173, 139, 0\)';
}
function update(){
swal("Better Luogu!","“千万别点!!!”里奇怪的东西分裂了!",{
buttons: {
cancel: "取消",
_continue: {
text: "继续",
value: "continue",
},
},
})
.then((value) => {
if(value == "continue"){
swal("Better Luogu!","添加了比赛题目返回题目列表的功能",{
buttons: {
cancel: "取消",
_continue: {
text: "继续",
value: "continue"
}
}
})
.then((value1) => {
if(value1 == "continue"){
swal("Better Luogu!","修复了在除了主页外的地方图标不显示的 bug");
}
});
}
});
}
let changecolor = getcookie('color');
let tagchange = getcookie('tag');
let tag = document.createElement("span");
let tagcolor;
if(changecolor != 'lg-fg-blue' && changecolor != 'lg-fg-orange'){
tagcolor = changecolor.replace('fg','bg');
tag.className = 'am-badge am-radius ' + tagcolor;
}
else if(changecolor == 'lg-fg-blue'){
tagcolor = 'color-none';
tag.className = 'am-badge am-radius ' + tagcolor;
tag.style.background = 'rgb(52, 152, 219)';
}
else if(changecolor == 'lg-fg-orange'){
tagcolor = 'color-none';
tag.className = 'am-badge am-radius ' + tagcolor;
tag.style.background = 'rgb(243, 156, 17)';
}
if(changecolor != 'lg-fg-brown') tag.textContent = tagchange;
else tag.textContent = '作弊者';
if(getcookie('version')!='1.8.1'&&nowurl=='https://www.luogu.com.cn/'){
deletecookie('version');
setcookie('version','1.8.1',114514,'/','luogu.com.cn',true);
update();
}
//首页
if(nowurl == 'https://www.luogu.com.cn/'){
//日历
const today=new Date();
const year=today.getFullYear();
const month=String(today.getMonth()+1);
const day=String(today.getDate());
let date_html='<h2>今天是 '+year+'-'+month+'-'+day+'</h2>';
if(month==1&&day==1){
date_html+='<h2>今天是元旦,新的一年,新的开始</h2>';
}
else if(month==2&&day==29){
date_html+='<h2>今年是闰年</h2>';
}
else if(month==4&&day==1){
date_html+='<h2>今天是愚人节</h2>';
}
else if(month==5&&day==1){
date_html+='<h2>今天是劳动节</h2>';
}
else if(month==6&&day==1){
date_html+='<h2>今天是儿童节</h2>';
}
else if(month==9&&day==10){
date_html+='<h2>今天是教师节</h2>';
}
else if(month==10&&day==1){
date_html+='<h2>今天是国庆节,七天小长假,启动!</h2>'
}
else if(month==12&&day==31){
date_html+='<h2>明年见</h2>';
}
else date_html+='<h2>欢迎!</h2>';
let date = document.createElement('div');
date.className = 'lg-article';
date.innerHTML = date_html;
document.querySelector('div.lg-right > div:nth-child(1)').insertAdjacentElement('beforebegin', date);
//更改用户名颜色
let color = document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a');
if(changecolor != 'null') color.className=changecolor;
function removeDivWithText(text) {
document.querySelectorAll('div').forEach(div => {
if(div.textContent === text) div.remove();
});
}
function findElement(element, text, cclass){
document.querySelectorAll(element).forEach(E => {
if(E.textContent === text) E.className = cclass;
});
}
removeDivWithText('暂无推荐');
removeDivWithText('洛谷根据您近期的做题情况,使用机器学习自动为您推荐符合您目前程度的题目。本列表每日更新一次。');
GM_xmlhttpRequest({
method: "GET",
url: 'https://top.baidu.com/board?tab=realtime',
onload: function(response){
let rs = new DOMParser();
let doc = rs.parseFromString(response.responseText, "text/html");
for(let i=1;i<=5;i++){
let th = i.toString();
let selector = '#sanRoot > main > div.container.right-container_2EFJr > div > div:nth-child(2) > div:nth-child(' + th + ') > div.content_1YWBm > a > div.c-single-text-ellipsis';
let _rs = doc.querySelector(selector);
let selector2 = '#sanRoot > main > div.container.right-container_2EFJr > div > div:nth-child(2) > div:nth-child(' + th + ') > div.content_1YWBm > a';
let rslink = doc.querySelector(selector2);
let link = document.createElement("a");
link.href = rslink.href;
link.textContent = _rs.textContent;
let RS = document.createElement("p");
RS.appendChild(link);
findElement('h2','智能推荐','zntj');
document.getElementsByClassName('zntj')[0].parentNode.append(RS);
}
}
});
let links = document.querySelector('.lg-article.am-hide-sm');
links.insertAdjacentHTML('beforeend','<p><strong>Better Luogu!</strong><br><a href="https://www.volatiles.us.kg/" target="_blank">Better Luogu!</a><br><a href="https://greasyfork.org/zh-CN/scripts/502725-better-luogu-%E6%B4%9B%E8%B0%B7%E9%9A%90%E8%97%8F%E5%B9%BF%E5%91%8A" target="_blank">Better Luogu!-洛谷隐藏广告</a></p>');
}
window.onload=function(){
let _hanger = getcookie('hanger');
let __hanger = document.createElementNS('http://www.w3.org/2000/svg','svg');
__hanger.setAttribute('width','16');
__hanger.setAttribute('height','16');
__hanger.setAttribute('viewBox','0 0 16 16');
__hanger.setAttribute('style','margin-bottom: -3px;');
if(_hanger == 'green')__hanger.setAttribute('fill','#5eb95e');
else if(_hanger == 'blue') __hanger.setAttribute('fill','#3498db');
else if(_hanger == 'golden') __hanger.setAttribute('fill','#f1c40f');
if(_hanger != 'null'&&document.getElementsByClassName('sb_amazeui')[0] != null) document.getElementsByClassName('sb_amazeui')[0].remove();
let ___hanger = document.createElementNS('http://www.w3.org/2000/svg','path');
___hanger.setAttribute('d','M16 8C16 6.84375 15.25 5.84375 14.1875 5.4375C14.6562 4.4375 14.4688 3.1875 13.6562 2.34375C12.8125 1.53125 11.5625 1.34375 10.5625 1.8125C10.1562 0.75 9.15625 0 8 0C6.8125 0 5.8125 0.75 5.40625 1.8125C4.40625 1.34375 3.15625 1.53125 2.34375 2.34375C1.5 3.1875 1.3125 4.4375 1.78125 5.4375C0.71875 5.84375 0 6.84375 0 8C0 9.1875 0.71875 10.1875 1.78125 10.5938C1.3125 11.5938 1.5 12.8438 2.34375 13.6562C3.15625 14.5 4.40625 14.6875 5.40625 14.2188C5.8125 15.2812 6.8125 16 8 16C9.15625 16 10.1562 15.2812 10.5625 14.2188C11.5938 14.6875 12.8125 14.5 13.6562 13.6562C14.4688 12.8438 14.6562 11.5938 14.1875 10.5938C15.25 10.1875 16 9.1875 16 8ZM11.4688 6.625L7.375 10.6875C7.21875 10.8438 7 10.8125 6.875 10.6875L4.5 8.3125C4.375 8.1875 4.375 7.96875 4.5 7.8125L5.3125 7C5.46875 6.875 5.6875 6.875 5.8125 7.03125L7.125 8.34375L10.1562 5.34375C10.3125 5.1875 10.5312 5.1875 10.6562 5.34375L11.4688 6.15625C11.5938 6.28125 11.5938 6.5 11.4688 6.625Z');
__hanger.appendChild(___hanger);
let _hanger_ = document.createElement("a");
_hanger_.href = 'https://www.luogu.com/discuss/142324';
_hanger_.appendChild(__hanger);
let button = document.createElement("a");
button.innerHTML = '<button id="NLTB" style=\"background-color: rgb\(94,114,228\);border-radius: 7px;color: white;border: none;padding: 7px 12px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;border: none;box-shadow: 2px 3px 7px #000;\">BETTER</button>';
if(nowurl == 'https://www.luogu.com.cn/' || nowurl == 'https://www.luogu.com.cn/chat' || nowurl.includes('https://www.luogu.com.cn/user/notification') || (nowurl.includes('https://www.luogu.com.cn/user/') && !nowurl.includes('https://www.luogu.com.cn/user/setting'))) document.querySelector('#app > div.main-container > div.wrapper.wrapped.lfe-body.header-layout.tiny > div.container > nav').insertAdjacentElement('beforebegin', button);
else{
let user_nav = document.querySelector('div.user-nav[data-v-2dfcfd35]');
let c_usernav = document.querySelector('nav[data-v-2dfcfd35]');
user_nav.style.verticalAlign = 'middle';
c_usernav.insertBefore(button,c_usernav.firstChild);
button.href = 'https://www.volatiles.us.kg/';
}
const tb = document.getElementById("NLTB");
function reloadmenu(){
swal("Better Luogu!","更改成功","success",{button: "刷新"}).then((value) => {location.reload();});
}
function changeusercolor(){
swal({
title: "Better Luogu!",
text: "输入一个词语(规则见油叉插件页)",
content: "input",
button: {
text: "更改",
closeModal: false,
},
})
.then((input) => {
if(input=='灰名'||input=='gray'){
deletecookie('color');
setcookie('color','lg-fg-gray',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='蓝名'||input=='blue'){
deletecookie('color');
setcookie('color','lg-fg-blue',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='绿名'||input=='green'){
deletecookie('color');
setcookie('color','lg-fg-green',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='橙名'||input=='orange'){
deletecookie('color');
setcookie('color','lg-fg-orange',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='红名'||input=='red'){
deletecookie('color');
setcookie('color','lg-fg-red',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='紫名'||input=='purple'){
deletecookie('color');
setcookie('color','lg-fg-purple',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='棕名'||input=='brown'){
deletecookie('color');
setcookie('color','lg-fg-brown',114514,'/','luogu.com.cn',true);
reloadmenu();
}
else if(input=='复原'||input=='recover'){
deletecookie('color');
setcookie('color',nowcolor,114514,'/','luogu.com.cn',true);
reloadmenu();
}
else swal("Better Luogu!","请按规范输入","error");
});
}
function changehanger(){
swal("Better Luogu!","选择一个勾子", {
buttons: {
cancel: "取消",
greenhanger: {
text: "绿勾",
value: "greenhanger",
},
bluehanger: {
text: "蓝勾",
value: "bluehanger",
},
goldenhanger: {
text: "金勾",
value: "goldenhanger",
},
nullhanger: {
text: "无勾",
value: "nullhanger",
},
}
})
.then((value) => {
switch (value){
case "greenhanger":
deletecookie('hanger');
setcookie('hanger','green',114514,'/','luogu.com.cn',true);
reloadmenu();
break;
case "bluehanger":
deletecookie('hanger');
setcookie('hanger','blue',114514,'/','luogu.com.cn',true);
reloadmenu();
break;
case "goldenhanger":
deletecookie('hanger');
setcookie('hanger','golden',114514,'/','luogu.com.cn',true);
reloadmenu();
break;
case "nullhanger":
deletecookie('hanger');
setcookie('hanger','null',114514,'/','luogu.com.cn',true);
reloadmenu();
break;
}
});
}
function changetag(){
swal({
title: "Better Luogu!",
text: "输入一个tag(无输入以取消)",
content: "input",
button: {
text: "更改",
closeModal: false,
},
})
.then((value) => {
if(getcookie('color') == 'lg-fg-brown') swal("Better Luogu!","当前为棕名","error");
else if(value.length > 10) swal("Better Luogu!","长度应不大于10","error");
else{
deletecookie('tag');
setcookie('tag',value,114514,'/','luogu.com.cn',true);
reloadmenu();
}
});
}
function not1(){
let words = ["78;101;118;101;114;32;103;111;110;110;97;32;103;105;118;101;32;121;111;117;32;117;112;","99;110;109;","83;66;","73;65;75;73;79;73;","49;49;52;53;49;52;","49;57;49;57;56;49;48;","115;116;111;","111;114;122;"];
let cishu = Math.floor(Math.random()*10)+1;
for(let i=1;i<=cishu;i++){
let qwq = words[Math.floor(Math.random()*words.length)];
let awa="";
let t=0;
for(let j=0;j<qwq.length;j++){
if(qwq[j]!=';'){
t*=10;
t+=parseInt(qwq[j]);
}
else{
awa+=String.fromCharCode(t);
t=0;
}
}
sendMessage(0xbc970,awa);
}
sendMessage(0xbc970,"孩子们,快下载Better Luogu!吧!");
swal("Better Luogu!","好像有点问题?!","error",{button: "前往私信"}).then((value) => {window.open('https://www.luogu.com.cn/chat','_self');});
}
function not2(){
sendBenBen("这个人AKIOI");
sendBenBen("Better Luogu!真好用");
let uid_=getcookie('uid');
if(uid_!='null') swal("Better Luogu!","好像有点问题?!","error",{button: "前往动态"}).then((value) => {window.open('https://www.luogu.com.cn/user/'+uid_+'#activity','_self');});
else swal("Better Luogu!","好像有点问题?!","error",{button: "前往主页犇犇"}).then((value) => {window.open('https://www.luogu.com.cn','_self');});
}
function not(){
swal("Better Luogu!","不怕死就选择一个", {
buttons: {
not1: {
text: "1",
value: "not1"
},
not2: {
text: "2",
value: "not2"
}
},
})
.then((value) => {
switch (value){
case "not1":
not1();
break;
case "not2":
not2();
break;
}
});
}
tb.addEventListener('click', function(){
if(nowurl == 'https://www.luogu.com.cn/'){
swal("Better Luogu!","选择一个更改", {
buttons: {
username: {
text: "颜色",
value: "color",
},
hanger: {
text: "勾子",
value: "hanger",
},
tag: {
text: "tag",
value: "tag",
},
update: {
text: "更新",
value: "update",
},
content: {
text: "内容",
value: "content"
},
not: {
text: "千万别点!!!",
value: "not"
}
},
})
.then((value) => {
switch (value){
case "color":
changeusercolor();
break;
case "hanger":
changehanger();
break;
case "tag":
changetag();
break;
case "update":
deletecookie('update');
setcookie('update','true',114514,'/','luogu.com.cn',true);
window.open('https://greasyfork.org/zh-CN/scripts/487511-better-luogu','_self');
break;
case "content":
update();
break;
case "not": not();
}
});
}
});
if(nowurl == 'https://www.luogu.com.cn/'){
if(getcookie('hanger')!='null'){
document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').innerHTML += ' ';
document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').appendChild(_hanger_);
}
if(getcookie('tag')!=''){
document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').innerHTML += ' ';
document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').appendChild(tag);
}
}
else if(nowurl.includes('https://www.luogu.com.cn/user/')&&!nowurl.includes('https://www.luogu.com.cn/user/setting')){
if(document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div:nth-child(2)').className == 'introduction marked') document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div:nth-child(2)').removeAttribute('style');
else document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div:nth-child(2)').remove();
let jieshao = document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div.introduction.marked');
if(jieshao.style.display == 'none') jieshao.removeAttribute('style');
}
else if(nowurl.includes('?contestId=')){
let contestID = "";
let contest = nowurl.toString();
for(let i=contest.length-1;i>=0;i--){
if(!(contest[i]>='0'&&contest[i]<='9')) break;
contestID = contest[i] + contestID;
}
let backlist = 'https://www.luogu.com.cn/contest/'+contestID+'#problems';
let backbtn = document.createElement('a');
backbtn.href = backlist;
backbtn.className = 'color-default';
backbtn.innerHTML = '<svg data-v-5a5fcbaa="" data-v-0640126c="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="right-from-bracket" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="svg-inline--fa fa-right-from-bracket"><path data-v-5a5fcbaa="" data-v-0640126c="" fill="currentColor" d="M377.9 105.9L500.7 228.7c7.2 7.2 11.3 17.1 11.3 27.3s-4.1 20.1-11.3 27.3L377.9 406.1c-6.4 6.4-15 9.9-24 9.9c-18.7 0-33.9-15.2-33.9-33.9l0-62.1-128 0c-17.7 0-32-14.3-32-32l0-64c0-17.7 14.3-32 32-32l128 0 0-62.1c0-18.7 15.2-33.9 33.9-33.9c9 0 17.6 3.6 24 9.9zM160 96L96 96c-17.7 0-32 14.3-32 32l0 256c0 17.7 14.3 32 32 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-64 0c-53 0-96-43-96-96L0 128C0 75 43 32 96 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32z" class=""></path></svg>返回题目列表';
document.querySelector('#app > div.main-container > main > div > section.side > div:nth-child(2)').appendChild(br);
document.querySelector('#app > div.main-container > main > div > section.side > div:nth-child(2)').appendChild(backbtn);
}
}
})();