Skip to content

Instantly share code, notes, and snippets.

View nuxodin's full-sized avatar

Tobias Buschor nuxodin

View GitHub Profile
writeStack = [];
write = function(callback){
if (!writeStack.length) requestAnimationFrame(handleWriteStack);
writeStack.push(callback);
}
handleWriteStack = function(){
while (writeStack.length) writeStack.pop()();
}
/* ussage
<div flex-gap=fallback-margin style="--x-gap:10px">
<span>a</span>
<span>b</span>
</div>
*/
var div = document.createElement('div');
div.innerHTML = '<i></i><i></i>';
div.style.display = 'inline-flex';
@nuxodin
nuxodin / href-everywere.js
Last active June 9, 2020 05:48
Enable each element to be linked
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
/**
ussage:
<table>
<tr data-c1-href="https://example.com" data-c1-target="_blank">
<td> <a href="https://example.com" target="_blank">example.com</a>
<td> Do not forget to add a real link so that your content is accessible.
</table>
*/
c1 = {
@nuxodin
nuxodin / requestSubmit_and_reportValidity.js
Last active April 17, 2020 08:58 — forked from mcshaz/reportValidity.js
reportValidity polyfill for IE 10, 11
if (!HTMLFormElement.prototype.requestSubmit) {
HTMLFormElement.prototype.requestSubmit = function(submitter) {
let submitBtn = submitter;
if (!submitBtn) {
submitBtn = document.createElement('input');
submitBtn.type = 'submit';
submitBtn.hidden = true;
this.appendChild(submitBtn);
}
submitBtn.click();
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 / 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 / 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]);