Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxodin committed Apr 13, 2021
1 parent 68409bc commit c7334be
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 38 deletions.
138 changes: 100 additions & 38 deletions mod.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,124 @@
!function(){
!function(window, document){

var urls {
'https://cdn.jsdelivr.net/npm/[email protected]/fetch.js':{
'fetch':[window]
'Headers':[window]
'Request':[window]
'Response':[window]
var urls = {
'cdn.jsdelivr.net/npm/[email protected]/fetch.js':{
'fetch':[window],
'Headers':[window],
'Request':[window],
'Response':[window],
},
'https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js':{
'cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js':{
'Promise':[window]
}
'https://polyfill.io/v3/polyfill.min.js?features=Intl':{
'Intl':[window]
}
'https://polyfill.io/v3/polyfill.min.js?features=Intl':{
'Intl':[window]
}
'https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver':{
},
'polyfill.io/v3/polyfill.min.js?features=IntersectionObserver':{
'IntersectionObserver':[window]
}
'https://polyfill.io/v3/polyfill.min.js?features=AbortController':{
},
'polyfill.io/v3/polyfill.min.js?features=AbortController':{
'AbortController':[window]
}
'https://polyfill.io/v3/polyfill.min.js?features=URL':{
'URL':[window]
},
'polyfill.io/v3/polyfill.min.js?features=URL':{
'URL':[window],
'URLSearchParams':[window]
},
'polyfill.io/v3/polyfill.js?features=ResizeObserver':{
'ResizeObserver':[window]
},
'unpkg.com/@ungap/weakset':{
'WeakSet':[window]
},
'unpkg.com/@ungap/custom-elements/es.js':{
'customElements':[window]
},
'unpkg.com/@ungap/item':{
'item':[Array.prototype]
}
/*
'polyfill.io/v3/polyfill.min.js?features=es2021':{
'keys':[Object],
'flags':[RegExp.prototype],
'Symbol':[window],
//'replace':[Symbol],
'Symbol':[window],
'replaceAll':[String.prototype],
}
*/
};

/*
They have internal depencies :(
var ftPolyfills = {
Array:{
from:1,
of:1,
prototype:{
copyWithin:1,
entries:1,
fill:1,
find:1,
findIndex:1,
flat:1,
flatMap:1,
includes:1,
keys:1,
values:1,
}
},
Element:{
prototype:{
toggleAttribute:1,
}
},
String:{
fromCodePoint:1,
}
};

var url, props, prop, obj, objects;
function ftFill(obj, realObj, rootUrl){
var prop;
for (prop in obj) {
if (obj[prop] === 1) {
var url = rootUrl + prop + '/polyfill.js'
//addGetter(realObj, prop, url);
urls[url] = {};
urls[url][prop] = [realObj];
} else {
ftFill(obj[prop], realObj[prop], rootUrl + prop + '/');
}
}
}
ftFill(ftPolyfills, window, 'cdn.jsdelivr.net/gh/Financial-Times/polyfill-library@3/polyfills/');
*/


var url, props, prop, obj, objects, i;
for (url in urls) {
props = urls[url];
for (prop in props) {
objects = props[prop];
for (var i=0; obj; obj=objects[i++];) {
for (i=0; obj=objects[i++];) {
if (prop in obj) {
//console.log('not needed '+prop+' in '+url+'<br>')
continue;
}
//console.log('"'+prop+'" not supported, adding getter');
addGetter(obj, prop, url);
}
}
}

//addGetter(window, 'fetch', 'https://cdn.jsdelivr.net/npm/[email protected]/fetch.js');
//addGetter(window, 'fetch', 'cdn.jsdelivr.net/npm/[email protected]/fetch.js');

function addGetter(obj, prop, url) {
if (obj.hasOwnProperty(prop)) return;
//if (hasOwn.call(obj, prop)) return;
/* other libaries should check properties like so: if (prop in obj) { ... }; so the getter will not fire */
Object.defineProperty(obj, prop, {
configurable: true,
get: function() {
delete obj[prop];
loadScriptSync(url)
console.log(prop+' needed loading sync');
loadScriptSync('https://'+url);
//return c1Use.call(this, prop);
return this[prop];
},
set: function(v) { // needed?
delete obj[prop];
Expand All @@ -59,25 +127,19 @@ function addGetter(obj, prop, url) {
});
};

function loadScriptSync(path, cb, eb) {
function loadScriptSync(path) {
var request = new XMLHttpRequest();
request.open('GET', path, false);
request.send(null);
if (request.status === 200) {
var elem = d.createElement('script');
var elem = document.createElement('script');
elem.text = request.responseText;
d.documentElement.firstChild.appendChild(elem);
document.documentElement.firstChild.appendChild(elem);
elem.setAttribute('data-c1-src',path);
cb({type:'load'});
} else {
eb({type:'error'});
console.warn('failed to load '+path)
}
console.warn('deprecated to load '+path+' sync');
}





}();

}(window, document);
26 changes: 26 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Level.css TEST</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./mod.js"></script>

</head>
<body>

<script style="display:block; white-space: pre-wrap; background:#eee; margin:1rem; padding:1rem;">
var body = document.body;

body.innerHTML += 'no polyfill loaded so far.<br>';

setTimeout(function(){

body.innerHTML += "executing ['x','y','z'].item(-1) loads the polyfill<br>";

var item = ['x','y','z'].item(-1);

body.innerHTML += "result: "+item;

}, 1000)
</script>

0 comments on commit c7334be

Please sign in to comment.