UNPKG

contract-scraper

Version:

A customisable data scraper for the web based on JSON contracts

31 lines (30 loc) 890 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/; const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/; function isAbsolute(url) { if (WINDOWS_PATH_REGEX.test(url)) { return false; } return ABSOLUTE_URL_REGEX.test(url); } exports.default = (inputValue, rootUrl) => { const isEmpty = (style) => { return style === undefined || style === null || style.trim().length === 0; }; if (isEmpty(inputValue)) { return null; } try { const base = new URL(rootUrl); const image = /(background-image:\s?url\((.*)?)\)/.exec(inputValue); const match = image[2].replace(/'|"/g, ''); if (!isAbsolute(match)) { return new URL(match, base.origin).href; } return match; } catch (e) { return null; } };