Skip to content

Instantly share code, notes, and snippets.

const Page = require('puppeteer/lib/Page');
// the following 2 methods are originally from: https://github.com/GoogleChrome/puppeteer/issues/85#issuecomment-341743259, with some modification to fit puppeteerv1.0.0
async function newPageWithNewContext(browser) {
const { browserContextId } = await browser._connection.send('Target.createBrowserContext');
const { targetId } = await browser._connection.send('Target.createTarget', { url: 'about:blank', browserContextId });
const target = await browser._targets.get(targetId);
const client = await browser._connection.createSession(targetId);
const page = await Page.create(client, target, browser._ignoreHTTPSErrors, browser._appMode, browser._screenshotTaskQueue);
page.browserContextId = browserContextId;
@Ariex
Ariex / Promise.allInOrder.js
Last active February 17, 2022 07:31
Promise.allInOrder
// v3 is shorter than v2, and achieve same goal
(()=>{
let Deferred = function(){
let resolver, rejector,
promise = new Promise((resolve, reject)=>{
resolver = resolve;
rejector = reject;
});
promise.resolve = d=>resolver(d);
promise.reject = d=>rejector(d);
@Ariex
Ariex / knapsack.js
Created February 20, 2017 04:03 — forked from danwoods/knapsack.js
Knapsack algorithm in JavaScript
//Knapsack algorithm
//==================
// wikipedia: [Knapsack (0/1)](http://en.wikipedia.org/wiki/Knapsack_problem#0.2F1_Knapsack_Problem)
// Given a set `[{weight:Number, benefit:Number}]` and a capacity,
// find the maximum value possible while keeping the weight below
// or equal to the capacity
// **params**:
// `capacity` : Number,
// `items` : [{w:Number, b:Number}]
// **returns**:
using System;
namespace RsaKeyConverter.Converter
{
public static class BytesExtensions
{
public static string ToBase64(this byte[] bytes)
{
return Convert.ToBase64String(bytes);
}
@Ariex
Ariex / middleware-test.js
Created November 22, 2016 01:46
A simple example of how middleware can be implemented using js-generator
class Application {
constructor() {
this.middlewares = [];
}
use(func) {
this.middlewares.push(func);
}
execute() {
let stack = [];
let idx = 0;
@Ariex
Ariex / GetBase64GifSpacer.cs
Created June 26, 2016 12:59
Create a transparent gif spacer with specified width and height
public static string GetBase64GifSpacer(int width, int height){
var bwidth = BitConverter.GetBytes(width);
var bheight = BitConverter.GetBytes(height);
var bytes = new byte[] { 0x47,0x49,0x46,0x38,0x39,0x61,bwidth[0],bwidth[1],bheight[0],bheight[1],0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0xF9,0x04,0x01,0x00,0x00,0x00,0x00,0x2C,0x00,0x00,0x00,0x00,bwidth[0],bwidth[1],bheight[0],bheight[1],0x00,0x02,0x02,0x44,0x01,0x00,0x3B };
return "data:image/gif;base64,"+Convert.ToBase64String(bytes);
}
/// <summary>
/// Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
/// </summary>
/// <typeparam name="T">Target Type, has to be a class type</typeparam>
/// <param name="validator">An evaluation method, check if current alternative is valid as a return value.</param>
/// <param name="that">The first alternative value</param>
/// <param name="alternatives">Alternatives could be a value that is or can convert to target type; or a Func that return value as target type.</param>
/// <returns>The first value that is not null (or white spaces for string) in arguments.</returns>
public static T Coalesce<T>(this T that, Func<T, bool> validator, params object[] alternatives) where T : class
{
@Ariex
Ariex / git-move-files-in-subfolder.md
Created April 13, 2016 05:13 — forked from ajaegers/git-move-files-in-subfolder.md
Git: move files in an subfolder keeping history

Change structure of project folder with Git

I have this structure:

 project-folder/
     .git
     wp-admin/
     wp-content/
     wp-includes/

.htaccess

@Ariex
Ariex / StringLocker.cs
Last active June 9, 2016 23:37
An implementation for helping to solve concurrent issue when multiple client write to multiple file
using System;
using System.Collections.Generic;
/// <summary>
/// Create a lock block against a string
/// </summary>
public class StringLocker
{
/// <summary>
/// Store map from string to lockable object
@Ariex
Ariex / gist:977eaf219bd29de45a25
Last active August 29, 2015 14:27 — forked from evantahler/gist:1574158
Build Node on Phidget (ARMv4tl + emdebian)