Skip to content

Instantly share code, notes, and snippets.

@dwsmart
dwsmart / test.js
Last active December 6, 2019 15:52
Puppeteer Shadow Dom > Dom
const puppeteer = require('puppeteer');
const url = process.env.URL || 'https://lit-element.polymer-project.org/';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.evaluate(() => {
// loop through elements and copy shadow to inner
@dwsmart
dwsmart / add_string_to_href.js
Last active January 13, 2020 11:43
Add a string to the start of all internal links on a page.
//get base url
const getUrl = window.location;
const baseUrl = getUrl .protocol + "//" + getUrl.host + "/";
// the string you want to append
const stringToAdd = "test/"
// get all a tags
let elements = document.getElementsByTagName('a');
for (let node of elements) {
// is this internal to the site? If so update the link
if(node.href.startsWith(baseUrl)){
@dwsmart
dwsmart / codeCover.js
Created January 22, 2020 16:44
Code Coverage in Puppetter
// npm install puppeteer
const puppeteer = require('puppeteer');
/* variables to allow for user-agent / screensize choice
example use: URL='https://tamethebots.com' useragent='Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' viewportw=412 viewporth=732 node codeCover.js
*/
const url = process.env.URL || 'https://www.example.com';
const useragent = process.env.useragent || 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
const viewportw = process.env.viewportw || 1920;
const viewporth = process.env.viewporth || 1696;
@dwsmart
dwsmart / shift.js
Last active January 23, 2020 17:08
Cumulative Shift Example
const puppeteer = require('puppeteer');
/* variables to allow for user-agent / device.
example use: URL='https://tamethebots.com' useragent='Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' viewportw=412 viewporth=732 node shift.js
*/
const url = process.env.URL || 'https://tamethebots.com/';
const useragent = process.env.useragent || 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
const viewportw = process.env.viewportw || 412;
const viewporth = process.env.viewporth || 732;
const threeGee = {
'offline': false,
//Run this in the console, will cooy the shadow DOM to the light. Will only work for open ones, and will not recurse nested shadow DOMs
for (let elMnt of document.getElementsByTagName('*')) {
if (elMnt.shadowRoot) elMnt.innerHTML = elMnt.shadowRoot.innerHTML;
}
//Run this in the console, will cooy the shadow DOM to the light. Will only work for open ones, and will not recurse nested shadow DOMs
for (let elMnt of document.getElementsByTagName('*')) {
if (elMnt.shadowRoot) elMnt.innerHTML = elMnt.shadowRoot.innerHTML;
}
//Run this in the console, will cooy the shadow DOM to the light. Will only work for open ones, and will not recurse nested shadow DOMs
for (let elMnt of document.getElementsByTagName('*')) {
if (elMnt.shadowRoot) elMnt.innerHTML = elMnt.shadowRoot.innerHTML;
}
/*
usage:
node usedcss url=<page_to_test> filename=<required_file_name> viewport=<widthxheight> ua='<my user agent>'
filename, viewport & ua optional,
Example:
node usedcss url=https://tamethebots.com filename=ttb_used.css viewport=360x640 ua='css coverage script'
Updated to print original stylesheet URL
*/
const currURL = `${window.location.protocol}//${window.location.host}${window.location.pathname}${window.location.search}`
let outputString = ''
for (var idx in document.links) {
const ancString = `${document.links[idx]}`
if (ancString.includes('#')) {
const anchParts = ancString.split('#')
if (anchParts[0] === currURL) {
if (!document.getElementById(anchParts[1])) {
outputString += `${document.links[idx]}\n`
const puppeteer = require('puppeteer');
// throttle network and cpu?
const throttle = true;
// viewport sizes
const viewportw = 412;
const viewporth = 732;
// url to test
const theurl = 'https://tamethebots.com';
(async() => {