-
-
Save bestK/0f8be12bd9d341883c3bd72b8e03b5d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name github tending markdown remove | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author bestK | |
// @match https://github.com/trending/* | |
// @icon https://www.google.com/s2/favicons?domain=github.com | |
// @grant GM_xmlhttpRequest | |
// @run-at document-idle | |
// ==/UserScript== | |
var allRows; | |
(function() { | |
'use strict'; | |
allRows = document.getElementsByClassName('Box-row'); | |
clear() | |
})(); | |
function clear(){ | |
var promises = []; | |
for(var i = 0; i<allRows.length;i++){ | |
var url = allRows[i].children[1].children[0].href | |
url = 'https://api.github.com/repos/'+url.split('https://github.com/')[1]+'/contents' | |
promises.push(getDirs(url,i)); | |
} | |
Promise.all(promises).then(rows => { | |
removeRows(rows) | |
}) | |
} | |
function getDirs(url,currentRowIdx) { | |
return new Promise((resolve, reject) => { | |
var limit = 0; | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: url, | |
onload: function(res) { | |
if (res.status == 200) { | |
var text = res.responseText; | |
var contents = JSON.parse(text); | |
for(var j=0;j<contents.length;j++){ | |
if(isChinese(contents[j].path) || contents[j].path.split('.')[1]==='md'){ | |
resolve(allRows[currentRowIdx]) | |
} | |
} | |
} else { | |
reject(false); | |
} | |
} | |
}); | |
}) | |
} | |
// 简单匹配要删除的行 | |
function getRows(){ | |
var filter = ['Markdown'] | |
for(var i = 0; i<allRows.length;i++){ | |
var language = allRows[i].children[allRows[i].children.length-1].children[0].children[1].innerHTML; | |
console.log(language) | |
if(filter.includes(language)){ | |
removeRows.push(allRows[i]) | |
} | |
} | |
return removeRows; | |
} | |
// 执行删除动作 | |
function removeRows(_rows){ | |
console.log('以下内容已从页面移除',_rows) | |
for(var i=0;i<_rows.length;i++){ | |
var self = _rows[i] | |
var parent = self.parentElement | |
parent.removeChild(self) | |
} | |
} | |
// 是否中文 | |
function isChinese(text){ | |
return /.*[\u4e00-\u9fa5]+.*$/.test(text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment