为什么要写这样一个脚本
这个脚本能做什么
使用效果:参阅视频;
安装脚本
谈一谈
JavaScript 是什么
JavaScript ( JS ) 是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。虽然它是作为开发Web 页面的脚本语言而出名的,但是它也被用到了很多非浏览器环境中,例如 Node.js、 Apache CouchDB 和 Adobe Acrobat。JavaScript 是一种基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式和声明式(如函数式编程)风格。了解更多 JavaScript。 via MDN;
JavaScript 能干什么?
HTML 定义了网页的内容;
CSS 描述了网页的布局;
JavaScript 控制了网页的行为;
具体如下:
1.JavaScript 能够改变 HTML 内容;
2.JavaScript 能够改变 HTML 属性;
3.JavaScript 能够改变 HTML 样式 (CSS);
源码
// ==UserScript==
// @name Adblock4limbo
// @namespace https://greasyfork.org/zh-CN/users/893587-limbopro
// @version 0.0.1
// @license CC BY-NC-SA 4.0
// @description 优化PC端浏览未登录状态下浏览知乎体验;
// @author limbopro
// @match https://zhuanlan.zhihu.com/*
// @match https://www.zhihu.com/*
// @run-at document-end
// @grant none
// ==/UserScript==
var zhihu_id = "zhihux"; // 定义一个 CSS ID
var zhihuAds = "[class='Card AppBanner'],.Footer,.Banner-link,div.Pc-word {display:none ! important; pointer-events: none !important;}"; // 广告样式优先级覆盖
buttonClick("[class='Button Modal-closeButton Button--plain']", 10); // 模拟点击行为
buttonAppend("header[role='banner']", "清理中! ♻️", "undefined", "position:fixed; right:0px;", zhihu_id); // 在页面某个元素后面插入一个按钮
cssAdsRemove(zhihuAds, 100, "hloyx"); // 在 HTML HEAD 标签部分插入一个样式, 用以覆盖广告元素
rewriteToExplore(); // 若点击知乎首页按钮,则自动跳转到 Explore 页面 // 避免登录操作
window.onload = hrefAttributeSet(500, zhihu_id); // 将所有属性为 _blank 的 a 标签,替换为 _self;避免点击当前页面内的链接在新标签页打开;
window.onload = addListener("a[class*='css-'],button[class='Button ContentItem-action Button--plain Button--withIcon Button--withLabel']", () => { hrefAttributeSet(500, zhihu_id) }); // 为热门页面的分类添加监听器,触发 hrefAttributeSet 动作;
// 循环监控整个页面可滚动高度 scrollHeight 是否变化
var body_scrollHeightCheck = setInterval(() => {
var body_scrollHeight_then = document.body.scrollHeight;
setTimeout(() => {
var body_scrollHeight_now = document.body.scrollHeight;
if (body_scrollHeight_now > body_scrollHeight_then) {
hrefAttributeSet(500, zhihu_id);
}
}, 500);
}, 500);
// 循环监控答案页面可滚动高度 scrollHeight 是否变化
var comment_scrollHeightCheck = setInterval(() => {
let comment = document.querySelectorAll("div.CommentListV2"); // div.CommentListV2 是相应触发按钮选择器
if (comment.length > 0) {
var comment_scrollHeight_then = comment[0].scrollHeight;
setTimeout(() => {
var comment_scrollHeight_now = comment[0].scrollHeight;
if (comment_scrollHeight_now > comment_scrollHeight_then) {
hrefAttributeSet(500, zhihu_id);
}
}, 500)
}
}, 500)
// 循环模拟模拟点击
function buttonClick(selector, times) {
var initCount = 0;
var loop = setInterval(() => {
var ele = document.querySelectorAll(selector);
if (ele.length > 0) {
ele[0].click()
}
initCount += 1;
if (initCount == times) {
clearInterval(loop);
}
}, 0)
}
// 在页面动态插入按钮并赋予 onclick 属性
function buttonAppend(ele, text, onclick, position, id) {
var button = document.createElement("button");
button.innerHTML = text;
button.setAttribute("onclick", onclick);
button.setAttribute("id", id);
var button_style_values = position + "padding: 6px 6px 6px 6px; display: inline-block; " +
"font-size: 15px; color:white; z-index:114154; border-right: 6px solid #38a3fd !important; " +
"border-left: #292f33 !important; border-top: #292f33 !important; " +
"border-bottom: #292f33 !important; background: black; " +
"border-radius: 0px 0px 0px 0px; margin-bottom: 10px; " +
"font-weight: 800 !important; " +
"text-align: right !important;"
button.setAttribute("style", button_style_values);
var here = document.querySelectorAll(ele);
if (here.length > 0) {
here[0].insertBefore(button, here[0].childNodes[3])
//here[0].appendChild(button);
}
}
// 动态创建引用内部资源 内嵌式样式 内嵌式脚本
function cssAdsRemove(newstyle, delaytime, id) {
setTimeout(() => {
var creatcss = document.createElement("style");
creatcss.id = id;
creatcss.innerHTML = newstyle;
document.getElementsByTagName('head')[0].appendChild(creatcss)
}, delaytime);
}
// 知乎循环跳转绕过登录页
function rewriteToExplore() { // 跳转至热门话题 Explore 或 随机
var url = document.location.href;
var url_list = [
"https://www.zhihu.com/knowledge-plan/hot-question/hot/",
]
var rand = Math.floor(Math.random() * url_list.length);
var url_random = url_list[rand];
var reg = /^https:\/\/www.zhihu.com\/signin/gi;
if (url.search(reg) !== -1) {
window.location = url_random;
}
}
// 禁止新页面跳转另一种实现 循环
function hrefAttributeSet(time, id) {
document.getElementById(id).style.background = "black";
document.getElementById(id).innerHTML = "清理中! ♻️";
setTimeout(() => {
// 监控页面是否有新的 button
let selector = "button[class*='Button PaginationButton']";
let ele_button = document.querySelectorAll(selector);
if (ele_button.length > 0) {
window.onload = addListener(selector, () => { hrefAttributeSet(time, id) });
}
let times = 0;
let loop = setInterval(() => {
// 修改属性
times += 1;
let href = document.querySelectorAll("a");
let i;
for (i = 0; i < href.length; i++) {
if (href[i].target == "_blank") {
href[i].setAttribute("target", "_self");
}
}
let href_Length = document.querySelectorAll("a[target='_blank']").length;
if (href_Length === 0 && times >= 2) {
clearInterval(loop);
if (document.getElementById(id)) {
document.getElementById(id).innerHTML = "100%! ♻️";
document.getElementById(id).style.background = "green";
}
}
}, time)
}, time)
}
/* 添加监听器 */
function addListener(selector, funx) {
setTimeout(() => {
var ele = document.querySelectorAll(selector);
for (let index = 0; index < ele.length; index++) {
ele[index].addEventListener("click", funx, false)
}
}, 1000)
}
尝试优化
function rewriteToExplore() 优化;
// 知乎循环跳转绕过登录页
function rewriteToExplore() { // 跳转至热门话题 Explore 或 随机
var url = document.location.href;
var url_list = [
"https://www.zhihu.com/knowledge-plan/hot-question/hot/",
]
var rand = Math.floor(Math.random() * url_list.length);
var url_random = url_list[rand];
var reg = /^https:\/\/www.zhihu.com\/signin/gi;
if (url.search(reg) !== -1) {
window.location = url_random;
}
}
优化后:
function rewriteToExplore() {
let url = document.location.href;
let cssSelector = "a[href='//www.zhihu.com/'],a[href='//www.zhihu.com']";
let rewrite_url = "https://www.zhihu.com/knowledge-plan/hot-question/hot/0/hour";
let reg = /^https:\/\/www.zhihu.com\/signin/gi;
if (url.search(reg) !== -1) {
window.location = rewrite_url;
}
var ele = document.querySelectorAll(cssSelector)
if (ele.length > 0) {
let i;
for (i = 0; i < ele.length; i++) {
ele[i].href = rewrite_url;
}
}
}
版权属于:毒奶
联系我们:https://limbopro.com/6.html
毒奶搜索:https://limbopro.com/search.html
机场推荐:https://limbopro.com/865.html IEPL专线/100Gb/¥15/月起
毒奶导航:https://limbopro.com/daohang/index.html本文链接:https://limbopro.com/archives/21675.html
本文采用 CC BY-NC-SA 4.0 许可协议,转载或引用本文时请遵守许可协议,注明出处、不得用于商业用途!