Extension User Interface
We have already built the popup that controls our extension. Now we need the other side of that interface: the code that runs directly on the target website. This is the job of ui.js.
The purpose of ui.js is to provide the tools we need while configuring a scraper. It creates and manages scraper state, lets us define listing and pagination selectors, allows us to visually select elements from the page, previews extracted data, and handles saving and loading scraper configurations.
The file is also responsible for receiving the messages sent by menu.js. The popup tells the page what the user wants to do, and ui.js calls the appropriate function.
Receiving Commands
var highlighted = null;
chrome.runtime.onMessage.addListener((message) => {
if (message.action == "create") createScraper();
else if (message.action == "remove") removeScraper();
else if (message.action == "start") startScraper();
else if (message.action == "stop") stopScraper();
else if (message.action == "download") downloadData();
else if (message.action == "clear") clearData();
else if (message.action == "links") setListings();
else if (message.action == "pagination") setPagination();
else if (message.action == "select") selectElements();
else if (message.action == "deselect") removeElements();
else if (message.action == "done") stopSelecting();
else if (message.action == "preview") previewData();
else if (message.action == "save") saveScraper();
else loadScraper(message.action);
});
The message listener is the entry point for the page UI. Whenever menu.js sends a message, this listener examines its action property and calls the corresponding function.
For example, the Start Scraper button sends the action start, which calls startScraper(). The Select Element button sends select, which calls selectElements().
The final else is used for loading a saved scraper. In that case the message itself contains the JSON representation of the scraper rather than one of the predefined action names.
Creating a Scraper
function createScraper() {
if (scraperIsRunning()) {
alert("Cannot create scraper while scraper is running");
return;
} else if (!scraperExists()) {
setScraper({
"running": false,
"cardUrlSelector": "",
"nextPageSelector": "",
"currentPage": "",
"selectors": {},
"listingUrls": [],
"listingUrlIndex": -1,
"data": []
}); alert("Scraper has been created");
} else alert("Scraper already exists");
}
createScraper() initializes a new scraper configuration. Before doing anything, it checks whether another scraper is currently running. Creating a new configuration while the scraper is running could destroy its state, so the operation is refused.
If no scraper exists, the function creates the state structure we introduced earlier. The selectors are initially empty, the listing URL array contains no URLs, and listingUrlIndex starts at -1.
Removing a Scraper
function removeScraper() {
if (scraperIsRunning()) {
alert("Cannot remove scraper while scraper is running");
return;
} else if (scraperExists()) {
localStorage.removeItem("scraper");
alert("Scraper has been removed");
} else alert("Scraper doesn't exist");
}
removeScraper() does the opposite. It removes the scraper state from local storage, but only when the scraper is stopped. This protects the running scraper from having its configuration deleted accidentally.
Starting and Stopping
function startScraper() {
if (!scraperExists()) alert("Scraper doesn't exist");
else if (scraperIsRunning()) alert("Scraper is already running");
else {
let scraper = getScraper();
scraper.running = true;
setScraper(scraper);
alert("Scraper has been started");
location.reload();
}
}
startScraper() checks that a scraper exists and is not already running. It then changes the running flag to true, saves the updated state, and reloads the page.
The reload is important because scraper.js will inspect the stored state when the page loads. Once it sees that running is true, it can begin the scraping process.
function stopScraper() {
if (!scraperExists()) alert("Scraper doesn't exist");
else if (!scraperIsRunning()) alert("Scraper has already been stopped");
else {
let scraper = getScraper();
scraper.running = false;
setScraper(scraper);
alert("Scraper has been stopped");
}
}
stopScraper() simply changes the same flag back to false. The scraper can then see that it should no longer continue processing pages.
Downloading Data
function downloadData() {
if (scraperIsRunning()) {
alert("Cannot download data while scraper is running");
return;
} else if (scraperExists()) {
let data = getScraper().data;
const blob = new Blob(
[JSON.stringify(data, null, 2)],
{ type: "text/json;charset=utf-8" }
); const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "listing-" + Date().toString().split(" ").slice(0, 5).join("-") + ".json";
link.click();
URL.revokeObjectURL(url);
}
}
downloadData() turns the collected data into a downloadable JSON file. The data is converted to a JSON string and placed inside a Blob, which allows the browser to treat it as a file.
A temporary object URL is then created for the blob. We create an invisible download link, assign the URL to it, and programmatically click the link. Finally, URL.revokeObjectURL() releases the temporary URL.
Clearing Data
function clearData() {
if (scraperIsRunning()) {
alert("Cannot clear data while scraper is running");
return;
} else if (scraperExists()) {
let scraper = getScraper();
scraper.data = [];
setScraper(scraper);
alert("Scraper data has been cleared");
}
}
clearData() removes only the collected results. The scraper configuration remains intact, so selectors and other settings can be reused for another run.
Setting Listing URLs
function setListings() {
if (scraperIsRunning()) {
alert("Cannot set listings while scraper is running");
return;
} else if (scraperExists()) {
stopSelecting();
let scraper = getScraper();
let cardUrlSelector = "";
while (cardUrlSelector == "")
try {
cardUrlSelector = prompt(
"Paste card URL selector:\n\n" +
"1. Open DevTools\n" +
"2. Rightclick listing card\n" +
"3. Find <a href=\"path/to/listing.html\">\n" +
"4. Rightclick element, select Copy -> Copy selector\n" +
"5. Paste selector below\n\n" +
"e.g. body > div > div:nth-child(1) > span:nth-child(2) > a"
).replace(/:nth-child\(\d+\)/g, "");
} catch(e) { return; }
let listingUrls = extractListingUrls(cardUrlSelector);
if (confirm("Do you confirm extracted links?\n\n" + listingUrls)) {
scraper.cardUrlSelector = cardUrlSelector;
setScraper(scraper);
}
} else alert("Scraper doesn't exist");
}
setListings() defines how the scraper finds individual listing URLs on a listing page. For this version, the user obtains a CSS selector using Chrome DevTools and pastes it into the prompt.
The selector is passed to extractListingUrls(), which tests it against the current page and returns the URLs it finds. The user gets a chance to inspect the result before the selector is stored in cardUrlSelector.
The :nth-child() portions are removed from the selector because they often make selectors unnecessarily specific. We will later build a more convenient visual selector mechanism that removes the need for this manual DevTools workflow.
Setting Pagination
function setPagination() {
if (scraperIsRunning()) {
alert("Cannot set pagination while scraper is running");
return;
} else if (scraperExists()) {
stopSelecting();
let scraper = getScraper();
let nextPageSelector = "";
while (nextPageSelector == "") nextPageSelector = prompt(
"Paste next page selector:\n\n" +
"1. Open DevTools\n" +
"2. Rightclick next page button" +
"3. Find <a href=\"path/to/next_page.html\"> or <button>Next</button>\n" +
"4. Rightclick element, select Copy -> Copy selector\n" +
"5. Paste selector below\n\n" +
"e.g. body > div > div:nth-child(2) > div.col-md-8 > nav > ul > li > a"
);
let nextPage = extractNextPage(nextPageSelector);
if (confirm("Do you confirm next page?\n\n" + nextPage)) {
scraper.nextPageSelector = nextPageSelector;
setScraper(scraper);
}
} else alert("Scraper doesn't exist");
}
setPagination() works in almost exactly the same way, except that it defines the selector used to find the next listing page. The selector is tested with extractNextPage(), and once confirmed it is saved as nextPageSelector.
Together, cardUrlSelector and nextPageSelector describe the navigation structure of the listing website we discussed earlier: one selector discovers the listings, while the other tells us how to move through the listing pages.
Starting Element Selection
function selectElements() {
if (scraperIsRunning()) {
alert("Cannot select elements while scraper is running");
return;
} else if (scraperExists()) {
stopSelecting();
document.addEventListener("mouseover", mouseover, true);
document.addEventListener("click", clickAddSelector, true);
alert("Select elements you want to extract text from");
} else alert("Scraper doesn't exist");
}
selectElements() enables the visual element-selection mode. It first removes any existing selection handlers and then attaches two event listeners to the document.
The mouseover listener highlights elements as the mouse moves over them. The click listener records an element when the user clicks it.
Removing Selected Elements
function removeElements() {
if (scraperIsRunning()) {
alert("Cannot remove elements while scraper is running");
return;
} else if (scraperExists()) {
alert("Remove elements you don't need");
stopSelecting();
document.addEventListener("mouseover", mouseover, true);
document.addEventListener("click", clickRemoveSelector, true);
} else alert("Scraper doesn't exist");
}
removeElements() uses the same visual interface but switches the click handler. Instead of adding a selector, clicking a previously selected element removes its selector from the scraper configuration.
Previewing Data
function previewData() {
if (scraperIsRunning()) {
alert("Cannot preview data while scraper is running");
return;
} else if (scraperExists()) {
try {
let scraper = getScraper();
let message = "";
let listingUrls = extractListingUrls(scraper.cardUrlSelector);
let nextPage = extractNextPage(scraper.nextPageSelector);
let data = JSON.stringify(extractData(), null, 4);
if (listingUrls != "") message += "Listing URLs:\n\n" + listingUrls + "\n\n";
if (nextPage != "" && nextPage != null) message += "Next Page:\n\n" + nextPage + "\n\n";
if (data != "{}") message += "Extracted data:\n\n" + data;
alert(message);
} catch(e) { alert("Nothing has been selected"); }
} else alert("Scraper doesn't exist");
}
previewData() is useful while configuring a scraper because it lets us test the current configuration without starting the entire crawling process. It checks the listing selector, pagination selector, and data selectors against the current page and displays the results.
This is especially useful for debugging selectors. We can configure the scraper, preview what it would extract, adjust the selectors if necessary, and only then start the actual crawl.
Loading a Scraper
function loadScraper(scraper) {
if (scraperIsRunning()) {
alert("Cannot load scraper while scraper is running");
return;
} else {
try {
let newScraper = JSON.parse(scraper);
setScraper(newScraper);
alert("Scraper has been loaded");
} catch(e) { alert("Failed loading scraper"); }
}
}
loadScraper() receives a JSON string, converts it back into a JavaScript object with JSON.parse(), and stores it as the current scraper configuration.
This means a scraper configuration can be saved to a file and later loaded again. We do not have to configure all of the selectors from scratch every time we want to use the scraper.
Saving a Scraper
function saveScraper() {
if (scraperIsRunning()) {
alert("Cannot download data while scraper is running");
return;
} else if (scraperExists()) {
let data = getScraper();
const blob = new Blob(
[JSON.stringify(data, null, 2)],
{ type: "text/json;charset=utf-8" }
); const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "scraper.json";
link.click();
URL.revokeObjectURL(url);
} else alert("Scraper doesn't exist");
}
saveScraper() is the reverse of loading. It takes the complete scraper object from local storage, converts it to formatted JSON, and downloads it as scraper.json.
Notice the difference between saving the scraper and downloading the data. The scraper file contains the configuration and state, while the downloaded data contains the listings that have actually been collected.
Stopping Selection Mode
function stopSelecting() {
if (scraperIsRunning()) {
alert("Cannot stop selecting while scraper is running");
return;
} else if (scraperExists()) {
document.removeEventListener("mouseover", mouseover, true);
document.removeEventListener("click", clickAddSelector, true);
document.removeEventListener("click", clickRemoveSelector, true);
} else alert("Scraper doesn't exist");
}
stopSelecting() disables the visual selection interface by removing the event listeners that were added by selectElements() and removeElements().
Calling this before starting another selection mode is important because otherwise multiple click handlers could remain active at the same time.
Highlighting Elements
function mouseover(e) {
if (highlighted) {
if (highlighted.style.outline == "green solid 2px") {
highlighted = null;
return;
} highlighted.style.outline = "";
}
if (e.target.style.outline != "green solid 2px") {
highlighted = e.target;
highlighted.style.outline = "2px solid red";
}
}
The mouseover() function provides the visual feedback that makes element selection possible. The global highlighted variable remembers the element currently under the mouse.
When the mouse moves to another element, the previous temporary highlight is removed and the new element receives a red outline. Elements that have already been selected receive a green outline and are deliberately left alone.
Selecting an Element
function clickAddSelector(e) {
e.preventDefault();
e.stopPropagation();
let scraper = getScraper();
let selector = getCssSelector(e.target);
if (e.target.style.outline == "green solid 2px") {
alert("You have already selected this element");
return;
}
e.target.style.outline = "2px solid green";
let name = prompt("How would you call this selector?");
try {
while (name == "" || scraper.selectors[name] != undefined) {
alert(`Invalid name "${name}"`);
name = prompt("How would you call this selector?");
}
} catch(e) {}
scraper.selectors[name] = selector;
setScraper(scraper);
}
When an element is clicked, clickAddSelector() prevents the normal browser click behavior and stops the event from propagating to other elements.
It then calls getCssSelector() to generate a CSS selector describing the clicked element. The element is given a green outline to show that it has been selected, and the user is asked to give the selector a name such as title, price, or address.
The name must be unique because it becomes a key in the scraper's selectors object. Finally, the selector is stored in local storage as part of the scraper configuration.
Removing an Element
function clickRemoveSelector(e) {
if (e.target.style.outline == "green solid 2px") {
let scraper = getScraper();
let selector = getCssSelector(e.target);
for (let key of Object.keys(scraper.selectors)) {
if (scraper.selectors[key] == selector) {
delete scraper.selectors[key];
alert(`Selector "${key}" has been removed`);
}
} e.target.style.outline = "";
setScraper(scraper);
} else { alert("There is no such selector"); }
}
clickRemoveSelector() performs the reverse operation. It checks whether the clicked element has a green selection outline, generates its CSS selector, and searches through the configured selectors for a matching value.
Once it finds the matching selector, the corresponding key is deleted from the scraper configuration and the green outline is removed from the element.
The Complete Selection Workflow
These functions work together to create the visual selector workflow. The user clicks Select Element, moves the mouse over the page to see elements highlighted, and clicks the elements whose text should be extracted. Each selected element is assigned a name and its CSS selector is stored in the scraper configuration.
If a selection is no longer needed, Remove Element switches the click behavior so that selected elements can be removed. Done Selecting then removes the event listeners and returns the page to its normal state.
This is where our project starts becoming more than a hard-coded scraper. Instead of editing JavaScript whenever we want to scrape a new website, the extension can now store the website-specific configuration separately from the scraping code. The same scraper can therefore be configured for different websites.
There are still several helper functions used by ui.js, such as getScraper(), setScraper(), scraperExists(), scraperIsRunning(), extractListingUrls(), extractNextPage(), extractData(), and getCssSelector(). These will be implemented as part of the remaining scraper functionality.
For now, the important architecture is clear: menu.js sends commands, ui.js turns those commands into actions on the target page, and the scraper configuration is persisted in local storage. The next step is to look at the helper functions that make this interaction possible.