Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Live view - frontend structure and styles

Vratislav Bartonicek63733f7dd558
diff --git a/src/puppeteer_live_view_client.js b/src/puppeteer_live_view_client.js
index cd9dacb1c..ef768589f 100644
--- a/src/puppeteer_live_view_client.js
+++ b/src/puppeteer_live_view_client.js
@@ -12,7 +12,13 @@ const DESTROY_FADEOUT_MILLIS = 2000;
* @param {string} url
* @returns {string} html
*/
-const createPage = (id, url) => `<div class="page" id="${id}">URL: ${url}</div>`;
+const createPage = (id, url) => `
+ <tr id="${id}">
+ <td class="status"></td>
+ <td class="url"><a class="url" href="${url}" target="m_blank">${url}</a></td>
+ <td class="more"><button>See more</button></td>
+ </tr>
+`;
/**
* Turns a Map of Pages into an Array of page divs.
@@ -28,7 +34,15 @@ const createPageCollection = (pages) => {
pages.forEach((page, id) => {
pageDivs.push(createPage(id, page.url()));
});
- return `<div class="page-collection">${pageDivs.join('\n')}</div>`;
+ return `
+ <table class="page-collection">${pageDivs.join('\n')}
+ <thead>
+ <th class="status">Status</th>
+ <th class="url">URL</th>
+ <th class="more">More</th>
+ </thead>
+ </table>
+ `;
};
/**
@@ -40,7 +54,7 @@ const createPageCollection = (pages) => {
* @param {Map<String,Page>} pages
* @returns {string} html
*/
-const createBrowser = (id, pages) => `<div class="browser" id="${id}"><h3>${id}</h3>${createPageCollection(pages)}</div>`;
+const createBrowser = (id, pages) => `<div class="browser" id="${id}"><h3>Browser: ${id}</h3>${createPageCollection(pages)}</div>`;
/**
* Turns a Set of Browsers into an Array of browser divs.
@@ -87,17 +101,35 @@ export const detailPage = ({ id, url, image, html }) => {
return `
<div id="${id}">
- <h3>Detail of Page: ${url}</h3>
- <div class="screenshot">
+ <h2>Page detail</h2>
+ <table class="page-detail">
+ <tbody>
+ <tr>
+ <td>URL:</td>
+ <td>${url}</td>
+ </tr>
+ </tbody>
+ </table>
+
+ <div id="tabmenu" class="tab-menu">
+ <ul>
+ <li><a class="tab-link" href="#" data-target="screenshot">Screenshot</a></li>
+ <li><a class="tab-link" href="#" data-target="code">HTML Code</a></li>
+ </ul>
+ </div>
+
+ <div id="screenshot" class="tabcontent">
<img src="data:image/png;base64, ${image.toString('base64')}" alt="Page screenshot" />
</div>
+
+ <div id="code" class="tabcontent">
<pre>
<code class="original-html">
${escapedHtml}
</code>
</pre>
+ </div>
</div>
-
`;
};
@@ -139,6 +171,7 @@ const wsHandler = (socket) => {
// Renders the Index Page - a list of browsers and their pages
renderIndex: ({ html }) => {
index.innerHTML = html;
+ index.insertAdjacentHTML('afterbegin', '<h2>Browser dashboard</h2>');
index.querySelectorAll('.page').forEach((page) => {
page.onclick = () => sendCommand('renderPage', {
id: page.getAttribute('id'),
@@ -151,6 +184,29 @@ const wsHandler = (socket) => {
backButton.classList.remove('hidden');
pageDetail.classList.remove('hidden');
pageDetail.innerHTML = html;
+
+ var tabMenuItems = document.querySelectorAll('.tab-menu .tab-link');
+ console.log(tabMenuItems)
+
+ for (i = 0; i < tabMenuItems.length; ++i) {
+ tabMenuItems[i].onclick = (event) => {
+ event.preventDefault();
+
+ event = event || window.event;
+ var target = event.target || event.srcElement,
+ text = target.textContent || text.innerText;
+
+ const contentId = target.getAttribute('data-target');
+
+ // Get all elements with class="tabcontent" and hide them
+ var tabcontent = document.getElementsByClassName("tabcontent");
+ for (var i = 0; i < tabcontent.length; i++) {
+ tabcontent[i].style.display = "none";
+ }
+
+ document.getElementById(contentId).style.display = "block";
+ }
+ };
},
// Adds a browser to the list
createBrowser: ({ id }) => {
@@ -169,18 +225,35 @@ const wsHandler = (socket) => {
const pages = document.getElementById(browserId).querySelector('.page-collection');
pages.insertAdjacentHTML('afterbegin', createPage(id, url));
const page = document.getElementById(id);
- page.onclick = () => sendCommand('renderPage', {
+
+ const button = page.querySelector('td.more button');
+ button.onclick = () => sendCommand('renderPage', {
id: page.getAttribute('id'),
});
+
+ var status = page.querySelector('td.status');
+ if(status) status.innerText = 'Starting'
},
// Updates page URL on navigation
updatePage: ({ id, url }) => {
const page = document.getElementById(id);
- page.innerText = `URL: ${url}`;
+
+ var spanUrl = page.querySelector('td.url');
+ if(spanUrl) spanUrl.innerText = `${url}`;
+
+ var status = page.querySelector('td.status');
+ if(status) status.innerText = 'Running';
},
// Removes a page from a browser after fade-out
destroyPage: ({ id }) => {
const page = document.getElementById(id);
+
+ var status = page.querySelector('td.status');
+ if(status) status.innerText = 'Finished';
+
+ var button = page.querySelector('td.more button');
+ button.remove();
+
page.classList.add('destroyed');
// Do not remove immediately since it can happen pretty fast
// and the page only pops in the list for a split second.
@@ -209,7 +282,7 @@ const wsHandler = (socket) => {
};
socket.onclose = () => {
- index.insertAdjacentHTML('beforebegin', '<div>Act finished</div>');
+ index.insertAdjacentHTML('beforeend', '<div>Actor finished</div>');
};
socket.onerror = (err) => {
@@ -266,13 +339,115 @@ export const layout = (url) => {
.original-html {
white-space: pre-wrap;
}
+ body {
+ margin: 0;
+ padding: 0;
+ font-family: 'Graphik';
+ }
+ header {
+ width: 100%;
+ padding: 5px 10px;
+ background: #1a346d;
+ color: white;
+ }
+ h1 {
+ margin: 10px 0;
+ font-size: 28px;
+ font-weight: 400;
+ }
+ h2 {
+ font-size: 22px;
+ font-weight: 500;
+ }
+ h3 {
+ font-size: 15px;
+ font-weight: 500;
+ }
+ main {
+ padding: 5px 10px;
+ }
+ table {
+ border-collapse: collapse;
+ }
+ table, th, td {
+ border: 1px solid #DDD;
+ }
+ thead {
+ background: #F8F9F9;
+ }
+ th,td {
+ padding: 8px;
+ }
+ th {
+ font-size: 16px;
+ font-weight: 500;
+ color: #9FA5A9;
+ }
+ th.status {
+ min-width: 100px;
+ }
+ th.url {
+ min-width: 400px;
+ }
+ th.more {
+ min-width: 80px;
+ }
+ tbody {
+
+ }
+ tr {
+
+ }
+ td {
+
+ }
+ td.status {
+ color: #69C242;
+ min-width: 100px;
+ }
+ td.url {
+ color: #00a6d0;
+ min-width: 400px;
+ }
+ td.more {
+ min-width: 80px;
+ }
+ table.page-detail td {
+ border: none;
+ }
+ .tab-menu {
+ margin: 30px 0 10px;
+ }
+ .tab-menu ul {
+ list-style-type: none;height: 29px;
+ border-bottom: 1px solid #ccc;
+ }
+ .tab-menu li {
+ float: left;
+ border: 1px solid #ccc;
+ margin-right: 5px;
+ }
+ .tabmenu li.active {
+ border-bottom-color: #ccc;
+ }
+ .tab-link {
+ padding: 5px;
+ display: block;
+ }
+ .tabcontent {
+ display: none;
+ }
</style>
</head>
<body>
- <h1>Puppeteer Live View</h1>
- <button id="back-button" class="hidden">Back to Index</button>
- <div id="index">Waiting for WebSocket connection.</div>
- <div id="page-detail" class="hidden"></div>
+ <header>
+ <h1>Puppeteer Live View</h1>
+ </header>
+ <main>
+ <button id="back-button" class="hidden">Back to Index</button>
+ <div id="index">Waiting for WebSocket connection.</div>
+ <div id="page-detail" class="hidden"></div>
+ </main>
<script>
const ws = new WebSocket("${url.replace('https', 'wss').replace('http', 'ws')}");
const DESTROY_FADEOUT_MILLIS = ${DESTROY_FADEOUT_MILLIS};