PikPak 新标签页打开

PikePak 为导航和文件夹添加一个新标签页打开按钮

当前为 2025-07-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         PikPak 新标签页打开
// @namespace    https://gf.qytechs.cn/zh-CN/users/722555-vveishu
// @version      1.3.3
// @description  PikePak 为导航和文件夹添加一个新标签页打开按钮
// @author       vvei
// @match        https://mypikpak.com/s/*
// @icon         http://mypikpak.com/favicon.ico
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
	'use strict';

	// 确保Material Symbols Outlined样式表被加载
	const link = document.createElement('link');
	link.rel = 'stylesheet';
	link.href = 'https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0&icon_names=open_in_new';
	document.head.appendChild(link);

	// 添加 css
	const style = document.createElement('style');
	const css = `
    .grid-operation{
        display:block!important;
        .pp-link-button{
            background-color: #fff7!important;
            .pp-icon{
                color: #000!important;
            }
        }
    }
    .pp-link-button.moveL2D7R.moveL2D7R.moveL2D7R.moveL2D7R {
        top: 40px;
    }
    .folder-navigator .open_in_new{
        font-size: 18px;
        height: 36px;
        min-width: 1em;
        margin-right: 8px;
        align-items: center;
        line-height: 36px;
        text-decoration: none;
    }
    .file-list > .grid .open_in_new {
        position: absolute;
        min-width: 24px;
        height: 24px;
        top: 8px;
        right: 8px;
    }
    `;
	style.textContent = css;
	document.head.appendChild(style);

	// 执行时间
	// 设置延迟1.5秒执行
	setTimeout(() => {
		// 初始处理一次
		processNav();
		processLis();

		// 监听 nav
		const navListObserver = new MutationObserver((mutationsList) => {
			mutationsList.forEach((mutation) => {
				if (mutation.type === 'childList') { processNav(); }
			});
		});
		navListObserver.observe(document.querySelector('.folder-navigator > ul'), { childList: true });

		// file-list 每1秒检查一次,持续检查3秒
		let checkCount = 0;
		const maxChecks = 3; // 3次检查
		const intervalId = setInterval(() => {
			processLis();
			checkCount++;
			if (checkCount >= maxChecks) {
				clearInterval(intervalId);
				// 3次检查完成后开始监听 file-list
				const observer = new MutationObserver((mutationsList) => {
					mutationsList.forEach((mutation) => {
						mutation.addedNodes.forEach((node) => {
							if (node.classList && node.classList.contains('grid')) {processLis();}
						});
					});
				});
				observer.observe(document.querySelector('.file-list'), { childList: true });
			}
		}, 1000); // 间隔1秒
	}, 1500); // 延迟1.5秒
})();

// 处理 .folder-navigator > ul 内所有 .pp-link-button 元素
function processNav() {
	const navList = document.querySelector('.folder-navigator > ul');
	const navAs = navList.querySelectorAll('.pp-link-button');
	navAs.forEach((item) => {
		// 判断 item 有链接
		if (item.href && item.href !== '') {
			// 判断下一个兄弟元素是否没有 .open_in_new 类
			if (!item.nextElementSibling || !item.nextElementSibling.classList.contains('open_in_new')) {
				navAddLink(item);
			}
		} else {
			// 无链接,移除 .open_in_new 类
			if (item.nextElementSibling && item.nextElementSibling.classList.contains('open_in_new')) {
				item.nextElementSibling.remove();
			}
		}
	});
}

function navAddLink(item) {
	// 创建一个新的a元素
	const newLink = document.createElement('a');
	newLink.href = item.href;
	newLink.target = '_blank';
	newLink.className = 'open_in_new material-symbols-outlined';
	newLink.textContent = 'open_in_new';

	// 将新创建的a元素插入到原始a元素之后
	item.parentNode.insertBefore(newLink, item.nextSibling);
}

// 处理所有li元素
function processLis() {
	const fileList = document.querySelector('.file-list');
	const listItems = fileList.querySelectorAll('.grid');
	listItems.forEach((item) => {
		// 判断 .folder-cover 存在
		if (item.querySelector('.folder-cover')) {
			// .pp-link-button 通过 css 下移
			const cppLink = item.querySelector('.pp-link-button');
			if (!cppLink.classList.contains('moveL2D7R')) { cppLink.classList.add('moveL2D7R'); }

			const gridOperation = item.querySelector('.grid-operation');
			// 获取 .grid-operation 的第一级子元素
			const childElements = gridOperation.children;
			if (!gridOperation.querySelector(':scope > .open_in_new')) { listAddLink(item, gridOperation); }
		}
	});
}

function listAddLink(item, gridOperation) {
	const newLink = document.createElement('a');
	newLink.href = item.id;
	newLink.target = '_blank';
	newLink.className = 'open_in_new';

	const span = document.createElement('span');
	span.className = 'material-symbols-outlined';
	span.textContent = 'open_in_new';
	newLink.appendChild(span);

	gridOperation.appendChild(newLink);
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址