@bringweb3/chrome-extension-kit 1.0.3 → 1.0.5
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 +92 -37
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
<img src="https://
|
|
1
|
+
<a href="https://bringweb3.io/"><img width="200px" src="https://bringweb3.io/wp-content/uploads/2024/05/logo-trans_black.png"/></a><br>
|
|
2
|
+
<br><br>
|
|
3
|
+
<h1>@bringweb3/chrome-extension-kit</h1>
|
|
2
4
|
<br><br>
|
|
3
|
-
<h1 align="center">@bringweb3/chrome-extension-kit</h1>
|
|
4
5
|
|
|
5
6
|
## Table of content
|
|
6
7
|
- [Table of content](#table-of-content)
|
|
@@ -10,9 +11,12 @@
|
|
|
10
11
|
- [Package](#package)
|
|
11
12
|
- [Manifest](#manifest)
|
|
12
13
|
- [Importing](#importing)
|
|
14
|
+
- [import](#import)
|
|
15
|
+
- [require](#require)
|
|
13
16
|
- [Example](#example)
|
|
14
17
|
- [background.js](#backgroundjs)
|
|
15
18
|
- [contentScript.js](#contentscriptjs)
|
|
19
|
+
- [Contact us](#contact-us)
|
|
16
20
|
|
|
17
21
|
## Description
|
|
18
22
|
This integration kit is designed to enhance existing Chrome extensions by adding functionality that enables automatic crypto cashback on online purchases.
|
|
@@ -25,7 +29,7 @@ When a user visits supported online retailer websites, the Crypto Cashback syste
|
|
|
25
29
|
|
|
26
30
|
- Node.js >= 14
|
|
27
31
|
- Chrome extension manifest >= V3 with required permissions
|
|
28
|
-
- Obtain an identifier key from Bringweb3
|
|
32
|
+
- Obtain an identifier key from [Bringweb3](https://bringweb3.io/#contact)
|
|
29
33
|
- Provide a specific logo for the specific outlet
|
|
30
34
|
|
|
31
35
|
## Installing
|
|
@@ -73,14 +77,23 @@ Include this configuration inside your `manifest.json` file:
|
|
|
73
77
|
]
|
|
74
78
|
```
|
|
75
79
|
|
|
76
|
-
Once the package is installed, you can import the library using `import` or `require` approach:
|
|
77
|
-
|
|
78
|
-
|
|
79
80
|
## Importing
|
|
81
|
+
Once the package is installed, you can import the library using `import` or `require` approach:
|
|
80
82
|
|
|
83
|
+
### import
|
|
81
84
|
```js
|
|
82
85
|
import { bringInitBackground } from '@bringweb3/chrome-extension-kit';
|
|
83
86
|
```
|
|
87
|
+
```js
|
|
88
|
+
import { bringInitContentScript } from '@bringweb3/chrome-extension-kit';
|
|
89
|
+
```
|
|
90
|
+
### require
|
|
91
|
+
```js
|
|
92
|
+
const { bringInitBackground } = require('@bringweb3/chrome-extension-kit');
|
|
93
|
+
```
|
|
94
|
+
```js
|
|
95
|
+
const { bringInitContentScript } = require('@bringweb3/chrome-extension-kit');
|
|
96
|
+
```
|
|
84
97
|
|
|
85
98
|
## Example
|
|
86
99
|
|
|
@@ -107,60 +120,102 @@ bringInitContentScript({
|
|
|
107
120
|
getWalletAddress: async () => await new Promise(resolve => setTimeout(() => resolve('<USER_WALLET_ADDRESS>'), 200)),// Async function that returns the current user's wallet address
|
|
108
121
|
promptLogin: () => {...}, // Function that prompts a UI element asking the user to login
|
|
109
122
|
walletAddressListeners: ["customEvent:addressChanged"], // A list of custom events that dispatched when the user's wallet address had changed
|
|
110
|
-
customTheme: {
|
|
123
|
+
customTheme: {
|
|
124
|
+
// font
|
|
111
125
|
fontUrl: 'https://fonts.googleapis.com/css2?family=Matemasie&display=swap',
|
|
112
126
|
fontFamily: "'Matemasie', system-ui",
|
|
113
|
-
|
|
127
|
+
// Popup
|
|
114
128
|
popupBg: "#192E34",
|
|
115
129
|
popupShadow: "",
|
|
116
|
-
|
|
130
|
+
// Primary button
|
|
117
131
|
primaryBtnBg: "linear-gradient(135deg, #5DEB5A 0%, #FDFC47 100%)",
|
|
118
|
-
|
|
119
|
-
|
|
132
|
+
primaryBtnFC: "#041417",
|
|
133
|
+
primaryBtnFW: "600",
|
|
134
|
+
primaryBtnFS: "14px",
|
|
135
|
+
primaryBtnBorderC: "transparent",
|
|
120
136
|
primaryBtnBorderW: "0",
|
|
121
137
|
primaryBtnRadius: "8px",
|
|
122
|
-
|
|
138
|
+
// Secondary button
|
|
123
139
|
secondaryBtnBg: "transparent",
|
|
124
|
-
|
|
125
|
-
|
|
140
|
+
secondaryBtnFS: "12px",
|
|
141
|
+
secondaryBtnFW: "500",
|
|
142
|
+
secondaryBtnFC: "white",
|
|
143
|
+
secondaryBtnBorderC: "rgba(149, 176, 178, 0.50)",
|
|
126
144
|
secondaryBtnBorderW: "2px",
|
|
127
145
|
secondaryBtnRadius: "8px",
|
|
128
|
-
|
|
146
|
+
// Markdown
|
|
129
147
|
markdownBg: "#07131766",
|
|
130
|
-
|
|
148
|
+
markdownFS: "12px",
|
|
149
|
+
markdownFC: "#DADCE5",
|
|
131
150
|
markdownBorderW: "0",
|
|
132
151
|
markdownRadius: "4px",
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
walletColor: "white",
|
|
152
|
+
markdownBorderC: "black",
|
|
153
|
+
markdownScrollbarC: "#DADCE5",
|
|
154
|
+
// Wallet address
|
|
137
155
|
walletBg: "#33535B",
|
|
138
|
-
|
|
156
|
+
walletFS: "10px",
|
|
157
|
+
walletFW: "400",
|
|
158
|
+
walletFC: "white",
|
|
159
|
+
walletBorderC: "white",
|
|
139
160
|
walletBorderW: "0",
|
|
140
161
|
walletRadius: "4px",
|
|
141
|
-
|
|
162
|
+
// Details of offering
|
|
142
163
|
detailsBg: "#33535B",
|
|
143
|
-
|
|
164
|
+
detailsTitleFS: "15px",
|
|
165
|
+
detailsTitleFW: "600",
|
|
166
|
+
detailsTitleFC: "white",
|
|
167
|
+
detailsSubtitleFS: "14px",
|
|
168
|
+
detailsSubtitleFW: "500",
|
|
169
|
+
detailsSubtitleFC: "#A8ADBF",
|
|
144
170
|
detailsRadius: "8px",
|
|
145
171
|
detailsBorderW: "0",
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
172
|
+
detailsBorderC: "transparent",
|
|
173
|
+
detailsAmountFC: "#5DEB5A",
|
|
174
|
+
detailsAmountFW: "700",
|
|
175
|
+
// Overlay
|
|
149
176
|
overlayBg: "#192E34E6",
|
|
150
|
-
|
|
151
|
-
|
|
177
|
+
overlayFS: "13px",
|
|
178
|
+
overlayFW: "400",
|
|
179
|
+
overlayFC: "#DADCE5",
|
|
180
|
+
loaderBg: "#0A2EC0",
|
|
181
|
+
// Optout \ Turn off
|
|
152
182
|
optoutBg: "#192E34",
|
|
153
|
-
|
|
183
|
+
optoutFS: "14px",
|
|
184
|
+
optoutFW: "400",
|
|
185
|
+
optoutFC: "white",
|
|
154
186
|
optoutRadius: "56px",
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
187
|
+
// X Button and close buttons
|
|
188
|
+
closeFS: "9px",
|
|
189
|
+
closeFW: "300",
|
|
190
|
+
closeFC: "#B9BBBF",
|
|
191
|
+
// Token name
|
|
158
192
|
tokenBg: "transparent",
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
193
|
+
tokenFS: "13px",
|
|
194
|
+
tokenFW: "600",
|
|
195
|
+
tokenFC: "#DADCE5",
|
|
196
|
+
tokenBorderW: "2px",
|
|
197
|
+
tokenBorderC: "#DADCE5",
|
|
198
|
+
// Notification popup
|
|
199
|
+
notificationFS: "14px",
|
|
200
|
+
notificationFW: "500",
|
|
201
|
+
notificationFC: "white",
|
|
162
202
|
notificationBtnBg: "linear-gradient(135deg, #5DEB5A 0%, #FDFC47 100%)",
|
|
163
|
-
|
|
203
|
+
notificationBtnFS: "12px",
|
|
204
|
+
notificationBtnFW: "500",
|
|
205
|
+
notificationBtnFC: "#041417",
|
|
206
|
+
notificationBtnBorderW: "0",
|
|
207
|
+
notificationBtnBorderC: "transparent",
|
|
208
|
+
notificationBtnRadius: "8px",
|
|
209
|
+
activateTitleFS: "--activate-title-f-s",
|
|
210
|
+
activateTitleFW: "--activate-title-f-w",
|
|
211
|
+
activateTitleFC: "--activate-title-f-c",
|
|
212
|
+
activateTitleBoldFS: "--activate-title-bold-f-s",
|
|
213
|
+
activateTitleBoldFW: "--activate-title-bold-f-w",
|
|
214
|
+
activateTitleBoldFC: "--activate-title-bold-f-c",
|
|
164
215
|
}
|
|
165
216
|
});
|
|
166
|
-
```
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Contact us
|
|
220
|
+
|
|
221
|
+
For more information: [contact us](https://bringweb3.io/#contact)
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var s in t)g(e,s,{get:t[s],enumerable:!0})},Q=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of F(t))!V.call(e,r)&&r!==s&&g(e,r,{get:()=>t[r],enumerable:!(n=$(t,r))||n.enumerable});return e};var J=e=>Q(g({},"__esModule",{value:!0}),e);var fe={};K(fe,{bringInitBackground:()=>W,bringInitContentScript:()=>O});module.exports=J(fe);var B=e=>{let t=new URLSearchParams,{query:s,prefix:n}=e;return Object.entries(s).forEach(([r,o])=>{o&&(n&&(r=`${n}_${r}`),r==="url"?t.append(r,encodeURIComponent(o)):t.append(r,o))}),t.toString()},h=B;var x={name:"@bringweb3/chrome-extension-kit",version:"1.0.5",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 H=()=>x.version,T=H;var Y=({query:e,theme:t,iframeSrc:s})=>{let n=chrome.runtime.id,r=h({query:{...e,extensionId:n,v:T()}}),o=t?`&${h({query:t,prefix:"t"})}`:"",i=document.createElement("iframe");return i.id=`bringweb3-iframe:${n}`,i.src=`${s}?${r}${o}`,i.setAttribute("sandbox","allow-popups allow-scripts allow-same-origin allow-top-navigation-by-user-activation"),i.style.position="fixed",i.scrolling="no",i.style.overflow="hidden",i.style.width="1px",i.style.height="1px",i.style.right="8px",i.style.borderRadius="10px",i.style.border="none",i.style.cssText+="z-index: 99999999999999 !important;",t?.popupShadow&&(i.style.boxShadow=t.popupShadow),document.documentElement.appendChild(i),i},k=Y;var z=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([s,n])=>{s in e.style&&(e.style[s]=n)})},D=z;var X=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let s=t.sheet;s?e.forEach(({name:n,rules:r})=>{s.insertRule(`@keyframes ${n} { ${r} }`,s.cssRules.length)}):console.error("Failed to create stylesheet")},v=X;var m={OPEN:"OPEN",CLOSE:"CLOSE",ACTIVATE:"ACTIVATE",PROMPT_LOGIN:"PROMPT_LOGIN",OPT_OUT:"OPT_OUT",ADD_KEYFRAMES:"ADD_KEYFRAMES"},Z=[m.ACTIVATE],ee=({event:e,iframeEl:t,promptLogin:s})=>{let{data:n}=e,{from:r,action:o,style:i,keyFrames:a,time:d,extensionId:p}=n;if(r==="bringweb3"&&!(p!==chrome.runtime.id&&!Z.includes(o)))switch(o){case m.OPEN:D(t,i);break;case m.CLOSE:t&&t.parentNode?.removeChild(t),d&&chrome.runtime.sendMessage({action:o,time:d});break;case m.PROMPT_LOGIN:s();break;case m.ACTIVATE:chrome.runtime.sendMessage({action:o});break;case m.OPT_OUT:chrome.runtime.sendMessage({action:o,time:d});break;case m.ADD_KEYFRAMES:v(a);break;default:break}},C=ee;var te=({walletAddressListeners:e,getWalletAddress:t,iframeEl:s})=>{for(let n=0;n<e.length;n++){let r=e[n];r&&window.addEventListener(r,async o=>{if(!s&&(s=document.querySelector(`#bringweb3-iframe:${chrome.runtime.id}`),!s)||!s.contentWindow)return;let i=await t();s.contentWindow.postMessage({action:"WALLET_ADDRESS_UPDATE",walletAddress:i},"*")})}},P=te;var w=null,S=!1,ne=async({getWalletAddress:e,promptLogin:t,walletAddressListeners:s,customTheme:n,iframeEndpoint:r})=>{if(!e||!t||!s?.length||!r)throw new Error("Missing configuration");P({walletAddressListeners:s,getWalletAddress:e,iframeEl:w}),window.addEventListener("message",o=>C({event:o,iframeEl:w,promptLogin:t})),chrome.runtime.onMessage.addListener((o,i,a)=>{let{action:d}=o;switch(d){case"GET_WALLET_ADDRESS":return e().then(u=>a({status:"success",walletAddress:u})).catch(u=>a({status:"success",walletAddress:void 0})),!0;case"INJECT":if(S)return;let{token:p,page:f}=o;return w=k({query:{token:p},iframeSrc:f==="notification"?`${r}notification`:r,theme:n}),S=!0,a({status:"success"}),!0;default:console.error(`Unknown action: ${d}`);break}})},O=ne;var l=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=l.getInstance().getApiEndpoint();return await(await fetch(`${t}/domains`,{headers:{"x-api-key":e}})).json()},R=se;var re=async({apiKey:e,body:t})=>{let s=l.getInstance().getApiEndpoint();return await(await fetch(`${s}/check/popup`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(t)})).json()},I=re;var ie=async({apiKey:e,walletAddress:t,cashbackUrl:s})=>{let n=l.getInstance().getApiEndpoint(),r={walletAddress:t,test:!0};return s&&(r.cashbackUrl=s),await(await fetch(`${n}/check/notification`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(r)})).json()},L=ie;var y="updateCache";var oe=async(e,t)=>{chrome.storage.local.set({[e]:t})},ae=async e=>(await chrome.storage.local.get(e))[e],ce=async e=>{chrome.storage.local.remove(e)},c={set:oe,get:ae,remove:ce};var M=30*60*1e3,U=async e=>{let t=await c.get("walletAddress");try{if(!e){let n=await chrome.tabs.query({active:!0,currentWindow:!0});if(!n||!n[0]||!n[0].id)return t;e=n[0].id}let s=await E(e,{action:"GET_WALLET_ADDRESS"});s?.walletAddress&&t!==s?.walletAddress&&(t=s?.walletAddress,await c.set("walletAddress",t))}catch{}return t},de=e=>{let t=Date.now();return(e-t)/1e3/60},N=async e=>{let t=await R(e);c.set("relevantDomains",t.relevantDomains);let{nextUpdateTimestamp:s}=t,n=de(s);chrome.alarms.create(y,{delayInMinutes:n})},le=async(e,t,s,n)=>{let r={showNotification:!1,token:""},o=await c.get("notificationCheck");if(o?.check&&Date.now()<o.check)return r;let i=await U(t);if(!i)return r;let a=await L({apiKey:e,walletAddress:i,cashbackUrl:s}),d={check:n?a.nextRequestTimestampActivated:a.nextRequestTimestampRegular,nextRequestTimestampActivated:a.nextRequestTimestampActivated,nextRequestTimestampRegular:a.nextRequestTimestampRegular};return c.set("notificationCheck",d),{showNotification:a.showNotification,token:a.token}},pe=e=>e.replace(/^(https?:\/\/)?(www\.)?/,""),_=async e=>{let t=await c.get("relevantDomains");if(!e||!t||!t.length)return"";let s=pe(e);for(let n of t)if(s.startsWith(n)){let r=await c.get("quietDomains");return r&&r[n]&&Date.now()<r[n]?"":n}return""},j=async(e,t)=>{t||(t=M);let s=await c.get("quietDomains");typeof s=="object"?s[e]=Date.now()+t:s={[e]:Date.now()+M},c.set("quietDomains",s)},me=e=>e?chrome.runtime.getURL(e):void 0,E=async(e,t)=>{for(let r=0;r<5;r++)try{let o=await chrome.tabs.get(e);return chrome.runtime.lastError?void 0:await chrome.tabs.sendMessage(e,t)}catch{r<4&&await new Promise(i=>setTimeout(i,1e3*Math.pow(2,r)))}},q=async(e,t,s)=>{let n=await le(e,t,me(s));n.showNotification&&await E(t,{action:"INJECT",token:n.token,page:"notification"})},A={},ue=async({identifier:e,apiEndpoint:t,cashbackPagePath:s})=>{if(!e||!t)throw new Error("Missing configuration");if(!["prod","sandbox"].includes(t))throw new Error("unknown apiEndpoint");l.getInstance().setApiEndpoint(t),N(e),chrome.alarms.onAlarm.addListener(async n=>{let{name:r}=n;switch(r){case y:N(e);break;default:console.error("alarm with no use case:",r);break}}),chrome.runtime.onMessage.addListener(async(n,r)=>{let{action:o,time:i}=n;switch(o){case"ACTIVATE":let a=await c.get("notificationCheck");if(!a.check||!a.nextRequestTimestampActivated)break;a.check=a.nextRequestTimestampActivated,c.set("notificationCheck",a);break;case"OPT_OUT":c.set("optOut",Date.now()+i);break;case"CLOSE":let d=await _(r.tab?.url||r.origin);if(!d)break;j(d,i);break;default:console.warn(`Bring unknown action: ${o}`);break}}),chrome.tabs.onUpdated.addListener(async(n,r,o)=>{let i=await c.get("optOut");if(i&&i>Date.now()){await q(e,n,s);return}if(!o.url)return;let a=o.url.replace("www.",""),d=A[n];if(r.status!=="complete"||a===d)return;A[n]=a;let p=await _(o.url);if(!p||!p.length){await q(e,n,s);return}let f=await U(n),{token:u,isValid:b}=await I({apiKey:e,body:{domain:p,url:o.url,address:f}});if(!b){b===!1&&j(p);return}E(n,{action:"INJECT",token:u,domain:a})}),chrome.tabs.onRemoved.addListener(n=>delete A[n])},W=ue;0&&(module.exports={bringInitBackground,bringInitContentScript});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var q=e=>{let t=new URLSearchParams,{query:
|
|
1
|
+
var q=e=>{let t=new URLSearchParams,{query:s,prefix:n}=e;return Object.entries(s).forEach(([r,o])=>{o&&(n&&(r=`${n}_${r}`),r==="url"?t.append(r,encodeURIComponent(o)):t.append(r,o))}),t.toString()},g=q;var b={name:"@bringweb3/chrome-extension-kit",version:"1.0.5",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 W=()=>b.version,x=W;var $=({query:e,theme:t,iframeSrc:s})=>{let n=chrome.runtime.id,r=g({query:{...e,extensionId:n,v:x()}}),o=t?`&${g({query:t,prefix:"t"})}`:"",i=document.createElement("iframe");return i.id=`bringweb3-iframe:${n}`,i.src=`${s}?${r}${o}`,i.setAttribute("sandbox","allow-popups allow-scripts allow-same-origin allow-top-navigation-by-user-activation"),i.style.position="fixed",i.scrolling="no",i.style.overflow="hidden",i.style.width="1px",i.style.height="1px",i.style.right="8px",i.style.borderRadius="10px",i.style.border="none",i.style.cssText+="z-index: 99999999999999 !important;",t?.popupShadow&&(i.style.boxShadow=t.popupShadow),document.documentElement.appendChild(i),i},T=$;var F=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([s,n])=>{s in e.style&&(e.style[s]=n)})},k=F;var V=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let s=t.sheet;s?e.forEach(({name:n,rules:r})=>{s.insertRule(`@keyframes ${n} { ${r} }`,s.cssRules.length)}):console.error("Failed to create stylesheet")},D=V;var m={OPEN:"OPEN",CLOSE:"CLOSE",ACTIVATE:"ACTIVATE",PROMPT_LOGIN:"PROMPT_LOGIN",OPT_OUT:"OPT_OUT",ADD_KEYFRAMES:"ADD_KEYFRAMES"},K=[m.ACTIVATE],Q=({event:e,iframeEl:t,promptLogin:s})=>{let{data:n}=e,{from:r,action:o,style:i,keyFrames:a,time:d,extensionId:p}=n;if(r==="bringweb3"&&!(p!==chrome.runtime.id&&!K.includes(o)))switch(o){case m.OPEN:k(t,i);break;case m.CLOSE:t&&t.parentNode?.removeChild(t),d&&chrome.runtime.sendMessage({action:o,time:d});break;case m.PROMPT_LOGIN:s();break;case m.ACTIVATE:chrome.runtime.sendMessage({action:o});break;case m.OPT_OUT:chrome.runtime.sendMessage({action:o,time:d});break;case m.ADD_KEYFRAMES:D(a);break;default:break}},v=Q;var J=({walletAddressListeners:e,getWalletAddress:t,iframeEl:s})=>{for(let n=0;n<e.length;n++){let r=e[n];r&&window.addEventListener(r,async o=>{if(!s&&(s=document.querySelector(`#bringweb3-iframe:${chrome.runtime.id}`),!s)||!s.contentWindow)return;let i=await t();s.contentWindow.postMessage({action:"WALLET_ADDRESS_UPDATE",walletAddress:i},"*")})}},C=J;var h=null,P=!1,B=async({getWalletAddress:e,promptLogin:t,walletAddressListeners:s,customTheme:n,iframeEndpoint:r})=>{if(!e||!t||!s?.length||!r)throw new Error("Missing configuration");C({walletAddressListeners:s,getWalletAddress:e,iframeEl:h}),window.addEventListener("message",o=>v({event:o,iframeEl:h,promptLogin:t})),chrome.runtime.onMessage.addListener((o,i,a)=>{let{action:d}=o;switch(d){case"GET_WALLET_ADDRESS":return e().then(u=>a({status:"success",walletAddress:u})).catch(u=>a({status:"success",walletAddress:void 0})),!0;case"INJECT":if(P)return;let{token:p,page:f}=o;return h=T({query:{token:p},iframeSrc:f==="notification"?`${r}notification`:r,theme:n}),P=!0,a({status:"success"}),!0;default:console.error(`Unknown action: ${d}`);break}})},G=B;var l=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 H=async e=>{let t=l.getInstance().getApiEndpoint();return await(await fetch(`${t}/domains`,{headers:{"x-api-key":e}})).json()},S=H;var Y=async({apiKey:e,body:t})=>{let s=l.getInstance().getApiEndpoint();return await(await fetch(`${s}/check/popup`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(t)})).json()},O=Y;var z=async({apiKey:e,walletAddress:t,cashbackUrl:s})=>{let n=l.getInstance().getApiEndpoint(),r={walletAddress:t,test:!0};return s&&(r.cashbackUrl=s),await(await fetch(`${n}/check/notification`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(r)})).json()},R=z;var w="updateCache";var X=async(e,t)=>{chrome.storage.local.set({[e]:t})},Z=async e=>(await chrome.storage.local.get(e))[e],ee=async e=>{chrome.storage.local.remove(e)},c={set:X,get:Z,remove:ee};var I=30*60*1e3,j=async e=>{let t=await c.get("walletAddress");try{if(!e){let n=await chrome.tabs.query({active:!0,currentWindow:!0});if(!n||!n[0]||!n[0].id)return t;e=n[0].id}let s=await A(e,{action:"GET_WALLET_ADDRESS"});s?.walletAddress&&t!==s?.walletAddress&&(t=s?.walletAddress,await c.set("walletAddress",t))}catch{}return t},te=e=>{let t=Date.now();return(e-t)/1e3/60},L=async e=>{let t=await S(e);c.set("relevantDomains",t.relevantDomains);let{nextUpdateTimestamp:s}=t,n=te(s);chrome.alarms.create(w,{delayInMinutes:n})},ne=async(e,t,s,n)=>{let r={showNotification:!1,token:""},o=await c.get("notificationCheck");if(o?.check&&Date.now()<o.check)return r;let i=await j(t);if(!i)return r;let a=await R({apiKey:e,walletAddress:i,cashbackUrl:s}),d={check:n?a.nextRequestTimestampActivated:a.nextRequestTimestampRegular,nextRequestTimestampActivated:a.nextRequestTimestampActivated,nextRequestTimestampRegular:a.nextRequestTimestampRegular};return c.set("notificationCheck",d),{showNotification:a.showNotification,token:a.token}},se=e=>e.replace(/^(https?:\/\/)?(www\.)?/,""),M=async e=>{let t=await c.get("relevantDomains");if(!e||!t||!t.length)return"";let s=se(e);for(let n of t)if(s.startsWith(n)){let r=await c.get("quietDomains");return r&&r[n]&&Date.now()<r[n]?"":n}return""},N=async(e,t)=>{t||(t=I);let s=await c.get("quietDomains");typeof s=="object"?s[e]=Date.now()+t:s={[e]:Date.now()+I},c.set("quietDomains",s)},re=e=>e?chrome.runtime.getURL(e):void 0,A=async(e,t)=>{for(let r=0;r<5;r++)try{let o=await chrome.tabs.get(e);return chrome.runtime.lastError?void 0:await chrome.tabs.sendMessage(e,t)}catch{r<4&&await new Promise(i=>setTimeout(i,1e3*Math.pow(2,r)))}},_=async(e,t,s)=>{let n=await ne(e,t,re(s));n.showNotification&&await A(t,{action:"INJECT",token:n.token,page:"notification"})},y={},ie=async({identifier:e,apiEndpoint:t,cashbackPagePath:s})=>{if(!e||!t)throw new Error("Missing configuration");if(!["prod","sandbox"].includes(t))throw new Error("unknown apiEndpoint");l.getInstance().setApiEndpoint(t),L(e),chrome.alarms.onAlarm.addListener(async n=>{let{name:r}=n;switch(r){case w:L(e);break;default:console.error("alarm with no use case:",r);break}}),chrome.runtime.onMessage.addListener(async(n,r)=>{let{action:o,time:i}=n;switch(o){case"ACTIVATE":let a=await c.get("notificationCheck");if(!a.check||!a.nextRequestTimestampActivated)break;a.check=a.nextRequestTimestampActivated,c.set("notificationCheck",a);break;case"OPT_OUT":c.set("optOut",Date.now()+i);break;case"CLOSE":let d=await M(r.tab?.url||r.origin);if(!d)break;N(d,i);break;default:console.warn(`Bring unknown action: ${o}`);break}}),chrome.tabs.onUpdated.addListener(async(n,r,o)=>{let i=await c.get("optOut");if(i&&i>Date.now()){await _(e,n,s);return}if(!o.url)return;let a=o.url.replace("www.",""),d=y[n];if(r.status!=="complete"||a===d)return;y[n]=a;let p=await M(o.url);if(!p||!p.length){await _(e,n,s);return}let f=await j(n),{token:u,isValid:E}=await O({apiKey:e,body:{domain:p,url:o.url,address:f}});if(!E){E===!1&&N(p);return}A(n,{action:"INJECT",token:u,domain:a})}),chrome.tabs.onRemoved.addListener(n=>delete y[n])},oe=ie;export{oe as bringInitBackground,G as bringInitContentScript};
|
package/package.json
CHANGED