Skip to content

Instantly share code, notes, and snippets.

View nuxodin's full-sized avatar

Tobias Buschor nuxodin

View GitHub Profile
@nuxodin
nuxodin / Event.submitter.polyfill.js
Created April 8, 2020 21:32
Event.submitter polyfill
!function(){
var lastBtn = null
document.addEventListener('click',function(e){
if (!e.target.closest) return;
lastBtn = e.target.closest('button, input[type=submit]');
}, true);
document.addEventListener('submit',function(e){
if (e.submitter) return;
var canditates = [document.activeElement, lastBtn];
for (var i=0; i < canditates.length; i++) {
writeStack = [];
write = function(callback){
if (!writeStack.length) requestAnimationFrame(handleWriteStack);
writeStack.push(callback);
}
handleWriteStack = function(){
while (writeStack.length) writeStack.pop()();
}
function interceptObjectProperties(obj, options){
if (!options) options = {};
if (!options.ignore) options.ignore = {};
for (var i in obj) {
const prop = i;
if (!obj.hasOwnProperty(prop)) continue;
if (options.ignore[prop]) continue;
var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
if (descriptor.set) {
@nuxodin
nuxodin / c1.cssImport.js
Last active June 10, 2019 18:02
CSS Import function. Loads a CSS file relative to the script where the function was called! (like js-module imports)
c1CssImport = function (location) { // todo: handle absolute urls
const e = new Error();
const calledFile = e.stack.split('\n')[2].match(/[a-z]+:[^:]+/);
const calledUrl = new URL(calledFile);
calledUrl.search = '';
const target = new URL(location, calledUrl).toString();
for (let el of document.querySelectorAll('link[rel=stylesheet]')) {
if (el.href === target) return; // already loaded
}
const link = document.createElement('link');
@nuxodin
nuxodin / custom_globals.js
Last active August 31, 2018 09:47
Find custom globals variables
{
const iframe = document.createElement('iframe');
document.body.append(iframe)
setTimeout(()=>{ // wait some time until chrome adds console-accessible properties to the iframe too
const customNames = Object.getOwnPropertyNames(window).filter(item => !(item in iframe.contentWindow) );
const object = Object.create(null);
customNames.forEach(name => object[name] = window[name] )
console.log(object)
},2000);
'wait...';
!function() {
// style
var styleObj = d.documentElement.style;
var vendors = {'moz':1,'webkit':1,'ms':1,'o':1};
c1.dom.css = function(el, style, value) {
if (value === undefined) {
if (typeof style === 'string') {
// getter
if (styleObj[style] !== undefined) return getComputedStyle(el).getPropertyValue(style);
return getComputedStyle(el).getPropertyValue( c1.dom.css.experimental(style) );
@nuxodin
nuxodin / stringToFragment.js
Last active April 8, 2017 11:26
string to dom-nodes with support for old ie's and table-elements
function stringToFragment(html){
var tmpl = document.createElement('template');
tmpl.innerHTML = html;
if (tmpl.content == void 0){ // ie11
var fragment = document.createDocumentFragment();
var isTableEl = /^[^\S]*?<(t(?:head|body|foot|r|d|h))/i.test(html);
tmpl.innerHTML = isTableEl ? '<table>'+html : html;
var els = isTableEl ? tmpl.querySelector(RegExp.$1).parentNode.childNodes : tmpl.childNodes;
while(els[0]) fragment.appendChild(els[0]);
@nuxodin
nuxodin / CSSStyleRule.selectorText.js
Last active March 22, 2017 10:49
hacky repair of CSSRule.selectorText, edge, chrome, safari
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
// hacky fix of CSSRule.selectorText, edge, chrome, safari, https://bugs.chromium.org/p/chromium/issues/detail?id=681814
var desc = Object.getOwnPropertyDescriptor(CSSStyleRule.prototype, 'selectorText');
var getter = desc.get;
desc.get = function(){
var str = getter.apply(this).replace(/\[([^\]]+[^\\\]]):([^\]]+)\]/g, '[$1\\:$2]');
return str;
}
Object.defineProperty(CSSStyleRule.prototype, 'selectorText', desc)
@nuxodin
nuxodin / document.nodeFromPoint.js
Last active November 20, 2017 05:25
Browsers have "elementFromPoint", here is "nodeFromPoint" (including text-nodes) !
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
document.nodeFromPoint = function(x, y) {
const el = document.elementFromPoint(x, y);
const nodes = el.childNodes;
for (let i = 0, node; node = nodes[i++];) {
if (node.nodeType === 3) {
const range = document.createRange();
range.selectNode(node);
const rects = range.getClientRects();
@nuxodin
nuxodin / HTMLFormElement-HTMLInputElement.reportValidity.js
Last active January 7, 2023 19:52
Polyfill for reportValidity()
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
if (!HTMLFormElement.prototype.reportValidity) {
HTMLFormElement.prototype.reportValidity = function() {
if (this.checkValidity()) return true;
var btn = document.createElement('button');
this.appendChild(btn);
btn.click();
this.removeChild(btn);
return false;