Skip to content

Instantly share code, notes, and snippets.

View defx's full-sized avatar

Matt Donkin defx

  • Hitchin, Hertfordshire.
  • 09:32 (UTC +01:00)
View GitHub Profile
@defx
defx / puppeteerScrape.js
Created October 12, 2022 12:53
Recursive site scraper using Puppeteer
import puppeteer from "puppeteer-core"
const unique = (arr) => [...new Set(arr)]
(async () => {
const browser = await puppeteer.launch({
headless: true,
channel: "chrome",
timeout: 60000,
})
@defx
defx / prefix-selectors.js
Created December 12, 2020 11:58
Add a prefix to all selectors in a CSS string
function prefixSelectors(prefix, css) {
let insideBlock = false;
let look = true;
let output = '';
for (let char of css) {
if (char === '}') {
insideBlock = false;
look = true;
} else if (char === ',') {
@defx
defx / faux-slotted-content.js
Last active December 12, 2020 16:54
Manual slot resolution without Shadow DOM (not recommended, more of a hypothetical in the absence of declarative Shadow DOM)
const childNodes = (node) => {
let frag = document.createDocumentFragment();
while (node.firstChild) {
frag.appendChild(node.firstChild);
}
return frag;
};
export default function merge(targetNode, sourceNode) {
let namedSlots = sourceNode.querySelectorAll('slot[name]');
@defx
defx / app.js
Last active January 21, 2020 12:01
Preact compatibility with customised built-in elements
import { h, Component } from "preact";
import { Router } from "preact-router";
import Header from "./header";
// Code-splitting is automated for routes
import Home from "../routes/home";
import Profile from "../routes/profile";
export default class App extends Component {
const idTag = (strings, ...values) =>
strings.reduce((a, s, i) => a + s + (values[i] || ""), "");
@defx
defx / inview.js
Created March 25, 2015 14:20
Protractor utility function for testing whether an absolutely positioned element is "in view" or not.
function inView(el, fn) {
var location, size;
el.getCssValue('display')
.then(function (display) {
if (display === 'none') {
fn(false);
} else {
return el.getSize()
@defx
defx / gist:f91089bdf0f2aad8fbcb
Last active August 29, 2015 14:01
John Resig's Regular Expression to Parse HTML Tags and Attributes (with added support for hyphened attributes)
/^<(\w+)((?:\s+(\w|-)+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/