@reclaimprotocol/browser-extension-sdk
Version:
This guide will walk you through integrating the Reclaim Protocol Browser Extension SDK into your own browser extension or web application. It covers installation, manifest configuration, background initialization, content script setup, and how to trigg
2 lines • 14.8 kB
JavaScript
(()=>{function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function r(e){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?t(Object(o),!0).forEach(function(t){n(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))})}return e}function n(e,t,r){return(t=d(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function o(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,a,s=[],u=!0,c=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;u=!1}else for(;!(u=(n=i.call(r)).done)&&(s.push(n.value),s.length!==t);u=!0);}catch(e){c=!0,o=e}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw o}}return s}}(e,t)||a(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=a(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,o=function(){};return{s:o,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,s=!0,u=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return s=e.done,e},e:function(e){u=!0,i=e},f:function(){try{s||null==r.return||r.return()}finally{if(u)throw i}}}}function a(e,t){if(e){if("string"==typeof e)return s(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?s(e,t):void 0}}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function u(){/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */var e,t,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var u=n&&n.prototype instanceof s?n:s,l=Object.create(u.prototype);return c(l,"_invoke",function(r,n,o){var i,s,u,c=0,l=o||[],f=!1,p={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(t,r){return i=t,s=0,u=e,p.n=r,a}};function d(r,n){for(s=r,u=n,t=0;!f&&c&&!o&&t<l.length;t++){var o,i=l[t],d=p.p,y=i[2];r>3?(o=y===n)&&(u=i[(s=i[4])?5:(s=3,3)],i[4]=i[5]=e):i[0]<=d&&((o=r<2&&d<i[1])?(s=0,p.v=n,p.n=i[1]):d<y&&(o=r<3||i[0]>n||n>y)&&(i[4]=r,i[5]=n,p.n=y,s=0))}if(o||r>1)return a;throw f=!0,n}return function(o,l,y){if(c>1)throw TypeError("Generator is already running");for(f&&1===l&&d(l,y),s=l,u=y;(t=s<2?e:u)||!f;){i||(s?s<3?(s>1&&(p.n=-1),d(s,u)):p.n=u:p.v=u);try{if(c=2,i){if(s||(o="next"),t=i[o]){if(!(t=t.call(i,u)))throw TypeError("iterator result is not an object");if(!t.done)return t;u=t.value,s<2&&(s=0)}else 1===s&&(t=i.return)&&t.call(i),s<2&&(u=TypeError("The iterator does not provide a '"+o+"' method"),s=1);i=e}else if((t=(f=p.n<0)?u:r.call(n,p))!==a)break}catch(t){i=e,s=1,u=t}finally{c=1}}return{value:t,done:f}}}(r,o,i),!0),l}var a={};function s(){}function l(){}function f(){}t=Object.getPrototypeOf;var p=[][n]?t(t([][n]())):(c(t={},n,function(){return this}),t),d=f.prototype=s.prototype=Object.create(p);function y(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,f):(e.__proto__=f,c(e,o,"GeneratorFunction")),e.prototype=Object.create(d),e}return l.prototype=f,c(d,"constructor",f),c(f,"constructor",l),l.displayName="GeneratorFunction",c(f,o,"GeneratorFunction"),c(d),c(d,o,"Generator"),c(d,n,function(){return this}),c(d,"toString",function(){return"[object Generator]"}),(u=function(){return{w:i,m:y}})()}function c(e,t,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}c=function(e,t,r,n){function i(t,r){c(e,t,function(e){return this._invoke(t,r,e)})}t?o?o(e,t,{value:r,enumerable:!n,configurable:!n,writable:!n}):e[t]=r:(i("next",0),i("throw",1),i("return",2))},c(e,t,r,n)}function l(e,t,r,n,o,i,a){try{var s=e[i](a),u=s.value}catch(e){return void r(e)}s.done?t(u):Promise.resolve(u).then(n,o)}function f(e){return function(){var t=this,r=arguments;return new Promise(function(n,o){var i=e.apply(t,r);function a(e){l(i,n,o,a,s,"next",e)}function s(e){l(i,n,o,a,s,"throw",e)}a(void 0)})}}function p(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,d(n.key),n)}}function d(t){var r=function(t,r){if("object"!=e(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,r||"default");if("object"!=e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"==e(r)?r:r+""}!function(){console.log("NETWORK INTERCEPTOR!!! LOADED!!!!");var t,n,a,s;t=function(){for(var e,t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];return(e=console).error.apply(e,["❌ [Error]:"].concat(r))},n=function(){for(var e,t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];return(e=console).info.apply(e,["ℹ️ [Info]:"].concat(r))},a=function(){return a=function e(){var r;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.requestMiddlewares=[],this.responseMiddlewares=[],this.originalFetch=null===(r=window.fetch)||void 0===r?void 0:r.bind(window),this.originalXHR=window.XMLHttpRequest,"undefined"!=typeof window&&this.originalFetch&&this.originalXHR?(this.setupInterceptor(),n("RequestInterceptor initialized")):t("Not in a browser environment or required APIs not available")},s=[{key:"processRequestMiddlewares",value:(y=f(u().m(function e(r){var n;return u().w(function(e){for(;;)switch(e.p=e.n){case 0:return e.p=0,e.n=1,Promise.all(this.requestMiddlewares.map(function(e){return e(r)}));case 1:e.n=3;break;case 2:e.p=2,n=e.v,t("Error in request middleware:",n);case 3:return e.a(2)}},e,this,[[0,2]])})),function(e){return y.apply(this,arguments)})},{key:"processResponseMiddlewares",value:(d=f(u().m(function e(r,n){var o,a,s,c,l,f;return u().w(function(e){for(;;)switch(e.p=e.n){case 0:return e.n=1,this.parseResponse(r);case 1:o=e.v,a=i(this.responseMiddlewares),e.p=2,a.s();case 3:if((s=a.n()).done){e.n=8;break}return c=s.value,e.p=4,e.n=5,c(o,n);case 5:e.n=7;break;case 6:e.p=6,l=e.v,t("Error in response middleware:",l);case 7:e.n=3;break;case 8:e.n=10;break;case 9:e.p=9,f=e.v,a.e(f);case 10:return e.p=10,a.f(),e.f(10);case 11:return e.a(2)}},e,this,[[4,6],[2,9,10,11]])})),function(e,t){return d.apply(this,arguments)})},{key:"parseResponse",value:(l=f(u().m(function e(r){var n,o,i;return u().w(function(e){for(;;)switch(e.p=e.n){case 0:return n=r.clone(),e.p=1,e.n=2,n.text();case 2:o=e.v,e.n=4;break;case 3:e.p=3,i=e.v,t("Error parsing response:",i),o="Could not read response body";case 4:return e.a(2,{url:r.url,status:r.status,statusText:r.statusText,headers:Object.fromEntries(r.headers.entries()),body:o,originalResponse:r})}},e,null,[[1,3]])})),function(e){return l.apply(this,arguments)})},{key:"setupInterceptor",value:function(){var n,i=this.originalFetch,a=this;window.fetch=new Proxy(i,{apply:(n=f(u().m(function e(n,i,s){var c,l,f,p,d,y,h;return u().w(function(e){for(;;)switch(e.p=e.n){case 0:if(c=o(s,2),l=c[0],f=c[1],p=void 0===f?{}:f,l){e.n=1;break}return e.a(2,Reflect.apply(n,i,s));case 1:return d={url:l,options:r(r({},p),{},{method:p.method||"GET",headers:p.headers||{}})},Object.defineProperty(d,"_rc",{value:!0,enumerable:!1,configurable:!1,writable:!1}),e.p=2,e.n=3,Promise.all(a.requestMiddlewares.map(function(e){return e(d)}));case 3:e.n=5;break;case 4:e.p=4,h=e.v,t("Error in request middleware:",h);case 5:return e.n=6,Reflect.apply(n,i,[d.url,d.options]);case 6:if(!(y=e.v)._rc)try{Object.defineProperty(y,"_rc",{value:!0,enumerable:!1,configurable:!1,writable:!1})}catch(e){t("Could not mark response:",e)}return a.processResponseMiddlewares(y.clone(),d).catch(function(e){t("Error in response middleware:",e)}),e.a(2,y)}},e,null,[[2,4]])})),function(e,t,r){return n.apply(this,arguments)})});var s=XMLHttpRequest.prototype.open,c=XMLHttpRequest.prototype.send,l=XMLHttpRequest.prototype.setRequestHeader,p=new WeakMap;XMLHttpRequest.prototype.open=function(){Object.defineProperty(this,"_rc",{value:!0,enumerable:!1,configurable:!1,writable:!1});for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];var n=t[0],o=void 0===n?"GET":n,i=t[1],a={url:void 0===i?"":i,options:{method:o,headers:{},body:null}};return p.set(this,a),s.apply(this,t)},XMLHttpRequest.prototype.setRequestHeader=function(e,t){var r=p.get(this);return r&&e&&t&&(r.options.headers[e]=t),l.apply(this,arguments)},XMLHttpRequest.prototype.send=function(r){var n=this,o=p.get(this);if(o){o.options.body=r;var i=function(){var e=f(u().m(function e(){var r;return u().w(function(e){for(;;)switch(e.p=e.n){case 0:return e.p=0,e.n=1,Promise.all(a.requestMiddlewares.map(function(e){return e(o)}));case 1:e.n=3;break;case 2:e.p=2,r=e.v,t("Error in request middleware:",r);case 3:return e.a(2)}},e,null,[[0,2]])}));return function(){return e.apply(this,arguments)}}(),s=this.onreadystatechange;this.onreadystatechange=function(r){if("function"==typeof s&&s.apply(this,arguments),4===this.readyState){var n=this.status||500,i=this.statusText||"Request Failed";try{var u=new Response(function(r){if(null==r)return"";switch(e(r)){case"string":return r;case"object":if(r instanceof Blob||r instanceof ArrayBuffer)return"[Binary Data]";if(r instanceof Document)return r.documentElement.outerHTML;try{return JSON.stringify(r)}catch(e){return t("Failed to stringify object response:",e),String(r)}default:return String(r)}}(this.response),{status:n,statusText:i,headers:new Headers(Object.fromEntries((this.getAllResponseHeaders()||"").split("\r\n").filter(Boolean).map(function(e){return e.split(": ")})))});Object.defineProperty(u,"url",{value:o.url,writable:!1}),a.processResponseMiddlewares(u,o).catch(function(e){return t("Error in response middleware:",e)})}catch(e){t("Error processing XHR response:",e)}}},i().then(function(){c.call(n,o.options.body)})}else c.apply(this,arguments)},this.resetXHRInterceptor=function(){XMLHttpRequest.prototype.open=s,XMLHttpRequest.prototype.send=c,XMLHttpRequest.prototype.setRequestHeader=l}}},{key:"addRequestMiddleware",value:function(e){"function"==typeof e&&this.requestMiddlewares.push(e)}},{key:"addResponseMiddleware",value:function(e){"function"==typeof e&&this.responseMiddlewares.push(e)}}],s&&p(a.prototype,s),c&&p(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a;var a,s,c,l,d,y}(),(s=new a).addRequestMiddleware(function(){var r=f(u().m(function r(n){var o,i,a,s,c;return u().w(function(r){for(;;)switch(r.n){case 0:try{o={};try{n.options.headers&&(n.options.headers instanceof Headers?o=Object.fromEntries(n.options.headers.entries()):"object"===e(n.options.headers)&&Object.keys(n.options.headers).forEach(function(e){var t=n.options.headers[e];"string"!=typeof t&&"number"!=typeof t||(o[e]=String(t))}))}catch(e){t("Error extracting headers:",e)}i=null;try{n.options.body&&("string"==typeof n.options.body?i=n.options.body:"object"===e(n.options.body)&&(i=JSON.stringify(n.options.body)))}catch(e){t("Error extracting body:",e)}a="";try{"string"==typeof n.url?a=n.url.startsWith("http")?n.url:new URL(n.url,window.location.origin).href:n.url&&"object"===e(n.url)&&n.url.url?a=n.url.url:n.url&&"object"===e(n.url)&&n.url.href?a=n.url.href:(a=String(n.url)).startsWith("http")||(a=new URL(a,window.location.origin).href)}catch(e){t("Error extracting URL:",e),a=window.location.href}s={url:a,method:"string"==typeof n.options.method?n.options.method:"GET",headers:o,body:i},window.postMessage({action:"INTERCEPTED_REQUEST",data:s},"*")}catch(r){t("Error posting request data:",r),c="";try{c="string"==typeof n.url?n.url.startsWith("http")?n.url:new URL(n.url,window.location.origin).href:n.url&&"object"===e(n.url)&&n.url.url?n.url.url:n.url&&"object"===e(n.url)&&n.url.href?n.url.href:window.location.href}catch(e){c=window.location.href}window.postMessage({action:"INTERCEPTED_REQUEST",data:{url:c,method:"string"==typeof n.options.method?n.options.method:"GET",headers:{},body:null}},"*")}case 1:return r.a(2)}},r)}));return function(e){return r.apply(this,arguments)}}()),s.addResponseMiddleware(function(){var r=f(u().m(function r(n,o){var i,a,s,c,l;return u().w(function(r){for(;;)switch(r.n){case 0:try{i={};try{n.headers&&(n.headers instanceof Headers?i=Object.fromEntries(n.headers.entries()):"object"===e(n.headers)&&Object.keys(n.headers).forEach(function(e){var t=n.headers[e];"string"!=typeof t&&"number"!=typeof t||(i[e]=String(t))}))}catch(e){t("Error extracting headers:",e)}a=null;try{n.body&&("string"==typeof n.body?a=n.body:"object"===e(n.body)&&(a=JSON.stringify(n.body)))}catch(e){t("Error extracting body:",e)}s="";try{"string"==typeof o.url?s=o.url.startsWith("http")?o.url:new URL(o.url,window.location.origin).href:o.url&&"object"===e(o.url)&&o.url.url?s=o.url.url:o.url&&"object"===e(o.url)&&o.url.href?s=o.url.href:(s=String(o.url)).startsWith("http")||(s=new URL(s,window.location.origin).href)}catch(e){t("Error extracting URL:",e),s=window.location.href}c={url:s,status:n.status,headers:i,body:a},window.postMessage({action:"INTERCEPTED_RESPONSE",data:c},"*")}catch(r){t("Error posting response data:",r),l="";try{l="string"==typeof o.url?o.url.startsWith("http")?o.url:new URL(o.url,window.location.origin).href:o.url&&"object"===e(o.url)&&o.url.url?o.url.url:o.url&&"object"===e(o.url)&&o.url.href?o.url.href:window.location.href}catch(e){l=window.location.href}window.postMessage({action:"INTERCEPTED_RESPONSE",data:{url:l,status:n.status,headers:{},body:null}},"*")}case 1:return r.a(2)}},r)}));return function(e,t){return r.apply(this,arguments)}}()),window.reclaimInterceptor=s,n("Userscript initialized and ready - Access via window.reclaimInterceptor")}()})();
//# sourceMappingURL=network-interceptor.bundle.js.map