元素位于shadow-root(close)里面时,无法定位和提取元素内容怎么办?
参考以下操作过程即可捕获元素也可以参考链接Shadow-Root (Closed) 强制解密指南 - 飞书云文档
1.浏览器下载油猴插件(tampermonkey)
第一步点击篡改猴,点击管理面板,新建脚本
// ==UserScript==
// @name Shadow-root(closed)
// @namespace http://your-domain.com
// @version 1.2
// @description 强制解除shadow-root(closed)限制
// @match :///*
// @run-at document-start
// @grant unsafeWindow
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// ================== 核心破解逻辑 ==================
const hijackShadowDOM = () => {
// 劫持原生API const nativeAttachShadow = Element.prototype.attachShadow; Element.prototype.attachShadow = function(config) { // 强制修改closed模式为open if (config?.mode === 'closed') { console.debug('[破解] 检测到closed模式,已强制开启'); config.mode = 'open'; } return nativeAttachShadow.call(this, config); }; // 框架级破解(Vue/React) if (unsafeWindow.Vue) { const originalVueMount = unsafeWindow.Vue.prototype.$mount; unsafeWindow.Vue.prototype.$mount = function(el) { this.$el = el; return originalVueMount.call(this, el); }; console.debug('[破解] Vue组件Shadow DOM限制已解除'); }
};
// ================== 反检测机制 ==================
const antiDetection = () => {
// 屏蔽控制台特征 const originalConsoleLog = console.log; console.log = function(...args) { if (!args.some(str => str.includes('shadow'))) { originalConsoleLog.apply(console, args); } }; // 阻断异常检测 const originalAddEventListener = EventTarget.prototype.addEventListener; EventTarget.prototype.addEventListener = function(type, listener, options) { if (type === 'securitypolicyviolation') return; originalAddEventListener.call(this, type, listener, options); };
// ================== DOM监控模块 ==================
const startDOMObserver = () => {
const observer = new MutationObserver(mutations => { mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { if (node.nodeType === 1 && node.shadowRoot) { node.shadowRoot.innerHTML += '<style>:host { display: block !important; }</style>'; } }); }); }); observer.observe(document.documentElement, { childList: true, subtree: true, attributes: false, characterData: false });
// ================== 执行入口 ==================
hijackShadowDOM();
antiDetection();
startDOMObserver();
// 注入辅助样式
GM_addStyle(`
#shadow-root (closed) { display: block !important; border: 1px dashed red !important; }
`);
// 调试模式开关(生产环境注释掉)
// unsafeWindow.DEBUG_SHADOW = true; })();
参考以下操作过程即可捕获元素也可以参考链接Shadow-Root (Closed) 强制解密指南 - 飞书云文档
1.浏览器下载油猴插件(tampermonkey)
2.新增脚本并启动(代码见下文)
第一步点击篡改猴,点击管理面板,新建脚本
第二步:粘贴以下这段代码到新建脚本里面
// ==UserScript==
// @name Shadow-root(closed)
// @namespace http://your-domain.com
// @version 1.2
// @description 强制解除shadow-root(closed)限制
// @match :///*
// @run-at document-start
// @grant unsafeWindow
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// ================== 核心破解逻辑 ==================
const hijackShadowDOM = () => {
};
// ================== 反检测机制 ==================
const antiDetection = () => {
};
// ================== DOM监控模块 ==================
const startDOMObserver = () => {
};
// ================== 执行入口 ==================
hijackShadowDOM();
antiDetection();
startDOMObserver();
// 注入辅助样式
GM_addStyle(`
`);
// 调试模式开关(生产环境注释掉)
// unsafeWindow.DEBUG_SHADOW = true;
})();
第三步:点击保存脚本
第四步:检查这个按钮是否为开着的状态(下图为开着的状态)
3.重新打开链接即可捕获