Skip to content

Instantly share code, notes, and snippets.

View coffee-mug's full-sized avatar

Lucas Kostka coffee-mug

View GitHub Profile
@coffee-mug
coffee-mug / hn_sorter.js
Created June 21, 2017 13:24
Script to sort HN links by comments count
/** Script to sort HN comments by post date **/
var itemsArray = [],
tb = document.querySelectorAll('table.itemlist > tbody > tr');
function sortBycom(a, b) {
var left = a[1].querySelector('td.subtext > a:last-child'),
right = b[1].querySelector('td.subtext > a:last-child');
// If no comments has been made yet, push them at the end
@coffee-mug
coffee-mug / chainPromises.js
Created March 15, 2018 14:04
Chain promises in JS
// Promises, the big deal
// Here's a, a small function that returns a promise,
// which resolves itself at a random time between 0 and 1/2s, returning "My question num"
var a = (num) => {
return new Promise( (res, rej) => {
// return "My question num" between immediately and 1/2 s.
return setTimeout( () => res(`My question ${num}`), 500 * Math.random())
})
}
@coffee-mug
coffee-mug / static-gen.js
Created September 14, 2018 06:47
Static generator first try
const fs = require('fs')
const path = require('path')
const DEV_MODE = true;
// utils
debug = (...messages) => DEV_MODE ? console.log(messages) : "";
const parser = {
feed(filename, opts) {
@coffee-mug
coffee-mug / raycaster.js
Last active January 22, 2019 15:24
Basic JS raycaster, based on awesome https://lodev.org/cgtutor/raycasting.html tutorial; WIP
/**
* Javasript adaptation of famous : https://lodev.org/cgtutor/raycasting.html
*/
// TODO: find a way to remove the globals
const SCREEN_HEIGHT = 512
const SCREEN_WIDTH = 384
const MOVE_SPEED = (1 / 60) * 3
const ROT_SPEED = (1 / 60) * 6
@coffee-mug
coffee-mug / main.go
Created June 4, 2019 06:34
Get all cookies with Golang chromedp
package main
import (
"context"
"fmt"
"log"
"strings"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
@coffee-mug
coffee-mug / ruleFinder.js
Created October 10, 2019 13:10
Find Pageload/Direct Call DTM rule name throguh js file id
var exists = (collection, property) => collection[property] && collection[property].length > 0;
var debugMode = true
// rule Finder - Find the name of a _satellite direct call rules by script id
// TODO:
// - Refactor
// - Make it recursive
// USAGE:
@coffee-mug
coffee-mug / gaDimensionsCreator.js
Created November 14, 2019 14:32
Create GA Custom Dimensions from a javascript array - no API.
/** UTILS **/
async function after(delay, fn) {
return new Promise((resolve, reject) => {
let to = setTimeout(function () {
resolve(fn());
clearTimeout(to);
}, delay)
})
}
// HOW TO:
// 1/ Update the STEPS_TO_CREATE array in the main function with the checkout steps names you want to create. Please list them in order.
// 2/ paste the script in the console from the ecommerce settings page.
main()
function main() {
// Indicate desired steps names, in order, that need to be created
var STEPS_TO_CREATE = ['shipping', 'payment'];
// config holds the required headers (authorization and content-type) to be passed to each request.
// To get a working token, just:
// 1- Go to oneTrust websites page and open your network
// 2- Click "Add Website"
// 3- Locate the request to the "token" endpoint in your network
// 4- In the Preview tab, locate the "access_token" property and copy/paste its value next to "Bearer" in the "authorization"
// header from the config object. Alternatively, if you have already instantiated an OTDomainAPI manager, use the method updateToken.
const config = {
headers: {
"accept": "application/json, text/plain, */*",
// Returns an array of launch element (rule, dataelements, ...) sorted
// by bytes length
function dataElementsWeights() {
// We use an object so that total is passed by reference (using an int would
// pass it by value, thus leading to a different value in each object instead
// of the total value)
var totalWeight = {
total: 0
};