// ==UserScript== // @name 卡特自訂首頁&收藏文章&黑名單 // @namespace http://tampermonkey.net/ // @version 0.1.01 // @description RT // @author SmallYue1 // @match https://kater.me* // @match https://kater.me/* // @require https://code.jquery.com/jquery-3.4.1.min.js // @require https://greasyfork.org/scripts/14208-datejs/code/Datejs.js?version=89671 // @grant GM_getValue // @grant GM_setValue // @downloadURL none // ==/UserScript== var BlackList,BlockList,FavoriteList; function 新增功能(ul) { 收藏文章(ul); 自訂首頁(ul); } function 覆寫樓層(Li_User) { if(Li_User.parentNode.lastElementChild.className !== "TextEditor-toolbar") { var Li_BlackList = document.createElement('li'); var Btn_BlackList = document.createElement('button'); var Icon_BlackList = document.createElement('i'); var href = Li_User.parentNode.firstElementChild.firstElementChild.firstElementChild.firstElementChild.href; Li_BlackList.setAttribute("class","TextEditor-toolbar"); Btn_BlackList.setAttribute("class","Button Button--icon Button--link hasIcon"); if(BlackList.indexOf(href) === -1) { Icon_BlackList.setAttribute("class","icon far fa-eye Button-icon"); Li_User.parentNode.parentNode.parentNode.childNodes[2].setAttribute("style",""); } else if(BlackList.indexOf(href) !== -1) { Icon_BlackList.setAttribute("class","icon far fa-eye-slash Button-icon"); Li_User.parentNode.parentNode.parentNode.childNodes[2].setAttribute("style","display:none;"); } Li_User.parentNode.appendChild(Li_BlackList); Li_BlackList.appendChild(Btn_BlackList); Btn_BlackList.appendChild(Icon_BlackList); Li_BlackList.addEventListener("click",function(e) { var icon = this.firstElementChild.firstElementChild; var href = this.parentNode.firstElementChild.firstElementChild.firstElementChild.firstElementChild.href; if(icon.getAttribute("class") !== "icon far fa-eye-slash Button-icon") { //新增黑名單 if(BlackList.indexOf(href) === -1) { BlackList.push(href) GM_setValue("Kater_BlackList", BlackList); this.parentNode.parentNode.parentNode.childNodes[2].setAttribute("style","display:none;"); } icon.setAttribute("class","icon far fa-eye-slash Button-icon"); } else { //解除黑名單 if(BlackList.indexOf(href) !== -1) { BlackList.splice(BlackList.indexOf(href), 1); GM_setValue("Kater_BlackList", BlackList); this.parentNode.parentNode.parentNode.childNodes[2].setAttribute("style",""); } icon.setAttribute("class","icon far fa-eye Button-icon"); } },false); } } function 覆寫Ul(Ul,Data_Id) { var Li = document.createElement('li'); Li.innerHTML = ``; Li.setAttribute("class","item-subscription"); Li.setAttribute("data-id",Data_Id); if(FavoriteList.indexOf(Data_Id) === -1) { Li.firstElementChild.firstElementChild.setAttribute("class","icon far fa-star Button-icon"); } else if(FavoriteList.indexOf(Data_Id) !== -1) { Li.firstElementChild.firstElementChild.setAttribute("class","icon fas fa-star Button-icon"); } Ul.insertBefore(Li,Ul.firstElementChild); Li.addEventListener("click",function(){ var Data_Id = this.getAttribute("data-id"); if(FavoriteList.indexOf(Data_Id) === -1) { //新增收藏 FavoriteList.push(Data_Id) GM_setValue("Kater_FavoriteList", FavoriteList); this.firstElementChild.firstElementChild.setAttribute("class","icon fas fa-star Button-icon"); } else if(FavoriteList.indexOf(Data_Id) !== -1) { //解除收藏 FavoriteList.splice(FavoriteList.indexOf(Data_Id), 1); GM_setValue("Kater_FavoriteList", FavoriteList); this.firstElementChild.firstElementChild.setAttribute("class","icon far fa-star Button-icon"); } },false); } function 新增腳本工具列按鈕() { if($("#Script_Btn").length === 0) { var Ul = $("ul.Header-controls")[1]; var Li_Setting = document.createElement('li'); Li_Setting.innerHTML = ``; Ul.appendChild(Li_Setting); 新增功能(Li_Setting.firstElementChild.lastElementChild); Li_Setting.setAttribute("id","Script_Btn"); Li_Setting.setAttribute("class","item-session"); } } function 自訂首頁(Ul) { var Li = document.createElement('li'); Ul.appendChild(Li); Li.innerHTML = ``; Li.setAttribute("class","item-profile"); Li.addEventListener("click",() => {取得Tag資料()}); } function 收藏文章(Ul) { var Li = document.createElement('li'); Ul.appendChild(Li); Li.innerHTML = ``; Li.setAttribute("class","item-profile"); Li.addEventListener("click",() => {建立收藏頁面()}); } function 取得Tag資料() { var datas = [],datas_has_parent = []; var settings = { "url":"https://kater.me/api/tags", "method":"GET", "headers":{ "X-CSRF-TOKEN":app.data.session.csrfToken }, success:function(e){ for(var data of e.data) { try{ if(data.relationships.parent.data.id !== "") { var Tempt2 = { "id":data.id, "position":data.attributes.position, "name":data.attributes.name, "icon":data.attributes.icon, "color":data.attributes.color, "parent_id":data.relationships.parent.data.id }; datas_has_parent.push(Tempt2); } } catch(d) { var Tempt = { "id":data.id, "position":data.attributes.position, "name":data.attributes.name, "icon":data.attributes.icon, "color":data.attributes.color, "parent_id":"" }; datas.push(Tempt); } } 建立設定頁面(datas,datas_has_parent); }, error:function(e){ console.log(e); } } try{ $.ajax(settings); } catch(e) { } } function 建立設定頁面(datas,datas_has_parent) { document.body.innerHTML = ""; var Div = document.createElement('div'); var Tags = document.createElement('ul'); var Text_Color,Icon_Color; Div.setAttribute("style","max-width:100%;height:100%;"); Tags.setAttribute("class","Dropdown-menu dropdown-menu"); Tags.setAttribute("style","width:;display:block;position:static;"); datas.sort(function (a, b) { return a.position < b.position ? -1 : 1; }); datas_has_parent.sort(function (a, b) { return a.position < b.position ? -1 : 1; }); for(var data of datas) { var Tag_Iteam = document.createElement('li'); if(BlockList.indexOf(data.name) === -1) { Icon_Color = data.color; Text_Color = "#111"; } else if(BlockList.indexOf(data.name) !== -1) { Icon_Color = "#b7b7b7"; Text_Color = "#b7b7b7"; } Tag_Iteam.innerHTML = `${data.name}`; Tag_Iteam.setAttribute("id",data.id); Tag_Iteam.setAttribute("name",data.name); Tag_Iteam.setAttribute("color",data.color); Tag_Iteam.setAttribute("style","max-width:100%;height:100%;"); Tag_Iteam.addEventListener("click",(e) => {Tag_Iteam_Onclick_Event(e);},false); Tags.appendChild(Tag_Iteam); } document.body.appendChild(Div); Div.appendChild(Tags); for(data of datas_has_parent) { var Parent_Tag_Iteam = document.getElementById(data.parent_id); Tag_Iteam = document.createElement('li'); if(BlockList.indexOf(data.name) === -1) { Icon_Color = data.color; Text_Color = "#111"; } else if(BlockList.indexOf(data.name) !== -1) { Icon_Color = "#b7b7b7"; Text_Color = "#b7b7b7"; } Tag_Iteam.innerHTML = `${data.name}`; Tag_Iteam.setAttribute("id",data.id); Tag_Iteam.setAttribute("name",data.name); Tag_Iteam.setAttribute("color",data.color); Tag_Iteam.setAttribute("style","max-width:100%;height:100%;"); Tag_Iteam.addEventListener("click",(e) => {Tag_Iteam_Onclick_Event(e);},false); insertAfter(Tag_Iteam,Parent_Tag_Iteam); } var Back_To_Kater = document.createElement('div'); Back_To_Kater.setAttribute("style","text-align:center;width:100%;height:100%;"); Back_To_Kater.innerHTML = `
`; document.body.appendChild(Back_To_Kater); } function Tag_Iteam_Onclick_Event(e) { var Target = $(e.target).parents("li")[0]; var Block_Text = Target.getAttribute("name"); var Text_Color,Icon_Color; if(BlockList.indexOf(Block_Text) === -1) { //新增屏蔽 BlockList.push(Block_Text) GM_setValue("Kater_BlockList", BlockList); Icon_Color = "#b7b7b7"; Text_Color = "#b7b7b7"; } else if(BlockList.indexOf(Block_Text) !== -1) { //解除屏蔽 BlockList.splice(BlockList.indexOf(Block_Text), 1); GM_setValue("Kater_BlockList", BlockList); Icon_Color = Target.getAttribute("color"); Text_Color = "#111"; } Target.firstElementChild.setAttribute("style",`text-align:center;width:100%;color:${Text_Color};`); Target.firstElementChild.firstElementChild.setAttribute("style",`float:initial;color:${Icon_Color};`); } function insertAfter(newE,targetE) { var parentE = targetE.parentNode; if(parentE.lastChild == targetE) { parentE.appendChild(newE); }else { parentE.insertBefore(newE,targetE.nextSibling); } } async function 建立收藏頁面(){ var Ul,Data,Data_Id,Article_Card,Li_Display; document.body.innerHTML = ""; var Back_To_Kater = document.createElement('div'); Back_To_Kater.setAttribute("style","text-align:center;width:100%;height:auto;position:fixed;display:block;z-index:5;"); Back_To_Kater.innerHTML = `
`; document.body.appendChild(Back_To_Kater); var Article_List = document.createElement('ul'); document.body.appendChild(Article_List); Article_List.setAttribute("class","PostsUserPage-list"); Article_List.setAttribute("style","width:80%;padding-top:45px;padding-bottom:45px;padding-left:20%;"); for(var Favorite of FavoriteList) { Data = await 取得收藏資料(Favorite); Article_Card = document.createElement('li'); Article_List.appendChild(Article_Card); Article_Card.innerHTML = `
${Data.title}
`; Ul = Article_Card.lastElementChild.firstElementChild.lastElementChild.firstElementChild; Data_Id = Data.data_id; await 覆寫Ul(Ul,Data_Id); Ul.firstElementChild.removeAttribute("class"); Ul.firstElementChild.firstElementChild.setAttribute("class","Dropdown-toggle Button Button--icon Button--flat"); Ul.firstElementChild.addEventListener("click",function(){ var Data_Id = this.getAttribute("data-id"); $(this).parents("li")[0].setAttribute("style","display:none;"); },false); Li_Display = Article_Card.lastElementChild.firstElementChild.firstElementChild.firstElementChild.lastElementChild; Li_Display.addEventListener("click",function() { var icon = this.firstElementChild.firstElementChild; if(icon.getAttribute("class") !== "icon far fa-eye-slash Button-icon") { this.parentNode.parentNode.parentNode.children[1].setAttribute("style","display:none;"); icon.setAttribute("class","icon far fa-eye-slash Button-icon"); } else { this.parentNode.parentNode.parentNode.children[1].setAttribute("style",""); icon.setAttribute("class","icon far fa-eye Button-icon"); } },false); } } async function 取得收藏資料(Favorite){ try{ return new Promise((resolve,reject) => { var settings = { "url":"https://kater.me/api/posts/"+Favorite, "method":"GET", "headers":{ "X-CSRF-TOKEN":app.data.session.csrfToken }, success:function(e){ for(var item of e.included) { if(item.type === "discussions") { var title = item.attributes.title; var discussions_id = item.id; } } var Data = { "data_id":e.data.id, "floor":e.data.attributes.number, "time":e.data.attributes.createdAt, "contentHtml":e.data.attributes.contentHtml, "username":e.included[0].attributes.username, "avatarUrl":e.included[0].attributes.avatarUrl, "uid":e.included[0].id, "discussions_id":discussions_id, "title":title }; resolve(Data); }, error:function(e){ console.log(e); reject("Error"); } } $.ajax(settings); }); } catch(e) { } } if(!GM_getValue("Kater_BlackList")) { BlackList = []; GM_setValue("Kater_BlackList", []); console.log("偵測到初次使用腳本 O_Ob"); } else { BlackList = GM_getValue("Kater_BlackList"); console.log(BlackList); } if(!GM_getValue("Kater_BlockList")) { BlockList = []; GM_setValue("Kater_BlockList", []); console.log("偵測到初次使用腳本 O_Ob"); } else { BlockList = GM_getValue("Kater_BlockList"); console.log(BlockList); } if(!GM_getValue("Kater_FavoriteList")) { FavoriteList = []; GM_setValue("Kater_FavoriteList", []); console.log("偵測到初次使用腳本 O_Ob"); } else { FavoriteList = GM_getValue("Kater_FavoriteList"); console.log(FavoriteList); } var callback = function (records){ records.map(function(record){ if(record.addedNodes.length != 0) { setTimeout(() => { if(location.href.indexOf("/d/") !== -1) { var Article = $(record.addedNodes[0]).find("article.CommentPost")[0]; if(Article !== undefined) { var Ul = Article.firstElementChild.children[2].firstElementChild.lastElementChild.firstElementChild.lastElementChild; if(Ul !== undefined) { if(Ul.firstElementChild.className !== "item-subscription") { var Data_Id = Article.parentElement.getAttribute("data-id"); 覆寫Ul(Ul,Data_Id) } } var Li_User = Article.firstElementChild.firstElementChild.firstElementChild.firstElementChild; if(Li_User !== undefined) { 覆寫樓層(Li_User); } } } else { var Div = $(record.addedNodes[0]).find("div.DiscussionListItem")[0]; if(Div !== undefined) { var Tags = Div.lastElementChild.children[2].lastElementChild.firstElementChild.firstElementChild.children; for(var Tag of Tags) { if(BlockList.indexOf(Tag.innerText.replace(/ /g,"")) !== -1) { Tag.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.setAttribute("style","display:none;"); } } var User = Div.lastElementChild.firstElementChild; if(BlackList.indexOf(User.href) !== -1) { User.parentElement.parentElement.parentElement.setAttribute("style","display:none;"); } } } },50); } }); }; var mo = new MutationObserver(callback); var option = { 'childList': true, 'subtree' : true }; mo.observe(document.getElementById("content"), option); $("document").ready( function() { 'use strict'; setTimeout(() => 新增腳本工具列按鈕(),100); // Your code here... } );