Refactored Mike Adriano scraper. Changed logo and favicon. Added style methods to qu.
This commit is contained in:
17
src/utils/jsdom-url.js
Normal file
17
src/utils/jsdom-url.js
Normal file
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const { JSDOM } = require('jsdom');
|
||||
|
||||
const el = new JSDOM(`
|
||||
<span id="url" style="background-image: url(https://w.wallhaven.cc/full/dg/wallhaven-dg7y23.jpg);">
|
||||
<span id="urlQuotes" style="background-image: url('https://w.wallhaven.cc/full/dg/wallhaven-dg7y23.jpg');">
|
||||
<span id="color" style="color: rgb(255, 0, 0);">
|
||||
<span id="urlSpaces" style="background-image: url( https://w.wallhaven.cc/full/md/wallhaven-mdvmrm.jpg );">
|
||||
<span id="colorSpaces" style="color: rgb( 255, 0, 0 );">
|
||||
`).window.document;
|
||||
|
||||
console.log(el.querySelector('#url').style.backgroundImage);
|
||||
console.log(el.querySelector('#urlQuotes').style.backgroundImage);
|
||||
console.log(el.querySelector('#color').style.color);
|
||||
console.log(el.querySelector('#urlSpaces').style.backgroundImage);
|
||||
console.log(el.querySelector('#colorSpaces').style.color);
|
||||
@@ -4,9 +4,11 @@ const { JSDOM } = require('jsdom');
|
||||
const moment = require('moment');
|
||||
const http = require('./http');
|
||||
|
||||
const { window: globalWindow } = new JSDOM('');
|
||||
|
||||
function trim(str) {
|
||||
if (typeof str !== 'string') {
|
||||
return null;
|
||||
return str;
|
||||
}
|
||||
|
||||
return str.trim().replace(/\s+/g, ' ');
|
||||
@@ -55,9 +57,9 @@ function q(context, selector, attrArg, applyTrim = true) {
|
||||
if (attr) {
|
||||
const value = selector
|
||||
? context.querySelector(selector)?.[attr] || context.querySelector(selector)?.attributes[attr]?.value
|
||||
: context[attr] || context.attributes[attr]?.value;
|
||||
: context[attr] || context.getAttribute(attr);
|
||||
|
||||
return applyTrim && value ? trim(value) : value;
|
||||
return applyTrim && typeof value === 'string' ? trim(value) : value;
|
||||
}
|
||||
|
||||
return selector ? context.querySelector(selector) : context;
|
||||
@@ -77,6 +79,14 @@ function exists(context, selector) {
|
||||
return !!q(context, selector);
|
||||
}
|
||||
|
||||
function content(context, selector, applyTrim = true) {
|
||||
return q(context, selector, 'textContent', applyTrim);
|
||||
}
|
||||
|
||||
function contents(context, selector, applyTrim) {
|
||||
return all(context, selector, 'textContent', applyTrim);
|
||||
}
|
||||
|
||||
function html(context, selector) {
|
||||
const el = q(context, selector, null, true);
|
||||
|
||||
@@ -103,6 +113,33 @@ function text(context, selector, applyTrim = true) {
|
||||
return applyTrim ? trim(textValue) : textValue;
|
||||
}
|
||||
|
||||
function removeStyleFunctionSpaces(el) {
|
||||
// jsdom appears to have a bug where it ignores inline CSS attributes set to a function() containing spaces, e.g. url( image.png )
|
||||
el.setAttribute('style', el.getAttribute('style').replace(/\(\s+(.*)\s+\)/g, (match, cssArgs) => `(${cssArgs})`));
|
||||
}
|
||||
|
||||
function style(context, selector, styleAttr) {
|
||||
const el = q(context, selector);
|
||||
|
||||
if (el) {
|
||||
removeStyleFunctionSpaces(el);
|
||||
|
||||
return styleAttr ? el.style[styleAttr] : el.style;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function styles(context, selector, styleAttr) {
|
||||
const elStyles = Array.from(context.querySelectorAll(selector), (el) => {
|
||||
removeStyleFunctionSpaces(el);
|
||||
|
||||
return styleAttr ? el.style[styleAttr] : el.style;
|
||||
});
|
||||
|
||||
return elStyles;
|
||||
}
|
||||
|
||||
function number(context, selector, attr = true) {
|
||||
const value = q(context, selector, attr);
|
||||
|
||||
@@ -236,6 +273,10 @@ const legacyFuncs = {
|
||||
const quFuncs = {
|
||||
all,
|
||||
html,
|
||||
content,
|
||||
contents,
|
||||
cnt: content,
|
||||
cnts: contents,
|
||||
date,
|
||||
dur: duration,
|
||||
duration,
|
||||
@@ -250,6 +291,8 @@ const quFuncs = {
|
||||
num: number,
|
||||
poster,
|
||||
q,
|
||||
style,
|
||||
styles,
|
||||
text,
|
||||
texts,
|
||||
trailer: video,
|
||||
@@ -265,7 +308,7 @@ function init(element, window) {
|
||||
const legacyContextFuncs = Object.entries(legacyFuncs) // dynamically attach methods with context
|
||||
.reduce((acc, [key, func]) => ({
|
||||
...acc,
|
||||
[key]: (...args) => (window && args[0] instanceof window.HTMLElement // allow for different context
|
||||
[key]: (...args) => (args[0] instanceof globalWindow.HTMLElement // allow for different context
|
||||
? func(...args)
|
||||
: func(element, ...args)),
|
||||
}), {});
|
||||
@@ -273,7 +316,7 @@ function init(element, window) {
|
||||
const quContextFuncs = Object.entries(quFuncs) // dynamically attach methods with context
|
||||
.reduce((acc, [key, func]) => ({
|
||||
...acc,
|
||||
[key]: (...args) => (window && args[0] instanceof window.HTMLElement // allow for different context
|
||||
[key]: (...args) => (args[0] instanceof globalWindow.HTMLElement // allow for different context
|
||||
? func(...args)
|
||||
: func(element, ...args)),
|
||||
}), {});
|
||||
@@ -319,7 +362,7 @@ function extractAll(htmlValue, selector) {
|
||||
}
|
||||
|
||||
async function get(urlValue, selector, headers, options, queryAll = false) {
|
||||
const res = await http.get(urlValue, headers);
|
||||
const res = await http.get(urlValue, headers, options);
|
||||
|
||||
if (res.statusCode === 200) {
|
||||
const item = queryAll
|
||||
|
||||
Reference in New Issue
Block a user