feat: visual selection (wip)

This commit is contained in:
karishmas6
2024-04-16 22:38:27 +05:30
parent 3fcf3d732f
commit 201b9fff6c

View File

@@ -1,10 +1,12 @@
import React, { useState } from 'react'; import React, { useState, useRef, useEffect } from 'react';
import axios from 'axios'; import axios from 'axios';
import Frame from 'react-frame-component';
function Scraper() { function Scraper() {
const [url, setUrl] = useState(''); const [url, setUrl] = useState('');
const [selections, setSelections] = useState([]); const [selections, setSelections] = useState([]);
const [data, setData] = useState(null); const [data, setData] = useState(null);
const iframeRef = useRef(null);
const handleUrlChange = (e) => { const handleUrlChange = (e) => {
setUrl(e.target.value); setUrl(e.target.value);
@@ -55,6 +57,22 @@ function Scraper() {
} }
}; };
// useEffect(() => {
// if (iframeRef.current && iframeRef.current.contentWindow) {
// const contentWindow = iframeRef.current.contentWindow;
// const contentDocument = contentWindow.document;
// contentDocument.body.onclick = handleSelectElement;
// }
// }, [url]);
const handleIframeLoad = () => {
if (iframeRef.current && iframeRef.current.contentDocument) {
const contentDocument = iframeRef.current.contentDocument;
contentDocument.body.onclick = handleSelectElement;
}
};
return ( return (
<div> <div>
<input <input
@@ -66,12 +84,10 @@ function Scraper() {
<button onClick={handleScrape}>Scrape</button> <button onClick={handleScrape}>Scrape</button>
<p>Click on the elements you want to scrape:</p> <p>Click on the elements you want to scrape:</p>
<iframe <iframe
ref={iframeRef}
src={url} src={url}
onLoad={(e) => {
const iframeDocument = e.target.contentDocument;
iframeDocument.body.onclick = handleSelectElement;
}}
style={{ width: '100%', height: '500px' }} style={{ width: '100%', height: '500px' }}
onLoad={handleIframeLoad}
/> />
<p>Selected Elements:</p> <p>Selected Elements:</p>
<ul> <ul>