BurakBal96 / BB Library

// ==UserScript==
// @exclude       *
// @author       BurakBal96

// ==UserLibrary==
// @name         BB Library
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Bunch of random functions
// @license      MIT
// ==/UserScript==
// ==/UserLibrary==


'use strict';

const addStyle = function (aCss) {
	const head = document.getElementsByTagName('head')[0];
	if (head) {
		const style = document.createElement('style');
		style.setAttribute('id', 'custom-css');
		style.setAttribute('type', 'text/css');
		style.textContent = aCss;
		head.appendChild(style);
		return style;
	}
	return null;
};

/*
    Sleep function to wait some time
*/
async function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
    
/*
    Find function with jquery
*/
const find =async (search) => {
    let timeout = 0;
    let res;
    do{
        res = $(search);
        await sleep(50);
        timeout += 50;
    } while(!res.length && timeout < 1000);
    await sleep(50);
    return res;
}

/*
    Debounce function to run functions with at least minimum interval
*/
const debounce = function (func, wait, immediate) {
	let timeout;
	return (...args) => {
		const later = () => {
			timeout = null;
			if (!immediate) func.apply(this, args);
		};
		const callNow = immediate && !timeout;
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
		if (callNow) func.apply(this, args);
	};
};

const normalizeEvent = (el, eventName) => {
    var ev;
    if ('KeyboardEvent' in window) {
        ev = new window.KeyboardEvent(eventName, {
            bubbles: true,
            cancelable: false,
        });
    }
    else {
        ev = el.ownerDocument.createEvent('Events');
        ev.initEvent(eventName, true, false);
        ev.charCode = 0;
        ev.keyCode = 0;
        ev.which = 0;
        ev.srcElement = el;
        ev.target = el;
    }

    return ev;
}

const clickElement =(el)  =>{
    if (!el || el && 'function' !== typeof el.click) {
        return false;
    }
    el.click();
    return true;
}

const doFocusElement =(el, setValue)  =>{
    if (setValue) {
        var existingValue = el.value;
        el.focus();
        el.value !== existingValue && (el.value = existingValue);
    } else {
        el.focus();
    }
}

const setValueForElement =(el) => {
    var valueToSet = el.value;
    clickElement(el);
    doFocusElement(el, false);
    el.dispatchEvent(normalizeEvent(el, 'keydown'));
    el.dispatchEvent(normalizeEvent(el, 'keypress'));
    el.dispatchEvent(normalizeEvent(el, 'keyup'));
    el.value !== valueToSet && (el.value = valueToSet);
}

const setValueForElementByEvent =(el) => {
    var valueToSet = el.value,
        ev1 = el.ownerDocument.createEvent('HTMLEvents'),
        ev2 = el.ownerDocument.createEvent('HTMLEvents');

    el.dispatchEvent(normalizeEvent(el, 'keydown'));
    el.dispatchEvent(normalizeEvent(el, 'keypress'));
    el.dispatchEvent(normalizeEvent(el, 'keyup'));
    ev2.initEvent('input', true, true);
    el.dispatchEvent(ev2);
    ev1.initEvent('change', true, true);
    el.dispatchEvent(ev1);
    el.blur();
    el.value !== valueToSet && (el.value = valueToSet);
}

const doAllFillOperations = (el) => {
    setValueForElement(el);
    setValueForElementByEvent(el);
}