Skip to content

Instantly share code, notes, and snippets.

View jonesnxt's full-sized avatar

Kelsey jonesnxt

  • Subsecond
  • San Diego, CA
View GitHub Profile
Array.from(document.styleSheets)
.filter((sheet) => {
try {
sheet.cssRules;
return true;
} catch (err) {
console.log(
`Due to CORS issues, this script can't access "${sheet.href}"`
);
return false;
styleSheets.map((sheet) =>
Array.from(sheet.cssRules).map((rule) =>
rule.type === rule.FONT_FACE_RULE
? { /* include this rule */ }
: rule.type === rule.STYLE_RULE
? { /* include this rule if above-the-fold */ }
: rule.type === rule.MEDIA_RULE
? Array.from(rule.cssRules)
.map((mediaRule) =>
mediaRule.type === mediaRule.FONT_FACE_RULE
Array.from(document.querySelectorAll(rule.selectorText)).some(
(node) =>
node.getBoundingClientRect().top < window.innerHeight &&
window.getComputedStyle(node).display !== 'none'
)
cssRuleList
.flat()
.filter((rule) => rule != null && !new RegExp(/^@media.*{}$/).test(rule))
.join(' ');
<link
rel="preload"
as="image"
imagesrcset="/subsecond.webp?width=414&quality=70 414w,/subsecond.webp?width=785&quality=70 785w,/subsecond.webp?width=1024&quality=70 1024w,/subsecond.webp?width=1600&quality=70 1600w"
imagesizes="100vw"
media="(-webkit-min-device-pixel-ratio: 0.1) and (-webkit-max-device-pixel-ratio: 1)"
/>
<link
rel="preload"
as="image"
function __ss(){fetch("/checkout",{method:"POST"}).then((b,c)=>{if(c)throw c;if(b.url.includes("/cart"))return;let a=document.createElement("link");a.setAttribute("rel","prefetch"),a.setAttribute("href",b.url),a.setAttribute("id","_ss_pre"),document.head.append(a),Array.from(document.querySelectorAll("button[name='checkout']")).forEach(a=>{a.setAttribute("type","button"),a.setAttribute("onclick","window.location = '"+b.url+"';")})})}__ss(),function(a,b){"function"==typeof b&&(a.fetch=function(){var c=b.apply(this,arguments);if(arguments[0].includes("cart")){let a=document.getElementById("_ss_pre");if(null==a)__ss();else{let d=a.getAttribute("href");a.setAttribute("href",""),setTimeout(()=>{a.setAttribute("href",d),Array.from(document.querySelectorAll("button[name='checkout']")).forEach(a=>{a.setAttribute("type","button"),a.setAttribute("onclick","window.location = '"+d+"';")})},500)}}return c})}(window,window.fetch)
S('JSXOpeningElement.Button JSXAttribute.primary').text('type="primary"');
S('JSXOpeningElement').each((openingTag) => {
const attributes = openingTag.children('JSXAttribute');
const sortedAttributes = attributes
.map((attribute) => attribute.text())
.sort();
attributes.each((attribute, i) => attribute.text(sortedAttributes[i]));
});
S('Program').each((file) => {
const imports = file
.children('ImportDeclaration')
.map((imp) => ({
text: imp.text(),
hasNamedImports: imp.children().length !== 1,
packageName: imp.children('Literal').text(),
}))
.sort((a, b) => (a.packageName > b.packageName ? -1 : 1));
const { parsers: typescriptParsers } = require('prettier/parser-typescript');
const { parsers: javascriptParsers } = require('prettier/parser-babel');
const S = require('subsecond');
function preprocess(text, opts) {
S.load({ 'any.tsx': text });
// A quick test to make sure everything works.
S().append('// its working!');