Skip to content

Instantly share code, notes, and snippets.

@codenameyau
Last active April 6, 2022 13:07
Show Gist options
  • Save codenameyau/9f0a26a0f1be35a6784a to your computer and use it in GitHub Desktop.
Save codenameyau/9f0a26a0f1be35a6784a to your computer and use it in GitHub Desktop.

Revisions

  1. codenameyau revised this gist Jan 19, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,11 @@ document.cookie = "token=12345; Max-Age=120; Secure; Domain=mozilla.org; Path=/;
    // Set a subdomain cookie.
    document.cookie = "name=hello world; Max-Age=120; Secure; Domain=developer.mozilla.org; Path=/;"

    // You can have cookies with the same name but with different paths and domains.
    document.cookie = "name=1; Max-Age=60; Secure; Domain=mozilla.org;"
    document.cookie = "name=2; Max-Age=60; Secure; Domain=mozilla.org; Path=/"
    document.cookie = "name=3; Max-Age=60; Secure; Domain=developer.mozilla.org; Path=/;"

    // Copies variable.
    copy(temp1)
    copy(JSON.stringify(temp1))
  2. codenameyau revised this gist Jan 19, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    *************************************************************************/

    // Set top level domain cookie.
    document.cookie = "name=hello world; Max-Age=120; Secure; Domain=mozilla.org; Path=/;"
    document.cookie = "token=12345; Max-Age=120; Secure; Domain=mozilla.org; Path=/;"

    // Set a subdomain cookie.
    document.cookie = "name=hello world; Max-Age=120; Secure; Domain=developer.mozilla.org; Path=/;"
  3. codenameyau revised this gist Jan 19, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    /************************************************************************
    * CLIENT ONE-LINERS
    *************************************************************************/

    // Set top level domain cookie.
    document.cookie = "name=hello world; Max-Age=120; Secure; Domain=mozilla.org; Path=/;"

    // Set a subdomain cookie.
    document.cookie = "name=hello world; Max-Age=120; Secure; Domain=developer.mozilla.org; Path=/;"

    // Copies variable.
    copy(temp1)
    copy(JSON.stringify(temp1))
  4. codenameyau revised this gist Jul 17, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,11 @@
    copy(temp1)
    copy(JSON.stringify(temp1))

    // Capture regex groups with variable
    const string = 'ayyyy lmao'
    const rgx = /(?<firstVowel>[aeiou])/
    string.match(rgx).groups.firstVowel

    // Copies current selection to clipboard.
    // https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
    // https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
  5. codenameyau revised this gist Jun 4, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,9 @@ document.execCommand('copy')
    // Copies text content of click to clipboard.
    navigator.clipboard && navigator.clipboard.writeText(event.target.textContent);

    // Makes page text editable.
    document.designMode = 'on'

    // Access third party frames on window.
    window.frames

  6. codenameyau revised this gist Mar 3, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    /************************************************************************
    * CLIENT ONE-LINERS
    *************************************************************************/
    // Copies variable.
    copy(temp1)
    copy(JSON.stringify(temp1))

    // Copies current selection to clipboard.
    // https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
  7. codenameyau revised this gist Oct 24, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,9 @@
    // https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
    document.execCommand('copy')

    // Copies text content of click to clipboard.
    navigator.clipboard && navigator.clipboard.writeText(event.target.textContent);

    // Access third party frames on window.
    window.frames

  8. codenameyau revised this gist Aug 14, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,11 @@
    * CLIENT ONE-LINERS
    *************************************************************************/

    // Copies current selection to clipboard.
    // https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
    // https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
    document.execCommand('copy')

    // Access third party frames on window.
    window.frames

  9. codenameyau revised this gist Jun 11, 2018. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions special-utils.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,41 @@ setTimeout(function (callback) {
    // Offload Heavy computation.
    }, 0);

    /********************************************************************
    * JQUERY serializeArray
    *********************************************************************/

    // Converts form into Array of Objects.
    // https://plainjs.com/javascript/ajax/serialize-form-data-into-an-array-46/
    function serializeArray(form) {
    var field, l, s = [];
    if (typeof form == 'object' && form.nodeName == "FORM") {
    var len = form.elements.length;
    for (var i = 0; i < len; i++) {
    field = form.elements[i];
    if (field.name && !field.disabled && field.type != 'file' && field.type != 'reset' && field.type != 'submit' && field.type != 'button') {
    if (field.type == 'select-multiple') {
    l = form.elements[i].options.length;
    for (j = 0; j < l; j++) {
    if (field.options[j].selected)
    s[s.length] = {
    name: field.name,
    value: field.options[j].value
    };
    }
    } else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) {
    s[s.length] = {
    name: field.name,
    value: field.value
    };
    }
    }
    }
    }
    return s;
    }


    // Nodejs check if module is called directly
    const CALLED_DIRECTLY = require.main === module;

    @@ -60,3 +95,4 @@ jQuery.Color.fn.contrastColor = function() {
    // usage examples:
    jQuery.Color("#bada55").contrastColor(); // "black"
    element.css( "color", jQuery.Color( element, "backgroundColor" ).contrastColor() );

  10. codenameyau revised this gist Jun 6, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    * CLIENT ONE-LINERS
    *************************************************************************/

    // Access third party frames on window.
    window.frames

    // Create bookmark and set this to URL to skip ads.
    javascript:void(document.querySelector('video').currentTime = document.querySelector('video').duration)

  11. codenameyau revised this gist Oct 16, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@
    // Create bookmark and set this to URL to skip ads.
    javascript:void(document.querySelector('video').currentTime = document.querySelector('video').duration)

    // See all event listeners. Remember to remove listener when unmounting.
    getEventListeners(document)

    // See Slack Emoji authors.
    Array.from(document.querySelectorAll('.author_cell > a')).map((a) => a.text.trim()).sort()

  12. codenameyau revised this gist Sep 18, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions special-utils.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,9 @@ setTimeout(function (callback) {
    // Offload Heavy computation.
    }, 0);

    // Nodejs check if module is called directly
    const CALLED_DIRECTLY = require.main === module;

    function splitTextarea(text) {
    return text.trim().replace(/\s+/, '\n').replace(/\n{2,}/, '').split('\n');
    }
  13. codenameyau revised this gist Jun 30, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions special-utils.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    /********************************************************************
    * UTILS
    *********************************************************************/
    // https://stackoverflow.com/q/779379
    setTimeout(function (callback) {
    // Offload Heavy computation.
    }, 0);

    function splitTextarea(text) {
    return text.trim().replace(/\s+/, '\n').replace(/\n{2,}/, '').split('\n');
    }
  14. codenameyau revised this gist May 26, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions console-tricks.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@
    // Create bookmark and set this to URL to skip ads.
    javascript:void(document.querySelector('video').currentTime = document.querySelector('video').duration)

    // See Slack Emoji authors.
    Array.from(document.querySelectorAll('.author_cell > a')).map((a) => a.text.trim()).sort()

    // Use this to click all links.
    Array.from(document.querySelectorAll('a.block')).forEach(a => a.click())

  15. codenameyau revised this gist May 17, 2017. 1 changed file with 0 additions and 39 deletions.
    39 changes: 0 additions & 39 deletions special-utils.js
    Original file line number Diff line number Diff line change
    @@ -52,42 +52,3 @@ jQuery.Color.fn.contrastColor = function() {
    // usage examples:
    jQuery.Color("#bada55").contrastColor(); // "black"
    element.css( "color", jQuery.Color( element, "backgroundColor" ).contrastColor() );


    /********************************************************************
    * MANY TO MANY
    *********************************************************************/
    var fake_data_points = [
    { id: 1, name: 'Can its beauty be refurbished?' },
    { id: 2, name: 'Offers online car cleaning?' },
    { id: 3, name: 'Has shopping cart?' }
    ];

    var fake_projects = [
    { id: 1, name: 'Auto' },
    { id: 2, name: 'Beauty' }
    ];

    var fake_data_points_to_project = [
    { id: 1, data_point_id: 1, project_id: 1 },
    { id: 2, data_point_id: 1, project_id: 2 },
    { id: 4, data_point_id: 2, project_id: 1 },
    { id: 5, data_point_id: 2, project_id: 2 }
    ];

    exports.get_many_to_many = function(params) {
    var many_to_many_data = _.where(params.many_to_many, params.lookup);
    var related_table_index = _.indexBy(params.related_table, 'id');

    return _.map(many_to_many_data, function(object) {
    return related_table_index[object[params.related_id]];
    });
    };

    get_many_to_many({
    many_to_many: fake_data_points_to_project,
    table: fake_data_points,
    related_table: fake_data_points,
    lookup: {'data_point_id': 1},
    related_id: 'project_id'
    });
  16. codenameyau renamed this gist May 17, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. codenameyau renamed this gist May 17, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  18. codenameyau revised this gist May 17, 2017. 1 changed file with 0 additions and 97 deletions.
    97 changes: 0 additions & 97 deletions ajax-upload-img.js
    Original file line number Diff line number Diff line change
    @@ -1,97 +0,0 @@
    /********************************************************************
    * PREVIEW, RESIZE, AND UPLOAD IMAGE VIA AJAX
    *********************************************************************/

    // https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
    function previewImageUpload ($e) {
    var files = $e.target.files;
    if (files) {
    var imgContainerSelector = $e.target.getAttribute('data-img-preview-container');
    var imgPreviewObj = URL.createObjectURL(files[0]);
    var $imgContainer = $(imgContainerSelector);
    $imgContainer.html('<img class="img-upload-preview">');
    $imgContainer.find('img').attr('src', imgPreviewObj);
    }
    }

    // http://stackoverflow.com/a/5100158
    function dataURItoBlob(dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
    byteString = atob(dataURI.split(',')[1]);
    else
    byteString = window.unescape(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to a typed array
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
    }

    return new Blob([ia], {type:mimeString});
    }

    function resizeWidth(file, max_width, callback) {
    max_width = max_width || 600;

    // Read image from file system and resize.
    var reader = new FileReader();
    reader.onload = function(e) {
    var img = document.createElement('img');
    img.src = e.target.result;

    // Specify new width and height.
    var width = img.width;
    var height = img.height;
    if (width > max_width) {
    height = parseInt(height * (max_width / width), 10);
    width = max_width;
    }

    // Create canvas which performs the re-sizing.
    var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    canvas.style.visibility = 'hidden';
    var ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    var resizedFile = dataURItoBlob(canvas.toDataURL(file.type));
    callback(resizedFile);
    };
    reader.readAsDataURL(file);
    }

    function formatFilename(filename) {
    var extIndex = filename.lastIndexOf('.');
    var basename = filename.slice(0, extIndex).replace(/\s+/g, '-');
    var extension = filename.slice(extIndex);
    return basename + '-' + Date.now() + extension;
    }

    // Callback to ajax. It needs a special handler to upload images.
    var $inputImage = $form.find('.input-image-upload');
    var imageFile = $inputImage[0].files[0];
    var formattedFilename = (imageFile === undefined) ?
    self.model.get('logo_filename') : this.formatFilename(imageFile.name);

    resizeWidth(imageFile, 115, function(resizedFile) {
    var formData = new FormData();
    formData.append('input-image-upload', resizedFile, formattedFilename);

    $.ajax({
    url: '/api/brand-logo/',
    type: 'POST',
    data: formData,
    cache: false,
    processData: false,
    contentType: false,

    success: function(data, status) {
    console.log(data);
    }
    });
    });
  19. codenameyau revised this gist May 3, 2017. 2 changed files with 2 additions and 20 deletions.
    4 changes: 2 additions & 2 deletions mongo.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Checks if nested property exists.
    db.drafts.find({"collection_type": "web_advertising", "email": "jorge.yau@l2inc.com", "rows.222": { $exists: true } });
    db.drafts.find({"collection_type": "web_advertising", "email": "jorge@email.com", "rows.222": { $exists: true } });

    // Grabs the count.
    db.drafts.find({"email": "jorge.yau@l2inc.com"}).count();
    db.drafts.find({"email": "jorge@email.com"}).count();
    18 changes: 0 additions & 18 deletions react-redux.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    /********************************************************************
    * NESTED REDUCERS
    *********************************************************************/
    var x = {
    name: 'Sabbir',
    same: 'name',
    skillz: { awesomeness: false, dbStuffs: false }
    };

    var y = {
    name: 'Sabbeez',
    skillz: { awesomeness: true }
    };

    // First extend flat props, then extended the nested skillz.
    var z = {...x, ...y, skillz: {...x.skillz, ...y.skillz}};

    console.log(z);
  20. codenameyau revised this gist Apr 12, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 1-console-oneliners.js
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,9 @@ Array.from(document.images).forEach(img => console.log(img.getAttribute('src')))
    // Like all facebook posts.
    Array.from(document.body.querySelectorAll('.UFILikeLink')).forEach((a) => a.click())

    // Selecting wildcard classes.
    Array.from(document.querySelectorAll('div[class^="ManageSection__column-left"] .dp-checkbox')).forEach((a) => a.click())

    /************************************************************************
    * SERVER ONE-LINERS
    *************************************************************************/
  21. codenameyau revised this gist Mar 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ajax-upload-img.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ function dataURItoBlob(dataURI) {
    }

    function resizeWidth(file, max_width, callback) {
    max_width = max_width || 115;
    max_width = max_width || 600;

    // Read image from file system and resize.
    var reader = new FileReader();
  22. codenameyau revised this gist Feb 17, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions special-utils.js
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,15 @@ encodeURIComparison = function() {
    console.table(arr);
    };

    function encodeQueryParams (url, queryParams) {
    return Object.keys(queryParams).reduce((a, key) => {
    a.push(key + '=' + encodeURIComponent(queryParams[key]));
    return a;
    }, []).join('&');
    }

    let queryString = encodeQueryParams(queryParams);
    url += url.indexOf('?') > -1 ? '&' : '?' + queryString;

    /********************************************************************
    * JQUERY COLOR CONTRAST
  23. codenameyau revised this gist Feb 8, 2017. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions react-redux.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    /********************************************************************
    * NESTED REDUCERS
    *********************************************************************/
    var x = {
    name: 'Sabbir',
    same: 'name',
    skillz: { awesomeness: false, dbStuffs: false }
    };

    var y = {
    name: 'Sabbeez',
    skillz: { awesomeness: true }
    };

    // First extend flat props, then extended the nested skillz.
    var z = {...x, ...y, skillz: {...x.skillz, ...y.skillz}};

    console.log(z);
  24. codenameyau revised this gist Feb 7, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 1-console-oneliners.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    * CLIENT ONE-LINERS
    *************************************************************************/

    // Create bookmark and set this to URL to skip ads.
    javascript:void(document.querySelector('video').currentTime = document.querySelector('video').duration)

    // Use this to click all links.
    Array.from(document.querySelectorAll('a.block')).forEach(a => a.click())

  25. codenameyau revised this gist Dec 16, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions special-utils.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,16 @@ function splitTextarea(text) {
    return text.trim().replace(/\s+/, '\n').replace(/\n{2,}/, '').split('\n');
    }

    // Download using fetch API..
    import download from 'downloadjs';
    const handleBufferDownload = (filename) => {
    res.arrayBuffer()
    .then((buffer) => {
    download(buffer, filename, contentType);
    stopLoading();
    });
    };

    // http://stackoverflow.com/a/23842171
    encodeURIComparison = function() {
    var arr = [];
  26. codenameyau revised this gist Dec 4, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 1-console-oneliners.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ Array.from(document.querySelectorAll('a.block')).forEach(a => a.click())
    // Grab all the images from a page (namely instagram)
    Array.from(document.images).forEach(img => console.log(img.getAttribute('src')))

    // Like all facebook posts.
    Array.from(document.body.querySelectorAll('.UFILikeLink')).forEach((a) => a.click())

    /************************************************************************
    * SERVER ONE-LINERS
  27. codenameyau revised this gist Nov 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1-console-oneliners.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * CLIENT ONE-LINERS
    *************************************************************************/

    // Use this to click all selected elements.
    // Use this to click all links.
    Array.from(document.querySelectorAll('a.block')).forEach(a => a.click())

    // Grab all the images from a page (namely instagram)
  28. codenameyau revised this gist Nov 14, 2016. No changes.
  29. codenameyau revised this gist Nov 14, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions 1-console-oneliners.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,17 @@
    /************************************************************************
    * CLIENT ONE-LINERS
    *************************************************************************/

    // Use this to click all selected elements.
    Array.from(document.querySelectorAll('a.block')).forEach(a => a.click())

    // Grab all the images from a page (namely instagram)
    Array.from(document.images).forEach(img => console.log(img.getAttribute('src')))


    /************************************************************************
    * SERVER ONE-LINERS
    *************************************************************************/

    // Use Now to deploying static app serving a single page app.
    ns ./build --cmd 'list ./content -s'
  30. codenameyau revised this gist Nov 10, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions 1-console-oneliners.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    // Use this to click all selected elements.
    Array.from(document.querySelectorAll('a.block')).forEach(a => a.click())

    // Grab all the images from a page (namely instagram)
    Array.from(document.images).forEach(img => console.log(img.getAttribute('src')))