domsnap
Version:
Offline web pages by persisting DOM to IndexedDB/WebSQL.
1 lines • 6.5 kB
JavaScript
!function(e){function n(o){if(t[o])return t[o].exports;var i=t[o]={exports:{},id:o,loaded:!1};return e[o].call(i.exports,i,i.exports,n),i.loaded=!0,i.exports}var t={};return n.m=e,n.c=t,n.p="",n(0)}([function(e,n,t){function o(e){return T.isNil(e)?N:e}function i(e){return T.isFunction(e)?e.apply(this,[].slice.call(arguments,1)):e}function r(e){var n=window.location,t=n.host,o=t+n.pathname;return/^host$/gi.test(e)?t:/^path$/gi.test(e)||T.isNil(e)?o:e}function c(e){if(E)return v;E=!0,T.apply(h,e);var n=A;return("WebSQL"==e.storeType||!e.storeType&&T.os.ios)&&(n=m),w=new n(function(){w.getAll(r(h.scope),h.version,function(e){e.forEach(function(e){S.set(e.selector,e.capture_id,e.htm)}),h.onReady&&h.onReady(v)})}),v}function s(e,n){n=n||{};var t,c;return t=T.isNil(n.id)?o(n.id):i(n.id,e),c=T.isNil(n.html)?T.html(e):i(n.html,e),S.set(e,t,c),w.add(e,t,c,r(h.scope),h.version),v}function u(e,n){n=n||{};var t,r;return t=T.isNil(n.id)?o(n.id):i(n.id,e),r=l(e,t),T.html(e,r),n.fallback&&T.isNil(r)&&n.fallback(),v}function a(e,n){n=n||{};var t=T.isArray(e)?e:[e];return t.forEach(function(e){I.watch(e,function(){s(e,{id:n.id,html:n.html})})}),v}function l(e,n){return S.get(e,n)}function d(e){return S.get(e)}function f(e,n){return S.del(e,n),w["delete"](e,n,r(h.scope),h.version),v}function p(e){return(T.isNil(e)||e==h.version)&&S.empty(),w.deleteAll(r(h.scope),e||h.version),v}var v,h,w,E,T=t(1),D=t(3),g=t(4),A=t(2),m=t(5),N="DEFAULT_CAPTURE_ID",S=new D,I=new g;h={storeType:null,scope:"path",version:1},v={capture:s,resume:u,watch:a,get:l,getAll:d,remove:f,clear:p},window.DOMSnap=c,e.exports=c},function(e,n){function t(e){return void 0==e||null==e||0==e}function o(e){return"function"==typeof e}function i(e){return"[object Array]"===toString.call(e)}function r(e,n,t){var i=o(n)?n.call(e):n;if(i){var r;for(r in i)e[r]=t?t(i[r]):i[r]}}function c(e){return document.querySelector(e)}function s(e,n){var o=c(e);return t(n)?o.innerHTML:o.innerHTML=n}var u=navigator.userAgent,a=u.match(/(Android);?[\s\/]+([\d.]+)?/),l=u.match(/(iPad).*OS\s([\d_]+)/),d=u.match(/(iPod)(.*OS\s([\d_]+))?/),f=!l&&u.match(/(iPhone\sOS)\s([\d_]+)/),p={};a&&(p.android=!0,p.version=a[2]),f&&!d&&(p.ios=p.iphone=!0,p.version=f[2].replace(/_/g,".")),l&&(p.ios=p.ipad=!0,p.version=l[2].replace(/_/g,".")),d&&(p.ios=p.ipod=!0,p.version=d[3]?d[3].replace(/_/g,"."):null),n.os=p,n.isNil=t,n.isFunction=o,n.isArray=i,n.apply=r,n.el=c,n.html=s},function(e,n,t){var o,i,r,c=t(1);o=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB,i=window.hasOwnProperty("webkitIndexedDB")?window.webkitIDBTransaction:window.IDBTransaction,r=window.hasOwnProperty("webkitIndexedDB")?window.webkitIDBKeyRange:window.IDBKeyRange,e.exports=function(e){function n(e){"undefined"!=typeof console&&console.error("An error occurred",e)}function t(){r.objectStoreNames.contains(l)&&r.deleteObjectStore(l);var e=r.createObjectStore(l,{keyPath:"id",autoIncrement:!0});e.createIndex("id","id",{unique:!0}),e.createIndex("selector","selector",{unique:!1}),e.createIndex("capture_id","capture_id",{unique:!1}),e.createIndex("scope","scope",{unique:!1}),e.createIndex("version","version",{unique:!1})}var r,s=this,u="DOMSnap_DB",a=14,l="Snap",d=o.open(u,a);d.onsuccess=function(n){var o;r=n.target.result,a=String(a),r.setVersion&&a!==r.version?(o=r.setVersion(a),o.onsuccess=function(){t(),o.result.oncomplete=function(){e&&e(s)}}):e&&e(s)},d.onupgradeneeded=function(e){r=e.target.result,t()},d.onerror=n,this.add=function(e,t,o,s,u){!c.isNil(e)&&!c.isNil(t)&&this["delete"](e,t,s,u,function(){var c={selector:e,capture_id:t,htm:o,scope:s,version:u,create_date:(new Date).getTime()};r.transaction([l],i.READ_WRITE||"readwrite").objectStore(l).add(c).onerror=n})},this["delete"]=function(e,n,t,o,c){r.transaction([l],i.READ_WRITE||"readwrite").objectStore(l).openCursor().onsuccess=function(i){var r,s=i.target.result;return s?(r=s.value,r.selector==e&&r.capture_id==n&&r.scope==t&&r.version==o&&s["delete"](),void s["continue"]()):void(c&&c())}},this.getAll=function(e,n,t){var o=[];r.transaction([l],i.READ_ONLY||"readonly").objectStore(l).openCursor().onsuccess=function(i){var r,c=i.target.result;return c?(r=c.value,r.scope==e&&r.version==n&&o.push(c.value),void c["continue"]()):void t(o)}},this.deleteAll=function(e,n){r.transaction([l],i.READ_WRITE||"readwrite").objectStore(l).openCursor().onsuccess=function(t){var o,i=t.target.result;i&&(o=i.value,o.scope==e&&o.version==n&&i["delete"](),i["continue"]())}}}},function(e,n,t){function o(){var e={};this.get=function(n,t){return e[n]?i.isNil(t)?e[n]:e[n][t]:null},this.set=function(n,t,o){i.isNil(n)||i.isNil(t)||i.isNil(o)||(e[n]=e[n]||{},e[n][t]=o)},this.del=function(n,t){e[n]&&(i.isNil(t)?(e[n]=null,delete e[n]):(e[n][t]=null,delete e[n][t]))},this.empty=function(){e={}}}var i=t(1);e.exports=o},function(e,n,t){function o(){function e(e,t){n(e),o[e]=t}function n(e){var n=o[e];n&&(n.disconnect(),o[e]=null,delete o[e])}function t(e,n){return new r(n).observe(i.el(e),{attributes:!0,childList:!0,characterData:!0,subtree:!0})}var o={};this.watch=function(n,o){e(n,t(n,o))},this.unwatch=function(e){n(e)}}var i=t(1),r=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver;e.exports=o},function(e,n,t){var o=t(1);e.exports=function(e){function n(e,n,o,i){function r(e,n){var t,r,c=[];if(a-=1,!a){for(t=0,r=n.rows.length;r>t;t+=1)c.push(n.rows.item(t));i&&i(o?c[0]:c)}}function c(e,n){"undefined"!=typeof console&&console.log("WebSQL error: ",e,n)}var s,u,a;n[0]instanceof Array||(n=[n]),a=n.length,t.transaction(function(t){for(s=0,u=n.length;u>s;s+=1)t.executeSql(e,n[s],r,c)})}var t,i="DOMSnap_DB",r=13,c="Snap";try{t=openDatabase(i,r,i,5242880),n("CREATE TABLE IF NOT EXISTS "+c+"(id INTEGER PRIMARY KEY ASC, create_date TIMESTAMP, selector TEXT, capture_id TEXT, scope TEXT, version INTEGER, htm TEXT)",[],!1,e)}catch(s){console.log("WebSQL error: ",s)}this.add=function(e,t,i,r,s){o.isNil(e)||o.isNil(t)||this["delete"](e,t,r,s,function(){n("INSERT INTO "+c+" (selector, capture_id, htm, scope, version, create_date) VALUES (?, ?, ?, ?, ?, ?);",[e+"",t+"",i+"",r+"",s,new Date],!1)})},this["delete"]=function(e,t,o,i,r){n("DELETE FROM "+c+" WHERE selector = ? AND capture_id = ? AND scope = ? AND version = ?",[e+"",t+"",o+"",i],!1,r)},this.getAll=function(e,t,o){n("SELECT * FROM "+c+" WHERE scope = ? AND version = ?",[e,t],!1,o)},this.deleteAll=function(e,t){n("DELETE FROM "+c+" WHERE scope = ? AND version = ?",[e,t])}}}]);