大佬,抓取不到iframe中的某一元件,帮忙看下 #182
Unanswered
raymondhung010
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
抓取不到谷歌图书阅读器iframe中的其中一个元件, 当运行时开发者主控台会报错:
Uncaught DOMException: Failed to read a named property 'document' from 'Window': Blocked a frame with origin "https://play.google.com" from accessing a cross-origin frame.
at http://127.0.0.1:5173/src/main.ts:24:40
at watchElementLoad (http://127.0.0.1:5173/src/main.ts:4:5)
at http://127.0.0.1:5173/src/main.ts:20:1
我的代码如下
`
import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';
export default defineConfig({
plugins: [
monkey({
entry: 'src/main.ts',
userscript: {
icon: 'https://vitejs.dev/logo.svg',
namespace: 'npm/vite-plugin-monkey',
match: ['https://play.google.com/books/reader?id=*'],
grant: [
'GM_xmlhttpRequest',
],
},
}),
],
});
`
`const watchElementLoad = (selector: string, callback: () => void): void => {
const targetNode = document.querySelector(selector);
if (targetNode) {
callback();
return;
}
const observer = new MutationObserver((mutations: MutationRecord[], obs: MutationObserver) => {
const element = document.querySelector(selector);
if (element) {
obs.disconnect(); // Stop observing
callback();
}
});
};
watchElementLoad('iframe', () => {
console.log('iframe loaded');
const iframe = document.querySelector('iframe') as HTMLIFrameElement;
if (iframe.contentWindow) {
// find h1.title
const title = iframe.contentWindow.document.querySelector('h1.title');
}
});
`
Beta Was this translation helpful? Give feedback.
All reactions