@bringweb3/chrome-extension-kit 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -8
- package/dist/index.d.mts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ When a user visits supported online retailer websites, the Crypto Cashback syste
|
|
|
28
28
|
## Prerequisites
|
|
29
29
|
|
|
30
30
|
- Node.js >= 14
|
|
31
|
-
- Chrome extension manifest >=
|
|
31
|
+
- Chrome extension manifest >= V2 with required permissions
|
|
32
32
|
- Obtain an identifier key from [Bringweb3](https://bringweb3.io/#contact)
|
|
33
33
|
- Provide a specific logo for the specific outlet
|
|
34
34
|
|
|
@@ -120,7 +120,10 @@ bringInitContentScript({
|
|
|
120
120
|
getWalletAddress: async () => await new Promise(resolve => setTimeout(() => resolve('<USER_WALLET_ADDRESS>'), 200)),// Async function that returns the current user's wallet address
|
|
121
121
|
promptLogin: () => {...}, // Function that prompts a UI element asking the user to login
|
|
122
122
|
walletAddressListeners: ["customEvent:addressChanged"], // A list of custom events that dispatched when the user's wallet address had changed
|
|
123
|
-
|
|
123
|
+
themeMode: 'light' // 'light' | 'dark',
|
|
124
|
+
text:'lower' // 'lower' | 'upper'
|
|
125
|
+
darkTheme: {...}, // Same as lightTheme
|
|
126
|
+
lightTheme: {
|
|
124
127
|
// font
|
|
125
128
|
fontUrl: 'https://fonts.googleapis.com/css2?family=Matemasie&display=swap',
|
|
126
129
|
fontFamily: "'Matemasie', system-ui",
|
|
@@ -195,6 +198,7 @@ bringInitContentScript({
|
|
|
195
198
|
tokenFC: "#DADCE5",
|
|
196
199
|
tokenBorderW: "2px",
|
|
197
200
|
tokenBorderC: "#DADCE5",
|
|
201
|
+
tokenRadius: "8px",
|
|
198
202
|
// Notification popup
|
|
199
203
|
notificationFS: "14px",
|
|
200
204
|
notificationFW: "500",
|
|
@@ -206,12 +210,12 @@ bringInitContentScript({
|
|
|
206
210
|
notificationBtnBorderW: "0",
|
|
207
211
|
notificationBtnBorderC: "transparent",
|
|
208
212
|
notificationBtnRadius: "8px",
|
|
209
|
-
activateTitleFS: "
|
|
210
|
-
activateTitleFW: "
|
|
211
|
-
activateTitleFC: "
|
|
212
|
-
activateTitleBoldFS: "
|
|
213
|
-
activateTitleBoldFW: "
|
|
214
|
-
activateTitleBoldFC: "
|
|
213
|
+
activateTitleFS: "14px",
|
|
214
|
+
activateTitleFW: "600",
|
|
215
|
+
activateTitleFC: "white",
|
|
216
|
+
activateTitleBoldFS: "14px",
|
|
217
|
+
activateTitleBoldFW: "700",
|
|
218
|
+
activateTitleBoldFC: "white",
|
|
215
219
|
}
|
|
216
220
|
});
|
|
217
221
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -20,7 +20,7 @@ interface Configuration$1 {
|
|
|
20
20
|
* @param {Object} [configuration.lightTheme] - Optional light theme settings.
|
|
21
21
|
* @param {Object} [configuration.darkTheme] - Optional dark theme settings.
|
|
22
22
|
* @param {string} configuration.theme - The chosen theme, light | dark.
|
|
23
|
-
* @param {string} configuration.
|
|
23
|
+
* @param {string} configuration.text - The chosen case for some of the texts, upper | lower.
|
|
24
24
|
* @throws {Error} Throws an error if any required configuration is missing.
|
|
25
25
|
* @returns {Promise<void>}
|
|
26
26
|
*
|
|
@@ -34,10 +34,13 @@ interface Configuration$1 {
|
|
|
34
34
|
* getWalletAddress: async () => '0x1234...',
|
|
35
35
|
* promptLogin: () => { ... },
|
|
36
36
|
* walletAddressListeners: ["listener1", "listener2"],
|
|
37
|
-
*
|
|
37
|
+
* theme: 'light',
|
|
38
|
+
* text: 'lower',
|
|
39
|
+
* lightTheme: { ... },
|
|
40
|
+
* darkTheme: { ... }
|
|
38
41
|
* });
|
|
39
42
|
*/
|
|
40
|
-
declare const bringInitContentScript: ({ getWalletAddress, promptLogin, walletAddressListeners, lightTheme, darkTheme, theme, text,
|
|
43
|
+
declare const bringInitContentScript: ({ getWalletAddress, promptLogin, walletAddressListeners, lightTheme, darkTheme, theme, text, }: Configuration$1) => Promise<void>;
|
|
41
44
|
|
|
42
45
|
interface Configuration {
|
|
43
46
|
identifier: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface Configuration$1 {
|
|
|
20
20
|
* @param {Object} [configuration.lightTheme] - Optional light theme settings.
|
|
21
21
|
* @param {Object} [configuration.darkTheme] - Optional dark theme settings.
|
|
22
22
|
* @param {string} configuration.theme - The chosen theme, light | dark.
|
|
23
|
-
* @param {string} configuration.
|
|
23
|
+
* @param {string} configuration.text - The chosen case for some of the texts, upper | lower.
|
|
24
24
|
* @throws {Error} Throws an error if any required configuration is missing.
|
|
25
25
|
* @returns {Promise<void>}
|
|
26
26
|
*
|
|
@@ -34,10 +34,13 @@ interface Configuration$1 {
|
|
|
34
34
|
* getWalletAddress: async () => '0x1234...',
|
|
35
35
|
* promptLogin: () => { ... },
|
|
36
36
|
* walletAddressListeners: ["listener1", "listener2"],
|
|
37
|
-
*
|
|
37
|
+
* theme: 'light',
|
|
38
|
+
* text: 'lower',
|
|
39
|
+
* lightTheme: { ... },
|
|
40
|
+
* darkTheme: { ... }
|
|
38
41
|
* });
|
|
39
42
|
*/
|
|
40
|
-
declare const bringInitContentScript: ({ getWalletAddress, promptLogin, walletAddressListeners, lightTheme, darkTheme, theme, text,
|
|
43
|
+
declare const bringInitContentScript: ({ getWalletAddress, promptLogin, walletAddressListeners, lightTheme, darkTheme, theme, text, }: Configuration$1) => Promise<void>;
|
|
41
44
|
|
|
42
45
|
interface Configuration {
|
|
43
46
|
identifier: string;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var w=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var K=Object.prototype.hasOwnProperty;var B=(e,t)=>{for(var n in t)w(e,n,{get:t[n],enumerable:!0})},J=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of V(t))!K.call(e,s)&&s!==n&&w(e,s,{get:()=>t[s],enumerable:!(r=F(t,s))||r.enumerable});return e};var Q=e=>J(w({},"__esModule",{value:!0}),e);var ge={};B(ge,{bringInitBackground:()=>q,bringInitContentScript:()=>R});module.exports=Q(ge);var G=e=>{let t=new URLSearchParams,{query:n,prefix:r}=e;return Object.entries(n).forEach(([s,i])=>{i&&(r&&(s=`${r}_${s}`),s==="url"?t.append(s,encodeURIComponent(i)):t.append(s,i))}),t.toString()},E=G;var D={name:"@bringweb3/chrome-extension-kit",version:"1.1.2",description:"Crypto cashback integration kit for crypto outlets extension",main:"dist/index.js",module:"dist/index.mjs",types:"dist/index.d.ts",license:"MIT",private:!1,publishConfig:{access:"public"},scripts:{watch:"tsup index.ts --format cjs,esm --dts --watch",build:"tsup index.ts --format cjs,esm --dts --minify",lint:"tsc",test:"vitest --coverage",release:"yarn build && changeset publish"},files:["dist","README.md"],devDependencies:{"@changesets/cli":"^2.27.7","@types/chrome":"^0.0.268","@vitest/coverage-v8":"^2.0.5",tsup:"^8.2.1",typescript:"^5.5.3",vitest:"^2.0.5"}};var Y=()=>D.version,k=Y;var z=({query:e,theme:t,themeMode:n,text:r,iframeUrl:s})=>{let i=chrome.runtime.id,o=`bringweb3-iframe-${i}`,a=document.getElementById(o);if(a)return a;let m=E({query:{...e,extensionId:i,v:k(),themeMode:n,textMode:r}}),l=t?`&${E({query:t,prefix:"t"})}`:"",c=document.createElement("iframe");return c.id=o,c.src=`${s}?${m}${l}`,c.setAttribute("sandbox","allow-popups allow-scripts allow-same-origin allow-top-navigation-by-user-activation"),c.style.position="fixed",c.scrolling="no",c.style.overflow="hidden",c.style.width="1px",c.style.height="1px",c.style.right="8px",c.style.borderRadius="10px",c.style.border="none",c.style.cssText+="z-index: 99999999999999 !important;",t?.popupShadow&&(c.style.boxShadow=t.popupShadow),document.documentElement.appendChild(c),c},v=z;var X=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([n,r])=>{n in e.style&&(e.style[n]=r)})},C=X;var Z=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let n=t.sheet;n?e.forEach(({name:r,rules:s})=>{n.insertRule(`@keyframes ${r} { ${s} }`,n.cssRules.length)}):console.error("Failed to create stylesheet")},P=Z;var p={OPEN:"OPEN",CLOSE:"CLOSE",ACTIVATE:"ACTIVATE",PROMPT_LOGIN:"PROMPT_LOGIN",OPT_OUT:"OPT_OUT",ADD_KEYFRAMES:"ADD_KEYFRAMES"},ee=[p.ACTIVATE],te=({event:e,iframeEl:t,promptLogin:n})=>{let{data:r}=e,{from:s,action:i,style:o,keyFrames:a,time:m,extensionId:l}=r;if(s==="bringweb3"&&!(l!==chrome.runtime.id&&!ee.includes(i)))switch(i){case p.OPEN:C(t,o);break;case p.CLOSE:t&&t.parentNode?.removeChild(t),m&&chrome.runtime.sendMessage({action:i,time:m,from:"bringweb3"});break;case p.PROMPT_LOGIN:n();break;case p.ACTIVATE:chrome.runtime.sendMessage({action:i,from:"bringweb3"});break;case p.OPT_OUT:chrome.runtime.sendMessage({action:i,time:m,from:"bringweb3"});break;case p.ADD_KEYFRAMES:P(a);break;default:break}},S=te;var ne=({walletAddressListeners:e,getWalletAddress:t,iframeEl:n})=>{for(let r=0;r<e.length;r++){let s=e[r];s&&window.addEventListener(s,async i=>{if(!n&&(n=document.querySelector(`#bringweb3-iframe-${chrome.runtime.id}`),!n)||!n.contentWindow)return;let o=await t();n.contentWindow.postMessage({action:"WALLET_ADDRESS_UPDATE",walletAddress:o},"*"),chrome.runtime.sendMessage({action:"WALLET_ADDRESS_UPDATE",from:"bringweb3",walletAddress:"address"})})}},L=ne;var y=null,M=!1,re=async({getWalletAddress:e,promptLogin:t,walletAddressListeners:n,lightTheme:r,darkTheme:s,theme:i,text:o})=>{if(!e||!t||!n?.length)throw new Error("Missing configuration");L({walletAddressListeners:n,getWalletAddress:e,iframeEl:y}),window.addEventListener("message",a=>S({event:a,iframeEl:y,promptLogin:t})),chrome.runtime.onMessage.addListener((a,m,l)=>{let{action:c}=a;switch(c){case"GET_WALLET_ADDRESS":return e().then(g=>l({status:"success",walletAddress:g})).catch(g=>l({status:"success",walletAddress:void 0})),!0;case"INJECT":if(M)return;let{token:h,iframeUrl:f}=a;return console.log({iframeUrl:f}),y=v({query:{token:h},iframeUrl:f,theme:i==="dark"?s:r,themeMode:i||"light",text:o}),M=!0,l({status:"success"}),!0;default:console.error(`Unknown action: ${c}`);break}})},R=re;var u=class e{static instance=null;apiEndpoint="";constructor(){}static getInstance(){return e.instance||(e.instance=new e),e.instance}setApiEndpoint(t){this.apiEndpoint=t==="prod"?"https://api.bringweb3.io/v1/extension":"https://sandbox-api.bringweb3.io/v1/extension"}getApiEndpoint(){if(!this.apiEndpoint)throw new Error("API endpoint not set. Call setApiEndpoint first.");return this.apiEndpoint}};var se=async e=>{let t=u.getInstance().getApiEndpoint();return await(await fetch(`${t}/domains`,{headers:{"x-api-key":e}})).json()},O=se;var ie=async({apiKey:e,body:t})=>{let n=u.getInstance().getApiEndpoint();return await(await fetch(`${n}/check/popup`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(t)})).json()},I=ie;var oe=async({apiKey:e,walletAddress:t,cashbackUrl:n})=>{let r=u.getInstance().getApiEndpoint(),s={walletAddress:t};return n&&(s.cashbackUrl=n),await(await fetch(`${r}/check/notification`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(s)})).json()},_=oe;var A="updateCache";var ae=async(e,t)=>new Promise((n,r)=>{chrome.storage.local.set({[`bring_${e}`]:t},()=>{chrome.runtime.lastError?r(chrome.runtime.lastError):n()})}),ce=async e=>new Promise((t,n)=>{chrome.storage.local.get([`bring_${e}`],r=>{chrome.runtime.lastError?n(chrome.runtime.lastError):t(r[`bring_${e}`])})}),de=async e=>new Promise((t,n)=>{chrome.storage.local.remove([`bring_${e}`],()=>{chrome.runtime.lastError?n(chrome.runtime.lastError):t()})}),d={set:ae,get:ce,remove:de};var U=30*60*1e3,$=async e=>{let t=await d.get("walletAddress");try{if(!e){let r=await chrome.tabs.query({active:!0,currentWindow:!0});if(!r||!r[0]||!r[0].id)return t;e=r[0].id}let n=await x(e,{action:"GET_WALLET_ADDRESS"});n?.walletAddress&&t!==n?.walletAddress&&(t=n?.walletAddress,await d.set("walletAddress",t))}catch{}return t},le=e=>{let t=Date.now();return(e-t)/1e3/60},T=async e=>{let t=await O(e);d.set("relevantDomains",t.relevantDomains);let{nextUpdateTimestamp:n}=t,r=le(n);return chrome.alarms.create(A,{delayInMinutes:r||60*24*2}),t.relevantDomains},me=async(e,t,n,r)=>{let s={showNotification:!1,token:"",iframeUrl:""},i=await d.get("notificationCheck");if(i?.check&&Date.now()<i.check)return s;let o=await $(t);if(!o)return s;let a=await _({apiKey:e,walletAddress:o,cashbackUrl:n}),m={check:r?a.nextRequestTimestampActivated:a.nextRequestTimestampRegular,nextRequestTimestampActivated:a.nextRequestTimestampActivated,nextRequestTimestampRegular:a.nextRequestTimestampRegular};return d.set("notificationCheck",m),{showNotification:a.showNotification,token:a.token,iframeUrl:a.iframeUrl}},ue=e=>e.replace(/^(https?:\/\/)?(www\.)?/,""),N=async(e,t)=>{let n=await d.get("relevantDomains");if(n===void 0&&(n=await T(t)),!e||!n||!n.length)return"";let r=ue(e);for(let s of n)if(r.startsWith(s)){let i=await d.get("quietDomains");return i&&i[s]&&Date.now()<i[s]?"":s}return""},j=async(e,t)=>{t||(t=U);let n=await d.get("quietDomains");typeof n=="object"?n[e]=Date.now()+t:n={[e]:Date.now()+U},d.set("quietDomains",n)},pe=e=>e?chrome.runtime.getURL(e):void 0,x=(e,t)=>new Promise((s,i)=>{let o=a=>{chrome.tabs.get(e,m=>{if(chrome.runtime.lastError){i(new Error(chrome.runtime.lastError.message));return}chrome.tabs.sendMessage(e,t,l=>{chrome.runtime.lastError?a<4?setTimeout(()=>o(a+1),1e3*Math.pow(2,a)):i(new Error(chrome.runtime.lastError.message)):s(l)})})};o(0)}),W=async(e,t,n)=>{let r=await me(e,t,pe(n));r.showNotification&&await x(t,{action:"INJECT",token:r.token,iframeUrl:r.iframeUrl})},b={},fe=async({identifier:e,apiEndpoint:t,cashbackPagePath:n})=>{if(!e||!t)throw new Error("Missing configuration");if(!["prod","sandbox"].includes(t))throw new Error("unknown apiEndpoint");u.getInstance().setApiEndpoint(t),T(e),chrome.alarms.onAlarm.addListener(async r=>{let{name:s}=r;switch(s){case A:T(e);break;default:console.error("alarm with no use case:",s);break}}),chrome.runtime.onMessage.addListener(async(r,s)=>{if(r?.from!=="bringweb3")return;let{action:i}=r;switch(i){case"ACTIVATE":{let o=await d.get("notificationCheck");if(!o.check||!o.nextRequestTimestampActivated)break;o.check=o.nextRequestTimestampActivated,d.set("notificationCheck",o);break}case"OPT_OUT":{let{time:o}=r;d.set("optOut",Date.now()+o);break}case"CLOSE":{let{time:o}=r,a=await N(s.tab?.url||s.origin,e);if(!a)break;j(a,o);break}case"WALLET_ADDRESS_UPDATE":{let{walletAddress:o}=r;d.set("walletAddress",o);break}default:{console.warn(`Bring unknown action: ${i}`);break}}}),chrome.tabs.onUpdated.addListener(async(r,s,i)=>{let o=await d.get("optOut");if(o&&o>Date.now()){await W(e,r,n);return}if(!i.url)return;let a=i.url.replace("www.",""),m=b[r];if(s.status!=="complete"||a===m)return;b[r]=a;let l=await N(i.url,e);if(!l||!l.length){await W(e,r,n);return}let c=await $(r),{token:h,isValid:f,iframeUrl:g}=await I({apiKey:e,body:{domain:l,url:i.url,address:c}});if(!f){f===!1&&j(l);return}x(r,{action:"INJECT",token:h,domain:a,iframeUrl:g})}),chrome.tabs.onRemoved.addListener(r=>delete b[r])},q=fe;0&&(module.exports={bringInitBackground,bringInitContentScript});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var W=e=>{let t=new URLSearchParams,{query:n,prefix:r}=e;return Object.entries(n).forEach(([s,i])=>{i&&(r&&(s=`${r}_${s}`),s==="url"?t.append(s,encodeURIComponent(i)):t.append(s,i))}),t.toString()},w=W;var x={name:"@bringweb3/chrome-extension-kit",version:"1.1.2",description:"Crypto cashback integration kit for crypto outlets extension",main:"dist/index.js",module:"dist/index.mjs",types:"dist/index.d.ts",license:"MIT",private:!1,publishConfig:{access:"public"},scripts:{watch:"tsup index.ts --format cjs,esm --dts --watch",build:"tsup index.ts --format cjs,esm --dts --minify",lint:"tsc",test:"vitest --coverage",release:"yarn build && changeset publish"},files:["dist","README.md"],devDependencies:{"@changesets/cli":"^2.27.7","@types/chrome":"^0.0.268","@vitest/coverage-v8":"^2.0.5",tsup:"^8.2.1",typescript:"^5.5.3",vitest:"^2.0.5"}};var q=()=>x.version,D=q;var F=({query:e,theme:t,themeMode:n,text:r,iframeUrl:s})=>{let i=chrome.runtime.id,o=`bringweb3-iframe-${i}`,a=document.getElementById(o);if(a)return a;let m=w({query:{...e,extensionId:i,v:D(),themeMode:n,textMode:r}}),l=t?`&${w({query:t,prefix:"t"})}`:"",c=document.createElement("iframe");return c.id=o,c.src=`${s}?${m}${l}`,c.setAttribute("sandbox","allow-popups allow-scripts allow-same-origin allow-top-navigation-by-user-activation"),c.style.position="fixed",c.scrolling="no",c.style.overflow="hidden",c.style.width="1px",c.style.height="1px",c.style.right="8px",c.style.borderRadius="10px",c.style.border="none",c.style.cssText+="z-index: 99999999999999 !important;",t?.popupShadow&&(c.style.boxShadow=t.popupShadow),document.documentElement.appendChild(c),c},k=F;var V=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([n,r])=>{n in e.style&&(e.style[n]=r)})},v=V;var K=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let n=t.sheet;n?e.forEach(({name:r,rules:s})=>{n.insertRule(`@keyframes ${r} { ${s} }`,n.cssRules.length)}):console.error("Failed to create stylesheet")},C=K;var p={OPEN:"OPEN",CLOSE:"CLOSE",ACTIVATE:"ACTIVATE",PROMPT_LOGIN:"PROMPT_LOGIN",OPT_OUT:"OPT_OUT",ADD_KEYFRAMES:"ADD_KEYFRAMES"},B=[p.ACTIVATE],J=({event:e,iframeEl:t,promptLogin:n})=>{let{data:r}=e,{from:s,action:i,style:o,keyFrames:a,time:m,extensionId:l}=r;if(s==="bringweb3"&&!(l!==chrome.runtime.id&&!B.includes(i)))switch(i){case p.OPEN:v(t,o);break;case p.CLOSE:t&&t.parentNode?.removeChild(t),m&&chrome.runtime.sendMessage({action:i,time:m,from:"bringweb3"});break;case p.PROMPT_LOGIN:n();break;case p.ACTIVATE:chrome.runtime.sendMessage({action:i,from:"bringweb3"});break;case p.OPT_OUT:chrome.runtime.sendMessage({action:i,time:m,from:"bringweb3"});break;case p.ADD_KEYFRAMES:C(a);break;default:break}},P=J;var Q=({walletAddressListeners:e,getWalletAddress:t,iframeEl:n})=>{for(let r=0;r<e.length;r++){let s=e[r];s&&window.addEventListener(s,async i=>{if(!n&&(n=document.querySelector(`#bringweb3-iframe-${chrome.runtime.id}`),!n)||!n.contentWindow)return;let o=await t();n.contentWindow.postMessage({action:"WALLET_ADDRESS_UPDATE",walletAddress:o},"*"),chrome.runtime.sendMessage({action:"WALLET_ADDRESS_UPDATE",from:"bringweb3",walletAddress:"address"})})}},S=Q;var E=null,L=!1,G=async({getWalletAddress:e,promptLogin:t,walletAddressListeners:n,lightTheme:r,darkTheme:s,theme:i,text:o})=>{if(!e||!t||!n?.length)throw new Error("Missing configuration");S({walletAddressListeners:n,getWalletAddress:e,iframeEl:E}),window.addEventListener("message",a=>P({event:a,iframeEl:E,promptLogin:t})),chrome.runtime.onMessage.addListener((a,m,l)=>{let{action:c}=a;switch(c){case"GET_WALLET_ADDRESS":return e().then(g=>l({status:"success",walletAddress:g})).catch(g=>l({status:"success",walletAddress:void 0})),!0;case"INJECT":if(L)return;let{token:h,iframeUrl:f}=a;return console.log({iframeUrl:f}),E=k({query:{token:h},iframeUrl:f,theme:i==="dark"?s:r,themeMode:i||"light",text:o}),L=!0,l({status:"success"}),!0;default:console.error(`Unknown action: ${c}`);break}})},H=G;var u=class e{static instance=null;apiEndpoint="";constructor(){}static getInstance(){return e.instance||(e.instance=new e),e.instance}setApiEndpoint(t){this.apiEndpoint=t==="prod"?"https://api.bringweb3.io/v1/extension":"https://sandbox-api.bringweb3.io/v1/extension"}getApiEndpoint(){if(!this.apiEndpoint)throw new Error("API endpoint not set. Call setApiEndpoint first.");return this.apiEndpoint}};var Y=async e=>{let t=u.getInstance().getApiEndpoint();return await(await fetch(`${t}/domains`,{headers:{"x-api-key":e}})).json()},M=Y;var z=async({apiKey:e,body:t})=>{let n=u.getInstance().getApiEndpoint();return await(await fetch(`${n}/check/popup`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(t)})).json()},R=z;var X=async({apiKey:e,walletAddress:t,cashbackUrl:n})=>{let r=u.getInstance().getApiEndpoint(),s={walletAddress:t};return n&&(s.cashbackUrl=n),await(await fetch(`${r}/check/notification`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(s)})).json()},O=X;var y="updateCache";var Z=async(e,t)=>new Promise((n,r)=>{chrome.storage.local.set({[`bring_${e}`]:t},()=>{chrome.runtime.lastError?r(chrome.runtime.lastError):n()})}),ee=async e=>new Promise((t,n)=>{chrome.storage.local.get([`bring_${e}`],r=>{chrome.runtime.lastError?n(chrome.runtime.lastError):t(r[`bring_${e}`])})}),te=async e=>new Promise((t,n)=>{chrome.storage.local.remove([`bring_${e}`],()=>{chrome.runtime.lastError?n(chrome.runtime.lastError):t()})}),d={set:Z,get:ee,remove:te};var I=30*60*1e3,j=async e=>{let t=await d.get("walletAddress");try{if(!e){let r=await chrome.tabs.query({active:!0,currentWindow:!0});if(!r||!r[0]||!r[0].id)return t;e=r[0].id}let n=await T(e,{action:"GET_WALLET_ADDRESS"});n?.walletAddress&&t!==n?.walletAddress&&(t=n?.walletAddress,await d.set("walletAddress",t))}catch{}return t},ne=e=>{let t=Date.now();return(e-t)/1e3/60},b=async e=>{let t=await M(e);d.set("relevantDomains",t.relevantDomains);let{nextUpdateTimestamp:n}=t,r=ne(n);return chrome.alarms.create(y,{delayInMinutes:r||60*24*2}),t.relevantDomains},re=async(e,t,n,r)=>{let s={showNotification:!1,token:"",iframeUrl:""},i=await d.get("notificationCheck");if(i?.check&&Date.now()<i.check)return s;let o=await j(t);if(!o)return s;let a=await O({apiKey:e,walletAddress:o,cashbackUrl:n}),m={check:r?a.nextRequestTimestampActivated:a.nextRequestTimestampRegular,nextRequestTimestampActivated:a.nextRequestTimestampActivated,nextRequestTimestampRegular:a.nextRequestTimestampRegular};return d.set("notificationCheck",m),{showNotification:a.showNotification,token:a.token,iframeUrl:a.iframeUrl}},se=e=>e.replace(/^(https?:\/\/)?(www\.)?/,""),_=async(e,t)=>{let n=await d.get("relevantDomains");if(n===void 0&&(n=await b(t)),!e||!n||!n.length)return"";let r=se(e);for(let s of n)if(r.startsWith(s)){let i=await d.get("quietDomains");return i&&i[s]&&Date.now()<i[s]?"":s}return""},U=async(e,t)=>{t||(t=I);let n=await d.get("quietDomains");typeof n=="object"?n[e]=Date.now()+t:n={[e]:Date.now()+I},d.set("quietDomains",n)},ie=e=>e?chrome.runtime.getURL(e):void 0,T=(e,t)=>new Promise((s,i)=>{let o=a=>{chrome.tabs.get(e,m=>{if(chrome.runtime.lastError){i(new Error(chrome.runtime.lastError.message));return}chrome.tabs.sendMessage(e,t,l=>{chrome.runtime.lastError?a<4?setTimeout(()=>o(a+1),1e3*Math.pow(2,a)):i(new Error(chrome.runtime.lastError.message)):s(l)})})};o(0)}),N=async(e,t,n)=>{let r=await re(e,t,ie(n));r.showNotification&&await T(t,{action:"INJECT",token:r.token,iframeUrl:r.iframeUrl})},A={},oe=async({identifier:e,apiEndpoint:t,cashbackPagePath:n})=>{if(!e||!t)throw new Error("Missing configuration");if(!["prod","sandbox"].includes(t))throw new Error("unknown apiEndpoint");u.getInstance().setApiEndpoint(t),b(e),chrome.alarms.onAlarm.addListener(async r=>{let{name:s}=r;switch(s){case y:b(e);break;default:console.error("alarm with no use case:",s);break}}),chrome.runtime.onMessage.addListener(async(r,s)=>{if(r?.from!=="bringweb3")return;let{action:i}=r;switch(i){case"ACTIVATE":{let o=await d.get("notificationCheck");if(!o.check||!o.nextRequestTimestampActivated)break;o.check=o.nextRequestTimestampActivated,d.set("notificationCheck",o);break}case"OPT_OUT":{let{time:o}=r;d.set("optOut",Date.now()+o);break}case"CLOSE":{let{time:o}=r,a=await _(s.tab?.url||s.origin,e);if(!a)break;U(a,o);break}case"WALLET_ADDRESS_UPDATE":{let{walletAddress:o}=r;d.set("walletAddress",o);break}default:{console.warn(`Bring unknown action: ${i}`);break}}}),chrome.tabs.onUpdated.addListener(async(r,s,i)=>{let o=await d.get("optOut");if(o&&o>Date.now()){await N(e,r,n);return}if(!i.url)return;let a=i.url.replace("www.",""),m=A[r];if(s.status!=="complete"||a===m)return;A[r]=a;let l=await _(i.url,e);if(!l||!l.length){await N(e,r,n);return}let c=await j(r),{token:h,isValid:f,iframeUrl:g}=await R({apiKey:e,body:{domain:l,url:i.url,address:c}});if(!f){f===!1&&U(l);return}T(r,{action:"INJECT",token:h,domain:a,iframeUrl:g})}),chrome.tabs.onRemoved.addListener(r=>delete A[r])},ae=oe;export{ae as bringInitBackground,H as bringInitContentScript};
|