@bringweb3/chrome-extension-kit 1.0.1 → 1.0.3
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/dist/index.d.mts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
interface Configuration$1 {
|
|
2
|
+
iframeEndpoint: string;
|
|
3
|
+
getWalletAddress: () => Promise<WalletAddress>;
|
|
4
|
+
promptLogin: () => Promise<WalletAddress>;
|
|
5
|
+
walletAddressListeners: string[];
|
|
6
|
+
customTheme?: Style;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Initializes the content script for the Bring extension.
|
|
10
|
+
*
|
|
11
|
+
* @async
|
|
12
|
+
* @function bringInitContentScript
|
|
13
|
+
* @param {Object} configuration - The configuration object.
|
|
14
|
+
* @param {Function} configuration.getWalletAddress - A function that returns a Promise resolving to the wallet address.
|
|
15
|
+
* @param {Function} configuration.promptLogin - A function to prompt the user to login.
|
|
16
|
+
* @param {string[]} configuration.walletAddressListeners - An array of strings representing wallet address listeners.
|
|
17
|
+
* @param {Object} [configuration.customTheme] - Optional custom theme settings.
|
|
18
|
+
* @param {string} configuration.iframeEndpoint - The endpoint URL for the iframe.
|
|
19
|
+
* @throws {Error} Throws an error if any required configuration is missing.
|
|
20
|
+
* @returns {Promise<void>}
|
|
21
|
+
*
|
|
22
|
+
* @description
|
|
23
|
+
* This function sets up event listeners for wallet address changes, iframe messages,
|
|
24
|
+
* and Chrome runtime messages. It handles actions such as getting the wallet address
|
|
25
|
+
* and injecting iframes based on received messages.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* bringInitContentScript({
|
|
29
|
+
* getWalletAddress: async () => '0x1234...',
|
|
30
|
+
* promptLogin: () => { ... },
|
|
31
|
+
* walletAddressListeners: ["listener1", "listener2"],
|
|
32
|
+
* iframeEndpoint: 'https://example.com/iframe'
|
|
33
|
+
* });
|
|
34
|
+
*/
|
|
35
|
+
declare const bringInitContentScript: ({ getWalletAddress, promptLogin, walletAddressListeners, customTheme, iframeEndpoint }: Configuration$1) => Promise<void>;
|
|
36
|
+
|
|
37
|
+
interface Configuration {
|
|
38
|
+
identifier: string;
|
|
39
|
+
apiEndpoint: string;
|
|
40
|
+
cashbackPagePath?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Initializes the background script for the Bring extension.
|
|
44
|
+
*
|
|
45
|
+
* @async
|
|
46
|
+
* @function bringInitBackground
|
|
47
|
+
* @param {Object} configuration - The configuration object.
|
|
48
|
+
* @param {string} configuration.identifier - The identifier for the extension.
|
|
49
|
+
* @param {string} configuration.apiEndpoint - The API endpoint ('prod' or 'sandbox').
|
|
50
|
+
* @param {string} [configuration.cashbackPagePath] - Optional path to the cashback page.
|
|
51
|
+
* @throws {Error} Throws an error if identifier or apiEndpoint is missing, or if apiEndpoint is invalid.
|
|
52
|
+
* @returns {Promise<void>}
|
|
53
|
+
*
|
|
54
|
+
* @description
|
|
55
|
+
* This function sets up the background processes for the Bring extension. It initializes
|
|
56
|
+
* the API endpoint, sets up listeners for alarms, runtime messages, and tab updates.
|
|
57
|
+
* It handles various actions such as opting out, closing notifications, injecting content
|
|
58
|
+
* based on URL changes, and managing quiet domains.
|
|
59
|
+
*
|
|
60
|
+
* The function performs the following tasks:
|
|
61
|
+
* - Validates and sets the API endpoint
|
|
62
|
+
* - Updates the cache
|
|
63
|
+
* - Sets up listeners for alarms to update cache periodically
|
|
64
|
+
* - Handles runtime messages for opting out and closing notifications
|
|
65
|
+
* - Monitors tab updates to inject content or show notifications based on URL changes
|
|
66
|
+
* - Validates domains and manages quiet domains
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* bringInitBackground({
|
|
70
|
+
* identifier: 'my-extension-id',
|
|
71
|
+
* apiEndpoint: 'sandbox',
|
|
72
|
+
* cashbackPagePath: '/cashback.html'
|
|
73
|
+
* });
|
|
74
|
+
*/
|
|
75
|
+
declare const bringInitBackground: ({ identifier, apiEndpoint, cashbackPagePath }: Configuration) => Promise<void>;
|
|
76
|
+
|
|
77
|
+
export { bringInitBackground, bringInitContentScript };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
interface Configuration$1 {
|
|
2
|
+
iframeEndpoint: string;
|
|
3
|
+
getWalletAddress: () => Promise<WalletAddress>;
|
|
4
|
+
promptLogin: () => Promise<WalletAddress>;
|
|
5
|
+
walletAddressListeners: string[];
|
|
6
|
+
customTheme?: Style;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Initializes the content script for the Bring extension.
|
|
10
|
+
*
|
|
11
|
+
* @async
|
|
12
|
+
* @function bringInitContentScript
|
|
13
|
+
* @param {Object} configuration - The configuration object.
|
|
14
|
+
* @param {Function} configuration.getWalletAddress - A function that returns a Promise resolving to the wallet address.
|
|
15
|
+
* @param {Function} configuration.promptLogin - A function to prompt the user to login.
|
|
16
|
+
* @param {string[]} configuration.walletAddressListeners - An array of strings representing wallet address listeners.
|
|
17
|
+
* @param {Object} [configuration.customTheme] - Optional custom theme settings.
|
|
18
|
+
* @param {string} configuration.iframeEndpoint - The endpoint URL for the iframe.
|
|
19
|
+
* @throws {Error} Throws an error if any required configuration is missing.
|
|
20
|
+
* @returns {Promise<void>}
|
|
21
|
+
*
|
|
22
|
+
* @description
|
|
23
|
+
* This function sets up event listeners for wallet address changes, iframe messages,
|
|
24
|
+
* and Chrome runtime messages. It handles actions such as getting the wallet address
|
|
25
|
+
* and injecting iframes based on received messages.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* bringInitContentScript({
|
|
29
|
+
* getWalletAddress: async () => '0x1234...',
|
|
30
|
+
* promptLogin: () => { ... },
|
|
31
|
+
* walletAddressListeners: ["listener1", "listener2"],
|
|
32
|
+
* iframeEndpoint: 'https://example.com/iframe'
|
|
33
|
+
* });
|
|
34
|
+
*/
|
|
35
|
+
declare const bringInitContentScript: ({ getWalletAddress, promptLogin, walletAddressListeners, customTheme, iframeEndpoint }: Configuration$1) => Promise<void>;
|
|
36
|
+
|
|
37
|
+
interface Configuration {
|
|
38
|
+
identifier: string;
|
|
39
|
+
apiEndpoint: string;
|
|
40
|
+
cashbackPagePath?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Initializes the background script for the Bring extension.
|
|
44
|
+
*
|
|
45
|
+
* @async
|
|
46
|
+
* @function bringInitBackground
|
|
47
|
+
* @param {Object} configuration - The configuration object.
|
|
48
|
+
* @param {string} configuration.identifier - The identifier for the extension.
|
|
49
|
+
* @param {string} configuration.apiEndpoint - The API endpoint ('prod' or 'sandbox').
|
|
50
|
+
* @param {string} [configuration.cashbackPagePath] - Optional path to the cashback page.
|
|
51
|
+
* @throws {Error} Throws an error if identifier or apiEndpoint is missing, or if apiEndpoint is invalid.
|
|
52
|
+
* @returns {Promise<void>}
|
|
53
|
+
*
|
|
54
|
+
* @description
|
|
55
|
+
* This function sets up the background processes for the Bring extension. It initializes
|
|
56
|
+
* the API endpoint, sets up listeners for alarms, runtime messages, and tab updates.
|
|
57
|
+
* It handles various actions such as opting out, closing notifications, injecting content
|
|
58
|
+
* based on URL changes, and managing quiet domains.
|
|
59
|
+
*
|
|
60
|
+
* The function performs the following tasks:
|
|
61
|
+
* - Validates and sets the API endpoint
|
|
62
|
+
* - Updates the cache
|
|
63
|
+
* - Sets up listeners for alarms to update cache periodically
|
|
64
|
+
* - Handles runtime messages for opting out and closing notifications
|
|
65
|
+
* - Monitors tab updates to inject content or show notifications based on URL changes
|
|
66
|
+
* - Validates domains and manages quiet domains
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* bringInitBackground({
|
|
70
|
+
* identifier: 'my-extension-id',
|
|
71
|
+
* apiEndpoint: 'sandbox',
|
|
72
|
+
* cashbackPagePath: '/cashback.html'
|
|
73
|
+
* });
|
|
74
|
+
*/
|
|
75
|
+
declare const bringInitBackground: ({ identifier, apiEndpoint, cashbackPagePath }: Configuration) => Promise<void>;
|
|
76
|
+
|
|
77
|
+
export { bringInitBackground, bringInitContentScript };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var h=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},V=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of j(t))!F.call(e,s)&&s!==r&&h(e,s,{get:()=>t[s],enumerable:!(n=W(t,s))||n.enumerable});return e};var Q=e=>V(h({},"__esModule",{value:!0}),e);var
|
|
1
|
+
"use strict";var h=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},V=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of j(t))!F.call(e,s)&&s!==r&&h(e,s,{get:()=>t[s],enumerable:!(n=W(t,s))||n.enumerable});return e};var Q=e=>V(h({},"__esModule",{value:!0}),e);var ue={};K(ue,{bringInitBackground:()=>q,bringInitContentScript:()=>v});module.exports=Q(ue);var B=e=>{let t=new URLSearchParams,{query:r,prefix:n}=e;return Object.entries(r).forEach(([s,o])=>{o&&(n&&(s=`${n}_${s}`),s==="url"?t.append(s,encodeURIComponent(o)):t.append(s,o))}),t.toString()},f=B;var G=({query:e,theme:t,iframeSrc:r})=>{let n=chrome.runtime.id,s=f({query:{...e,extensionId:n}}),o=t?`&${f({query:t,prefix:"t"})}`:"",i=document.createElement("iframe");return i.id=`bringweb3-iframe:${n}`,i.src=`${r}?${s}${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},b=G;var J=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([r,n])=>{r in e.style&&(e.style[r]=n)})},T=J;var H=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let r=t.sheet;r?e.forEach(({name:n,rules:s})=>{r.insertRule(`@keyframes ${n} { ${s} }`,r.cssRules.length)}):console.error("Failed to create stylesheet")},k=H;var u={OPEN:"OPEN",CLOSE:"CLOSE",ACTIVATE:"ACTIVATE",PROMPT_LOGIN:"PROMPT_LOGIN",OPT_OUT:"OPT_OUT",ADD_KEYFRAMES:"ADD_KEYFRAMES"},Y=[u.ACTIVATE],z=({event:e,iframeEl:t,promptLogin:r})=>{let{data:n}=e,{from:s,action:o,style:i,keyFrames:a,time:c,extensionId:p}=n;if(s==="bringweb3"&&!(p!==chrome.runtime.id&&!Y.includes(o)))switch(o){case u.OPEN:T(t,i);break;case u.CLOSE:t&&t.parentNode?.removeChild(t),c&&chrome.runtime.sendMessage({action:o,time:c});break;case u.PROMPT_LOGIN:r();break;case u.ACTIVATE:chrome.runtime.sendMessage({action:o});break;case u.OPT_OUT:chrome.runtime.sendMessage({action:o,time:c});break;case u.ADD_KEYFRAMES:k(a);break;default:break}},x=z;var X=({walletAddressListeners:e,getWalletAddress:t,iframeEl:r})=>{for(let n=0;n<e.length;n++){let s=e[n];s&&window.addEventListener(s,async o=>{if(!r&&(r=document.querySelector(`#bringweb3-iframe:${chrome.runtime.id}`),!r)||!r.contentWindow)return;let i=await t();r.contentWindow.postMessage({action:"WALLET_ADDRESS_UPDATE",walletAddress:i},"*")})}},D=X;var w=null,C=!1,Z=async({getWalletAddress:e,promptLogin:t,walletAddressListeners:r,customTheme:n,iframeEndpoint:s})=>{if(!e||!t||!r?.length||!s)throw new Error("Missing configuration");D({walletAddressListeners:r,getWalletAddress:e,iframeEl:w}),window.addEventListener("message",o=>x({event:o,iframeEl:w,promptLogin:t})),chrome.runtime.onMessage.addListener((o,i,a)=>{let{action:c}=o;switch(c){case"GET_WALLET_ADDRESS":return e().then(g=>a({status:"success",walletAddress:g})).catch(g=>a({status:"success",walletAddress:void 0})),!0;case"INJECT":if(C)return;let{token:p,page:m}=o;return w=b({query:{token:p},iframeSrc:m==="notification"?`${s}notification`:s,theme:n}),C=!0,a({status:"success"}),!0;default:console.error(`Unknown action: ${c}`);break}})},v=Z;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 ee=async e=>{let t=l.getInstance().getApiEndpoint();return await(await fetch(`${t}/domains`,{headers:{"x-api-key":e}})).json()},P=ee;var te=async({apiKey:e,query:t})=>{let r=l.getInstance().getApiEndpoint(),n=f({query:{...t,country:"us"}});return await(await fetch(`${r}/token?${n.toString()}`,{headers:{"x-api-key":e}})).json()},S=te;var ne=async({apiKey:e,walletAddress:t,cashbackUrl:r})=>{let n=l.getInstance().getApiEndpoint(),s={walletAddress:t};return r&&(s.cashbackUrl=r),await(await fetch(`${n}/check-events`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(s)})).json()},R=ne;var y="updateCache";var re=async(e,t)=>{chrome.storage.local.set({[e]:t})},se=async e=>(await chrome.storage.local.get(e))[e],ie=async e=>{chrome.storage.local.remove(e)},d={set:re,get:se,remove:ie};var O=30*60*1e3,_=async e=>{let t=await d.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 r=await E(e,{action:"GET_WALLET_ADDRESS"});r?.walletAddress&&t!==r?.walletAddress&&(t=r?.walletAddress,await d.set("walletAddress",t))}catch{}return t},oe=e=>{let t=Date.now();return(e-t)/1e3/60},L=async e=>{let t=await P(e);d.set("relevantDomains",t.relevantDomains);let{nextUpdateTimestamp:r}=t,n=oe(r);chrome.alarms.create(y,{delayInMinutes:n})},ae=async(e,t,r,n)=>{let s={showNotification:!1,token:""},o=await d.get("notificationCheck");if(o?.check&&Date.now()<o.check)return s;let i=await _(t);if(!i)return s;let a=await R({apiKey:e,walletAddress:i,cashbackUrl:r}),c={check:n?a.nextRequestTimestampActivated:a.nextRequestTimestampRegular,nextRequestTimestampActivated:a.nextRequestTimestampActivated,nextRequestTimestampRegular:a.nextRequestTimestampRegular};return d.set("notificationCheck",c),{showNotification:a.showNotification,token:a.token}},ce=e=>e.replace(/^(https?:\/\/)?(www\.)?/,""),I=async e=>{let t=await d.get("relevantDomains");if(!e||!t||!t.length)return"";let r=ce(e);for(let n of t)if(r.startsWith(n)){let s=await d.get("quietDomains");return s&&s[n]&&Date.now()<s[n]?"":n}return""},M=async(e,t)=>{t||(t=O);let r=await d.get("quietDomains");typeof r=="object"?r[e]=Date.now()+t:r={[e]:Date.now()+O},d.set("quietDomains",r)},de=e=>e?chrome.runtime.getURL(e):void 0,E=async(e,t)=>{for(let s=0;s<5;s++)try{let o=await chrome.tabs.get(e);return chrome.runtime.lastError?void 0:await chrome.tabs.sendMessage(e,t)}catch{s<4&&await new Promise(i=>setTimeout(i,1e3*Math.pow(2,s)))}},N=async(e,t,r)=>{let n=await ae(e,t,de(r));n.showNotification&&await E(t,{action:"INJECT",token:n.token,page:"notification"})},A={},le=async({identifier:e,apiEndpoint:t,cashbackPagePath:r})=>{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:s}=n;switch(s){case y:L(e);break;default:console.error("alarm with no use case:",s);break}}),chrome.runtime.onMessage.addListener(async(n,s)=>{let{action:o,time:i}=n;switch(o){case"ACTIVATE":let a=await d.get("notificationCheck");if(!a.check||!a.nextRequestTimestampActivated)break;a.check=a.nextRequestTimestampActivated,d.set("notificationCheck",a);break;case"OPT_OUT":d.set("optOut",Date.now()+i);break;case"CLOSE":let c=await I(s.tab?.url||s.origin);if(!c)break;M(c,i);break;default:console.warn(`Bring unknown action: ${o}`);break}}),chrome.tabs.onUpdated.addListener(async(n,s,o)=>{let i=await d.get("optOut");if(i&&i>Date.now()){await N(e,n,r);return}if(!o.url)return;let a=new URL(o.url),c=`${a.hostname.replace("www.","")}${a.pathname}`,p=A[n];if(s.status!=="complete"||c===p)return;A[n]=c;let m=await I(o.url);if(!m||!m.length){await N(e,n,r);return}let g=await _(n),{token:U,isValid:$}=await S({apiKey:e,query:{domain:m,url:o.url,address:g}});if(!$){M(m);return}E(n,{action:"INJECT",token:U,domain:c})}),chrome.tabs.onRemoved.addListener(n=>delete A[n])},q=le;0&&(module.exports={bringInitBackground,bringInitContentScript});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var q=e=>{let t=new URLSearchParams,{query:r,prefix:n}=e;return Object.entries(r).forEach(([s,o])=>{o&&(n&&(s=`${n}_${s}`),s==="url"?t.append(s,encodeURIComponent(o)):t.append(s,o))}),t.toString()},f=q;var U=({query:e,theme:t,iframeSrc:r})=>{let n=chrome.runtime.id,s=f({query:{...e,extensionId:n}}),o=t?`&${f({query:t,prefix:"t"})}`:"",i=document.createElement("iframe");return i.id=`bringweb3-iframe:${n}`,i.src=`${r}?${s}${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},E=U;var $=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([r,n])=>{r in e.style&&(e.style[r]=n)})},b=$;var W=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let r=t.sheet;r?e.forEach(({name:n,rules:s})=>{r.insertRule(`@keyframes ${n} { ${s} }`,r.cssRules.length)}):console.error("Failed to create stylesheet")},T=W;var
|
|
1
|
+
var q=e=>{let t=new URLSearchParams,{query:r,prefix:n}=e;return Object.entries(r).forEach(([s,o])=>{o&&(n&&(s=`${n}_${s}`),s==="url"?t.append(s,encodeURIComponent(o)):t.append(s,o))}),t.toString()},f=q;var U=({query:e,theme:t,iframeSrc:r})=>{let n=chrome.runtime.id,s=f({query:{...e,extensionId:n}}),o=t?`&${f({query:t,prefix:"t"})}`:"",i=document.createElement("iframe");return i.id=`bringweb3-iframe:${n}`,i.src=`${r}?${s}${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},E=U;var $=(e,t)=>{!e||!t||!Object.keys(t).length||Object.entries(t).forEach(([r,n])=>{r in e.style&&(e.style[r]=n)})},b=$;var W=e=>{if(!e||!e.length)return;let t=document.createElement("style");document.head.appendChild(t);let r=t.sheet;r?e.forEach(({name:n,rules:s})=>{r.insertRule(`@keyframes ${n} { ${s} }`,r.cssRules.length)}):console.error("Failed to create stylesheet")},T=W;var u={OPEN:"OPEN",CLOSE:"CLOSE",ACTIVATE:"ACTIVATE",PROMPT_LOGIN:"PROMPT_LOGIN",OPT_OUT:"OPT_OUT",ADD_KEYFRAMES:"ADD_KEYFRAMES"},j=[u.ACTIVATE],F=({event:e,iframeEl:t,promptLogin:r})=>{let{data:n}=e,{from:s,action:o,style:i,keyFrames:a,time:c,extensionId:p}=n;if(s==="bringweb3"&&!(p!==chrome.runtime.id&&!j.includes(o)))switch(o){case u.OPEN:b(t,i);break;case u.CLOSE:t&&t.parentNode?.removeChild(t),c&&chrome.runtime.sendMessage({action:o,time:c});break;case u.PROMPT_LOGIN:r();break;case u.ACTIVATE:chrome.runtime.sendMessage({action:o});break;case u.OPT_OUT:chrome.runtime.sendMessage({action:o,time:c});break;case u.ADD_KEYFRAMES:T(a);break;default:break}},k=F;var K=({walletAddressListeners:e,getWalletAddress:t,iframeEl:r})=>{for(let n=0;n<e.length;n++){let s=e[n];s&&window.addEventListener(s,async o=>{if(!r&&(r=document.querySelector(`#bringweb3-iframe:${chrome.runtime.id}`),!r)||!r.contentWindow)return;let i=await t();r.contentWindow.postMessage({action:"WALLET_ADDRESS_UPDATE",walletAddress:i},"*")})}},x=K;var h=null,D=!1,V=async({getWalletAddress:e,promptLogin:t,walletAddressListeners:r,customTheme:n,iframeEndpoint:s})=>{if(!e||!t||!r?.length||!s)throw new Error("Missing configuration");x({walletAddressListeners:r,getWalletAddress:e,iframeEl:h}),window.addEventListener("message",o=>k({event:o,iframeEl:h,promptLogin:t})),chrome.runtime.onMessage.addListener((o,i,a)=>{let{action:c}=o;switch(c){case"GET_WALLET_ADDRESS":return e().then(g=>a({status:"success",walletAddress:g})).catch(g=>a({status:"success",walletAddress:void 0})),!0;case"INJECT":if(D)return;let{token:p,page:m}=o;return h=E({query:{token:p},iframeSrc:m==="notification"?`${s}notification`:s,theme:n}),D=!0,a({status:"success"}),!0;default:console.error(`Unknown action: ${c}`);break}})},Q=V;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 B=async e=>{let t=l.getInstance().getApiEndpoint();return await(await fetch(`${t}/domains`,{headers:{"x-api-key":e}})).json()},C=B;var G=async({apiKey:e,query:t})=>{let r=l.getInstance().getApiEndpoint(),n=f({query:{...t,country:"us"}});return await(await fetch(`${r}/token?${n.toString()}`,{headers:{"x-api-key":e}})).json()},v=G;var J=async({apiKey:e,walletAddress:t,cashbackUrl:r})=>{let n=l.getInstance().getApiEndpoint(),s={walletAddress:t};return r&&(s.cashbackUrl=r),await(await fetch(`${n}/check-events`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":e},body:JSON.stringify(s)})).json()},P=J;var w="updateCache";var H=async(e,t)=>{chrome.storage.local.set({[e]:t})},Y=async e=>(await chrome.storage.local.get(e))[e],z=async e=>{chrome.storage.local.remove(e)},d={set:H,get:Y,remove:z};var S=30*60*1e3,M=async e=>{let t=await d.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 r=await A(e,{action:"GET_WALLET_ADDRESS"});r?.walletAddress&&t!==r?.walletAddress&&(t=r?.walletAddress,await d.set("walletAddress",t))}catch{}return t},X=e=>{let t=Date.now();return(e-t)/1e3/60},R=async e=>{let t=await C(e);d.set("relevantDomains",t.relevantDomains);let{nextUpdateTimestamp:r}=t,n=X(r);chrome.alarms.create(w,{delayInMinutes:n})},Z=async(e,t,r,n)=>{let s={showNotification:!1,token:""},o=await d.get("notificationCheck");if(o?.check&&Date.now()<o.check)return s;let i=await M(t);if(!i)return s;let a=await P({apiKey:e,walletAddress:i,cashbackUrl:r}),c={check:n?a.nextRequestTimestampActivated:a.nextRequestTimestampRegular,nextRequestTimestampActivated:a.nextRequestTimestampActivated,nextRequestTimestampRegular:a.nextRequestTimestampRegular};return d.set("notificationCheck",c),{showNotification:a.showNotification,token:a.token}},ee=e=>e.replace(/^(https?:\/\/)?(www\.)?/,""),O=async e=>{let t=await d.get("relevantDomains");if(!e||!t||!t.length)return"";let r=ee(e);for(let n of t)if(r.startsWith(n)){let s=await d.get("quietDomains");return s&&s[n]&&Date.now()<s[n]?"":n}return""},L=async(e,t)=>{t||(t=S);let r=await d.get("quietDomains");typeof r=="object"?r[e]=Date.now()+t:r={[e]:Date.now()+S},d.set("quietDomains",r)},te=e=>e?chrome.runtime.getURL(e):void 0,A=async(e,t)=>{for(let s=0;s<5;s++)try{let o=await chrome.tabs.get(e);return chrome.runtime.lastError?void 0:await chrome.tabs.sendMessage(e,t)}catch{s<4&&await new Promise(i=>setTimeout(i,1e3*Math.pow(2,s)))}},I=async(e,t,r)=>{let n=await Z(e,t,te(r));n.showNotification&&await A(t,{action:"INJECT",token:n.token,page:"notification"})},y={},ne=async({identifier:e,apiEndpoint:t,cashbackPagePath:r})=>{if(!e||!t)throw new Error("Missing configuration");if(!["prod","sandbox"].includes(t))throw new Error("unknown apiEndpoint");l.getInstance().setApiEndpoint(t),R(e),chrome.alarms.onAlarm.addListener(async n=>{let{name:s}=n;switch(s){case w:R(e);break;default:console.error("alarm with no use case:",s);break}}),chrome.runtime.onMessage.addListener(async(n,s)=>{let{action:o,time:i}=n;switch(o){case"ACTIVATE":let a=await d.get("notificationCheck");if(!a.check||!a.nextRequestTimestampActivated)break;a.check=a.nextRequestTimestampActivated,d.set("notificationCheck",a);break;case"OPT_OUT":d.set("optOut",Date.now()+i);break;case"CLOSE":let c=await O(s.tab?.url||s.origin);if(!c)break;L(c,i);break;default:console.warn(`Bring unknown action: ${o}`);break}}),chrome.tabs.onUpdated.addListener(async(n,s,o)=>{let i=await d.get("optOut");if(i&&i>Date.now()){await I(e,n,r);return}if(!o.url)return;let a=new URL(o.url),c=`${a.hostname.replace("www.","")}${a.pathname}`,p=y[n];if(s.status!=="complete"||c===p)return;y[n]=c;let m=await O(o.url);if(!m||!m.length){await I(e,n,r);return}let g=await M(n),{token:N,isValid:_}=await v({apiKey:e,query:{domain:m,url:o.url,address:g}});if(!_){L(m);return}A(n,{action:"INJECT",token:N,domain:c})}),chrome.tabs.onRemoved.addListener(n=>delete y[n])},re=ne;export{re as bringInitBackground,Q as bringInitContentScript};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bringweb3/chrome-extension-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"build": "tsup index.ts --format cjs,esm --dts --minify",
|
|
15
15
|
"lint": "tsc",
|
|
16
16
|
"test": "vitest --coverage",
|
|
17
|
-
"release": "yarn build
|
|
17
|
+
"release": "yarn build && changeset publish"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|