Tab evaluation and content scraping

1. Evaluate code in a tab

tab.eval() will execute code in the tab web page context.

let tab = await warp.tab("https://google.com"); tab.eval('alert("hello from Warp console")');

2. Evaluate code with return

Use return to return immutable data from tab.eval. This example counts divs in a tab.

let tab = await warp.tab("https://bing.com"); alert(await tab.eval("return document.querySelectorAll('div').length"));

3. Execute a function in tab context

Send JS functions directly to tab.eval. Eval will serialize the function and automatically return its value.

let tab = await warp.tab("https://nyt.com"); function countDivs() {  return document.querySelectorAll('div').length; } alert(await tab.eval(countDivs));

4. Scrape Google search results

Scrape Google search and show elements containing results in a Warp display.

function scrape() { let els = document.querySelectorAll('#main h3'); let res = []; els.forEach(function(el) { res.push({ el: el, caption: el.textContent, text: el.textContent, url: el.parentNode.getAttribute('href'), image: el.parentNode.querySelector('img')?.getAttribute('src') }); }); return res; } let tab = await warp.tab('https://google.com/search?q=bemjax'); let code = ` ${ scrape.toString() } return scrape(); `; let result = await tab.eval(code); tab.destroy(); let el = warp.dom.node('div'); result.forEach(function(data) { let d = warp.dom.node('div', { click: function() { warp.tab(data.url); } }, { class: 'item sm m5' }); d.innerHTML = ` ${data.caption} `; el.append(d); }); warp.display(el);

5. Google “testing” and open first result

Extract the first search result and open it in a new tab.

let tab = await warp.tab('https://google.com/search?q=testing'); let code = ` return document.querySelectorAll('#main h3')[0].parentNode.getAttribute('href'); `; let result =await tab.eval(code); tab.destroy(); if (result) { await warp.tab(result); } else { toast("No results found."); }

6. Open Gmail and toast last email sender

Scrape the Gmail inbox and display the sender of the latest email via toast.

let tab = await warp.tab("https://mail.google.com/mail/u/0/"); let code = ` let el = document.querySelectorAll('.zA')[0]; //this element hold the selected, read/notread, from(in text format ot email), title, time, first part of meil text let textAll = el.querySelector('.afn').textContent; textAll = textAll.split(",") //index 2 is "from" return { read : textAll[0], from : textAll[1], email : el.querySelector(".yP").getAttribute("email"), title : textAll[2], time : textAll[3], excerpt : textAll[4] } `; let result = await tab.eval(code); toast("The last email is from: " + result.email);