Greasy Fork is available in English.
隐藏抖音上的登录弹窗和评论登录提示
当前为
// ==UserScript==
// @name 隐藏抖音登录弹窗
// @namespace 隐藏抖音登录弹窗
// @version 1.7.3
// @description 隐藏抖音上的登录弹窗和评论登录提示
// @author LR
// @license MIT
// @match https://www.douyin.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const selectors = [
'[id^="login-full-panel-"]',
'#related-video-card-login-guide',
'pace-island[id^="island_"]',
'[id^="pace-island"]',
'pace-island[id^=""]',
'[id^="island_"]',
'[class*="login-guide"]',
'[class*="login-prompt"]'
];
// 在DOM结构加载完成时尽早尝试隐藏弹窗
document.addEventListener('DOMContentLoaded', function() {
hideLoginPopup();
startMutationObserver();
addVideoToHistory();
});
// 使用 requestAnimationFrame 优化定期检查
function startChecking() {
function check() {
hideLoginPopup();
requestAnimationFrame(check);
}
requestAnimationFrame(check);
}
// 隐藏登录弹窗和相关提示的函数
function hideLoginPopup() {
selectors.forEach(selector => {
document.querySelectorAll(selector).forEach(element => {
if (isElementVisible(element)) {
element.style.display = 'none';
}
});
});
}
// 检查元素是否可见
function isElementVisible(element) {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
}
// 使用 MutationObserver 观察 DOM 变化
function startMutationObserver() {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes && mutation.addedNodes.length > 0) {
hideLoginPopup();
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
}
// 页面完全加载后再次隐藏弹窗,确保未被遗漏的弹窗被隐藏
window.addEventListener('load', function() {
hideLoginPopup();
});
// 提示欢迎消息
window.addEventListener('load', function() {
showGreetingsMessage();
});
// 弹出消息的函数
function showGreetingsMessage() {
const greetingsMessage = document.createElement('div');
greetingsMessage.textContent = '你好同学! - LR';
Object.assign(greetingsMessage.style, {
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
backgroundColor: 'rgba(255, 0, 0, 0.5)',
padding: '10px',
borderRadius: '5px',
color: '#fff',
zIndex: '9999',
});
document.body.appendChild(greetingsMessage);
setTimeout(() => greetingsMessage.remove(), 3500);
}
// 添加当前视频到浏览历史
function addVideoToHistory() {
const videoId = getCurrentVideoId();
if (videoId) {
const history = getCookie('video_history') ? JSON.parse(getCookie('video_history')) : [];
if (!history.includes(videoId)) {
history.push(videoId);
setCookie('video_history', JSON.stringify(history), 30);
}
}
}
// 获取当前视频ID的函数(需根据实际情况实现)
function getCurrentVideoId() {
// 根据实际的DOM结构获取视频ID
const videoElement = document.querySelector('video'); // 示例,具体选择器需调整
return videoElement ? videoElement.getAttribute('data-id') : null;
}
// 设置cookie
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
}
// 获取cookie
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
startChecking(); // 启动定期检查
})();