UNPKG

marklet

Version:

Framework for loading dependent external scripts into bookmarklets.

1 lines 7.4 kB
javascript:!function(){(function(e,n){"use strict";function t(n){e.logging&&!u&&console.log(n)}function o(e,n){if("function"==typeof e&&!u)return e(n)}function i(e,n){return u?Promise.reject():Array.isArray(e)?Promise.all(e.map(function(e){return i(e,n)})):"function"==typeof e.skipCondition&&l(e.skipCondition,!1)?(t("Skip condition met, skipping "+e.id),Promise.resolve()):n(e).then(function(){if(e.hasOwnProperty("then"))return i(e.then,n)},function(r){return o(e.onFail,r),e.hasOwnProperty("catch")?(t("Error with include: "+e.id+". Attempting alternate branch."),i(e.catch,n).catch(function(n){return e.required?Promise.reject(n):(t("Error with non-essential include: "+e.id+" and its alternates. Continuing."),Promise.resolve())})):e.required?Promise.reject("Required include "+e.id+" encountered an error. Marklet aborted."):(t("Error with non-essential include: "+e.id+". Continuing."),Promise.resolve())})}function r(n,i){return u?Promise.reject():new Promise(function(r,c){var l=n.id,u=i?n.backupUrl:n.url;l||(l="marklet"+a.toString(),a++);var s=document;if(s.getElementById(l))t(l+" tag already present"),e.rejectIdConflict?c(l+" ID conflict rejected. Marklet aborted."):r();else{var f=s.createElement("script");f.src=u,f.id=l,t("Fetching script: "+l),s.body.appendChild(f),o(n.onFetch);var m=setTimeout(function(){t("Timeout on script: "+l),f.parentNode.removeChild(f),c(),o(n.onTimeout)},e.timeout);f.addEventListener("load",function(){t("Success with script: "+l),clearTimeout(m),d.push(l),r(),o(n.onLoad)}),f.addEventListener("error",function(e){t("Error with script: "+l+". Err: "),t(e),clearTimeout(m),f.parentNode.removeChild(f),c(e),o(n.onError,e)})}})}function c(n,i){return u?Promise.reject():new Promise(function(r,c){var l=i?n.backupUrl:n.url,u=n.id;if(u||(u="marklet"+a.toString(),a++),document.getElementById(u))t(u+" tag already present"),e.rejectIdConflict?c(u+" ID conflict rejected. Marklet aborted."):r();else{var s=document.createElement("link");s.id=u,s.rel="stylesheet",s.type="text/css",s.href=l,t("Fetching style: "+u),document.getElementsByTagName("head")[0].appendChild(s),o(n.onFetch);var f=setTimeout(function(){t("Timeout on style: "+u),s.parentNode.removeChild(s),c(),o(n.onTimeout)},e.timeout);s.addEventListener("load",function(){t("Success with style: "+u),clearTimeout(f),d.push(u),r(),o(n.onLoad)}),s.addEventListener("error",function(e){t("Error with style: "+u+". Err: "),t(e),clearTimeout(f),s.parentNode.removeChild(s),c(e),o(n.onError,e)})}})}function l(e,n){return"function"!=typeof e?(t("No condition given."),!n||Promise.resolve()):(t("Testing code "+e),n?new Promise(function(n,o){e()?(t("Success with "+e),n()):o()}):e()?(t("Success with "+e),!0):void 0)}e.timeout=e.timeout||1e4,e.tickLength=e.tickLength||100,e.localStyleId=e.localStyleId||"markletLocalCss";var a=1,u=!1,d=[],s=setInterval(function(){t("Tick")},e.tickLength),f=i(e.scripts,function(n){return!1!==n.required&&(n.required=!0),l(n.loadCondition,!1)?r(n).catch(function(e){return n.backupUrl?(t("Main URL failed, attempting backup URL for "+n.id),o(n.onBackup),r(n,!0)):Promise.reject(e)}):(t("Condition failed. Will retry in one tick."),new Promise(function(o,i){var c=!0,a=setInterval(function(){l(n.loadCondition,!1)&&(t("Success with "+n.loadCondition),c=!1,r(n).then(function(){o()},function(e){i(e)}),clearInterval(a))},e.tickLength);setTimeout(function(){c&&(t("Timeout with "+n.loadCondition),i("Timeout"),clearInterval(a))},e.timeout)}))}),m=i(e.styles,function(n){return!1!==n.required&&(n.required=!0),"function"==typeof n.skipCondition&&l(n.skipCondition,!1)?(t("Skip condition met, skipping "+n.id),Promise.resolve()):l(n.loadCondition,!1)?c(n).catch(function(){return n.backupUrl?(t("Main URL failed, attempting backup URL for "+n.id),o(n.onBackup),c(n,!0)):Promise.reject(err)}):(t("Condition failed. Will retry in one tick."),new Promise(function(o,i){var r=!0,a=setInterval(function(){l(n.loadCondition,!1)&&(t("Success with "+n.loadCondition),r=!1,c(n).then(function(){o()},function(e){i(e)}),clearInterval(a))},e.tickLength);setTimeout(function(){r&&(t("Timeout with "+n.loadCondition),i("Timeout"),clearInterval(a))},e.timeout)}))});if(e.localStyle){var h=document.createElement("style");h.type="text/css",h.id=e.localStyleId,d.push(e.localStyleId);var p=e.localStyle;h.styleSheet?h.styleSheet.cssText=p:h.appendChild(document.createTextNode(p)),t("Adding local style"),document.getElementsByTagName("head")[0].appendChild(h)}return Promise.all([f,m]).then(function(){t("All tags accounted for, on to the main code.");var i=function(e,n){return function(t){return new Promise(function(i,r){e.forEach(function(e){var n=document.getElementById(e);n.parentNode.removeChild(n)}),n&&console.log("Deleted Marklet Elements."),o(t),i()})}}(d,e.logging);return l(e.codeRunCondition,!0).then(function(){return new Promise(function(e,o){clearInterval(s),t("Running main code."),"function"==typeof n&&n(i),e(i)})},function(){return new Promise(function(o,r){t("Condition failed. Will retry in one tick.");var c=setInterval(function(){l(e.codeRunCondition,!0).then(function(){t("Success with "+e.codeRunCondition),clearInterval(c),clearInterval(s),t("Running main code."),"function"==typeof n&&n(i),o(i)},function(){})},e.tickLength)})})}).catch(function(n){return clearInterval(s),console.error(n),d.forEach(function(e){var n=document.getElementById(e);n.parentNode.removeChild(n)}),t("Deleted Marklet Elements."),u=!0,"function"==typeof e.onAbort&&e.onAbort(n),Promise.reject(n)})})({scripts:[{url:"//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js",backupUrl:"https://code.jquery.com/jquery-3.2.1.min.js",id:"jquery",onFetch:function(){console.log("jquery on Fetch handler!")},onLoad:function(){console.log("jquery on load handler!")},onError:function(){console.log("jquery on error handler!")},onTimeout:function(){console.log("jquery on timeout handler!")},onFail:function(){console.log("jquery on fail handler!")},then:[{url:"//cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.2.3/jquery-confirm.min.js",id:"jquery-alert",loadCondition:function(){if("undefined"!=typeof $)return!0}},{url:"//cdnjs.cloudflare.com/ajax/libs/Sortable/1.6.0/Sortable.min.js",id:"sortable"}],catch:{url:"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js",id:"angular",then:{url:"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.js",id:"chartJS",then:{url:"https://cdnjs.cloudflare.com/ajax/libs/angular-chart.js/1.1.1/angular-chart.js",id:"angularChart"}}}}],styles:[{url:"//cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.2.3/jquery-confirm.min.css",id:"alert-style",required:!1},{url:"//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css",id:"bootstrap",onFetch:function(){console.log("bootstrap on Fetch handler!")},onLoad:function(){console.log("bootstrap on load handler!")},onError:function(){console.log("bootstrap on error handler!")},onTimeout:function(){console.log("bootstrap on timeout handler!")},onFail:function(){console.log("bootstrap on fail handler!")}}],localStyle:"div:min-height:100px;",codeRunCondition:function(){if(void 0!==$.alert)return!0},logging:!0,rejectIdConflict:!1},function(){console.log("I made it!")}).then(function(e){console.log("everything is dandy "),setTimeout(function(){e().then(function(){console.log("Ran deleter function.")})},3e4)}).catch(function(){alert("Oh no, Marklet aborted!")})}();