@temple-wallet/extension-ads 10.1.1-dev.1638.1 → 10.1.1-dev.1638.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.
@@ -1,18 +1,189 @@
1
1
  'use strict';
2
2
 
3
- var A = require('webextension-polyfill');
3
+ var browser = require('webextension-polyfill');
4
4
  var lodash = require('lodash');
5
- var D = require('axios');
6
- var U = require('memoizee');
5
+ var axios = require('axios');
6
+ var memoizee = require('memoizee');
7
7
 
8
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
9
 
10
- var A__default = /*#__PURE__*/_interopDefault(A);
11
- var D__default = /*#__PURE__*/_interopDefault(D);
12
- var U__default = /*#__PURE__*/_interopDefault(U);
10
+ var browser__default = /*#__PURE__*/_interopDefault(browser);
11
+ var axios__default = /*#__PURE__*/_interopDefault(axios);
12
+ var memoizee__default = /*#__PURE__*/_interopDefault(memoizee);
13
13
 
14
- var h=(r,t,i)=>new Promise((u,o)=>{var g=e=>{try{a(i.next(e));}catch(s){o(s);}},l=e=>{try{a(i.throw(e));}catch(s){o(s);}},a=e=>e.done?u(e.value):Promise.resolve(e.value).then(g,l);a((i=i.apply(r,t)).next());});var m=process&&process.env&&process.env.NODE_ENV==="development",R=navigator.userAgent.includes("Macintosh"),y=r=>!!r;var x=lodash.memoize(()=>w(window.location.hostname,"www"));function w(r,t){return r.includes(`${t}.`)?r.slice(t.length+1):r}var k="data-tw-referral";function S(r,t,i,u){return h(this,null,function*(){let o=Array.from(document.querySelectorAll("a"));if(!o.length)throw new Error("No anchors found");let g=o.map(e=>{if(e.hasAttribute(k))return null;let s=I(e.href);if(!s||!r.has(s))return null;let d=b(e.href);return d?{iri:d,aElem:e,urlDomain:s}:null}).filter(y);if(!g.length)return void(m&&console.info("Nothing to replace"));let l=g.map(e=>e.iri),a=yield A__default.default.runtime.sendMessage({type:i.FetchReferrals,links:l});if(!a.data.length)return void(m&&console.info("No referrals received"));m&&console.info("Replacing",a.data.length,"referralas");for(let{iri:e,aElem:s,urlDomain:d}of g){let p=a.data.find(f=>f.iri===e),n=p==null?void 0:p.trackingLink;if(!n){m&&console.warn("No affiliate link for",s);continue}let c=p.imageUrl;L({iri:e,aElem:s,urlDomain:d,referralUrl:n,imageUrl:c},t,i,u);}})}function L(r,t,i,u){var p;let{aElem:o,urlDomain:g,referralUrl:l,imageUrl:a}=r,e=o.href,s=u?v(u,l):l;if(m&&console.info("Replacing referral:",e,"to",l,"for anchor:",o),o.setAttribute(k,"set"),o.onclick=n=>{n.preventDefault(),m&&console.log("Referral clicked:",e,"->",l),A__default.default.runtime.sendMessage({type:i.ReferralClick,urlDomain:g,pageDomain:x()});let c=n.button===1||(R?n.metaKey:n.ctrlKey);window.open(s,c?"_blank":"_self");},o.oncontextmenu=()=>{o.href=s;let n=()=>{o.href=e,window.removeEventListener("focus",n);};window.addEventListener("focus",n);},!a)return;let d=x();for(let n of t){if(new RegExp(n.hostRegExStr).test(d)===!1)continue;let c=n.aChildSelector?o.querySelector(n.aChildSelector):null;if(c||(c=n.aMatchSelector&&o.matches(n.aMatchSelector)?o:null),!c)continue;let f=document.createElement("img");f.src=a,f.height=(p=n.iconHeight)!=null?p:20,f.style.display="inline-block",f.style.marginLeft="6px",f.style.verticalAlign="middle",c.appendChild(f);return}}function v(r,t){let i=new URL(r);return i.searchParams.set("url",t),i.toString()}function I(r){try{return w(new URL(r).hostname,"www")}catch(t){return null}}function b(r){try{let t=new URL(r);return t.origin+t.pathname}catch(t){return null}}var T=class{constructor(t,i="https://api.takeads.com"){this.publicKey=t;this.apiUrl=i;this.axios=D__default.default.create({baseURL:i,headers:{Authorization:`Bearer ${this.publicKey}`},adapter:"fetch"});}affiliateLinks(t,i){return h(this,null,function*(){return (yield this.axios.put("/v1/product/monetize-api/v2/resolve",{iris:t,subId:i,withImages:!0})).data})}},C=U__default.default(r=>new T(r),{max:2});
14
+ var __async = (__this, __arguments, generator) => {
15
+ return new Promise((resolve, reject) => {
16
+ var fulfilled = (value) => {
17
+ try {
18
+ step(generator.next(value));
19
+ } catch (e) {
20
+ reject(e);
21
+ }
22
+ };
23
+ var rejected = (value) => {
24
+ try {
25
+ step(generator.throw(value));
26
+ } catch (e) {
27
+ reject(e);
28
+ }
29
+ };
30
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
31
+ step((generator = generator.apply(__this, __arguments)).next());
32
+ });
33
+ };
15
34
 
16
- exports.buildTakeadsClient = C;
17
- exports.getCurrentPageDomain = x;
18
- exports.processAnchors = S;
35
+ // src/utils.ts
36
+ var IS_DEV = process && process.env && process.env.NODE_ENV === "development";
37
+ var IS_MAC_OS = navigator.userAgent.includes("Macintosh");
38
+ var isTruthy = (value) => Boolean(value);
39
+ var getCurrentPageDomain = lodash.memoize(() => {
40
+ return stripSubdomain(window.location.hostname, "www");
41
+ });
42
+ function stripSubdomain(hostname, subdomain) {
43
+ if (hostname.includes(`${subdomain}.`)) {
44
+ return hostname.slice(subdomain.length + 1);
45
+ }
46
+ return hostname;
47
+ }
48
+
49
+ // src/referrals/replace.ts
50
+ var TEMPLE_WALLET_ANCHOR_ATTRIBUTE = "data-tw-referral";
51
+ function processAnchors(supportedDomains, textIconRules, AdsBrowserExtensionMessageType, redirectUrl) {
52
+ return __async(this, null, function* () {
53
+ const anchors = Array.from(document.querySelectorAll("a"));
54
+ if (!anchors.length)
55
+ throw new Error("No anchors found");
56
+ const items = anchors.map((aElem) => {
57
+ if (aElem.hasAttribute(TEMPLE_WALLET_ANCHOR_ATTRIBUTE))
58
+ return null;
59
+ const urlDomain = getDomain(aElem.href);
60
+ if (!urlDomain || !supportedDomains.has(urlDomain))
61
+ return null;
62
+ const iri = cleanLink(aElem.href);
63
+ if (!iri)
64
+ return null;
65
+ return { iri, aElem, urlDomain };
66
+ }).filter(isTruthy);
67
+ if (!items.length)
68
+ return void (IS_DEV && console.info("Nothing to replace"));
69
+ const links = items.map((l) => l.iri);
70
+ const takeadsItems = yield browser__default.default.runtime.sendMessage({
71
+ type: AdsBrowserExtensionMessageType.FetchReferrals,
72
+ links
73
+ });
74
+ if (!takeadsItems.data.length)
75
+ return void (IS_DEV && console.info("No referrals received"));
76
+ IS_DEV && console.info("Replacing", takeadsItems.data.length, "referralas");
77
+ for (const { iri, aElem, urlDomain } of items) {
78
+ const data = takeadsItems.data.find((item) => item.iri === iri);
79
+ const referralUrl = data == null ? void 0 : data.trackingLink;
80
+ if (!referralUrl) {
81
+ IS_DEV && console.warn("No affiliate link for", aElem);
82
+ continue;
83
+ }
84
+ const imageUrl = data.imageUrl;
85
+ processAnchorElement(
86
+ { iri, aElem, urlDomain, referralUrl, imageUrl },
87
+ textIconRules,
88
+ AdsBrowserExtensionMessageType,
89
+ redirectUrl
90
+ );
91
+ }
92
+ return;
93
+ });
94
+ }
95
+ function processAnchorElement(item, textIconRules, AdsBrowserExtensionMessageType, redirectUrl) {
96
+ var _a;
97
+ const { aElem, urlDomain, referralUrl, imageUrl } = item;
98
+ const showHref = aElem.href;
99
+ const url = redirectUrl ? buildRedirectedUrl(redirectUrl, referralUrl) : referralUrl;
100
+ IS_DEV && console.info("Replacing referral:", showHref, "to", referralUrl, "for anchor:", aElem);
101
+ aElem.setAttribute(TEMPLE_WALLET_ANCHOR_ATTRIBUTE, "set");
102
+ aElem.onclick = (event) => {
103
+ event.preventDefault();
104
+ IS_DEV && console.log("Referral clicked:", showHref, "->", referralUrl);
105
+ browser__default.default.runtime.sendMessage({
106
+ type: AdsBrowserExtensionMessageType.ReferralClick,
107
+ urlDomain,
108
+ pageDomain: getCurrentPageDomain()
109
+ });
110
+ const newTab = event.button === 1 || (IS_MAC_OS ? event.metaKey : event.ctrlKey);
111
+ window.open(url, newTab ? "_blank" : "_self");
112
+ };
113
+ aElem.oncontextmenu = () => {
114
+ aElem.href = url;
115
+ const revertHref = () => {
116
+ aElem.href = showHref;
117
+ window.removeEventListener("focus", revertHref);
118
+ };
119
+ window.addEventListener("focus", revertHref);
120
+ };
121
+ if (!imageUrl)
122
+ return;
123
+ const hostname = getCurrentPageDomain();
124
+ for (const rule of textIconRules) {
125
+ if (new RegExp(rule.hostRegExStr).test(hostname) === false)
126
+ continue;
127
+ let targetElem = rule.aChildSelector ? aElem.querySelector(rule.aChildSelector) : null;
128
+ if (!targetElem)
129
+ targetElem = rule.aMatchSelector && aElem.matches(rule.aMatchSelector) ? aElem : null;
130
+ if (!targetElem)
131
+ continue;
132
+ const icon = document.createElement("img");
133
+ icon.src = imageUrl;
134
+ icon.height = (_a = rule.iconHeight) != null ? _a : 20;
135
+ icon.style.display = "inline-block";
136
+ icon.style.marginLeft = "6px";
137
+ icon.style.verticalAlign = "middle";
138
+ targetElem.appendChild(icon);
139
+ return;
140
+ }
141
+ }
142
+ function buildRedirectedUrl(redirectUrl, url) {
143
+ const $url = new URL(redirectUrl);
144
+ $url.searchParams.set("url", url);
145
+ return $url.toString();
146
+ }
147
+ function getDomain(href) {
148
+ try {
149
+ return stripSubdomain(new URL(href).hostname, "www");
150
+ } catch (e) {
151
+ return null;
152
+ }
153
+ }
154
+ function cleanLink(href) {
155
+ try {
156
+ const dirtyLink = new URL(href);
157
+ return dirtyLink.origin + dirtyLink.pathname;
158
+ } catch (e) {
159
+ return null;
160
+ }
161
+ }
162
+ var TakeAdsClient = class {
163
+ constructor(publicKey, apiUrl = "https://api.takeads.com") {
164
+ this.publicKey = publicKey;
165
+ this.apiUrl = apiUrl;
166
+ this.axios = axios__default.default.create({
167
+ baseURL: apiUrl,
168
+ headers: {
169
+ Authorization: `Bearer ${this.publicKey}`
170
+ },
171
+ adapter: "fetch"
172
+ });
173
+ }
174
+ affiliateLinks(websiteUrls, subId) {
175
+ return __async(this, null, function* () {
176
+ const response = yield this.axios.put("/v1/product/monetize-api/v2/resolve", {
177
+ iris: websiteUrls,
178
+ subId,
179
+ withImages: true
180
+ });
181
+ return response.data;
182
+ });
183
+ }
184
+ };
185
+ var buildTakeadsClient = memoizee__default.default((publicKey) => new TakeAdsClient(publicKey), { max: 2 });
186
+
187
+ exports.buildTakeadsClient = buildTakeadsClient;
188
+ exports.getCurrentPageDomain = getCurrentPageDomain;
189
+ exports.processAnchors = processAnchors;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temple-wallet/extension-ads",
3
- "version": "10.1.1-dev.1638.1",
3
+ "version": "10.1.1-dev.1638.2",
4
4
  "main": "dist/index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,6 +28,19 @@ const adjustToAd = (wrapperElement: HTMLDivElement, adDimensions: Pick<AdDimensi
28
28
  });
29
29
  };
30
30
 
31
+ const getWrapperDimensions = (action: InsertAdAction) => {
32
+ const { adsMetadata, originalHeight, originalWidth, shouldUseBlurredBackground } = action;
33
+
34
+ const firstAdMetadataOrId = adsMetadata[0];
35
+ const { dimensions: minDimensions } =
36
+ typeof firstAdMetadataOrId === 'number' ? AdsConfiguration.bannerAdsMeta[firstAdMetadataOrId] : firstAdMetadataOrId;
37
+
38
+ return {
39
+ width: shouldUseBlurredBackground ? Math.max(originalWidth, minDimensions.width) : minDimensions.width,
40
+ height: shouldUseBlurredBackground ? Math.max(originalHeight, minDimensions.height) : minDimensions.height
41
+ };
42
+ };
43
+
31
44
  const processInsertAdActionOnce = async (
32
45
  action: InsertAdAction,
33
46
  wrapperElement: HTMLDivElement,
@@ -40,9 +53,6 @@ const processInsertAdActionOnce = async (
40
53
  stylesOverrides = [],
41
54
  adsMetadata,
42
55
  wrapperType,
43
- originalHeight,
44
- originalWidth,
45
- shouldUseBlurredBackground,
46
56
  adCategories,
47
57
  themeColor,
48
58
  fontSize,
@@ -67,13 +77,7 @@ const processInsertAdActionOnce = async (
67
77
  });
68
78
  adElement.id = adId;
69
79
  adElement.allow = IFRAME_ALLOWANCE;
70
- const firstAdMetadataOrId = adsMetadata[0];
71
- const { dimensions: minDimensions } =
72
- typeof firstAdMetadataOrId === 'number' ? AdsConfiguration.bannerAdsMeta[firstAdMetadataOrId] : firstAdMetadataOrId;
73
- const dimensions = {
74
- width: shouldUseBlurredBackground ? Math.max(originalWidth, minDimensions.width) : minDimensions.width,
75
- height: shouldUseBlurredBackground ? Math.max(originalHeight, minDimensions.height) : minDimensions.height
76
- };
80
+ const dimensions = getWrapperDimensions(action);
77
81
  adElement.style.width = wrapperType === 'tbody' ? '100%' : `${dimensions.width}px`;
78
82
  adElement.style.height = `${dimensions.height}px`;
79
83
  adElement.style.border = 'none';
@@ -82,7 +86,6 @@ const processInsertAdActionOnce = async (
82
86
  }
83
87
  adElement.setAttribute(TEMPLE_WALLET_AD_ATTRIBUTE_NAME, 'true');
84
88
 
85
- adjustToAd(wrapperElement, dimensions);
86
89
  insertionPoint.appendChild(adElement);
87
90
 
88
91
  switch (action.type) {
@@ -159,6 +162,22 @@ const processInsertAdActionOnce = async (
159
162
  });
160
163
  };
161
164
 
165
+ const hidingStylesOverrides = {
166
+ width: '0px',
167
+ height: '0px',
168
+ display: 'block',
169
+ 'overflow-x': 'hidden',
170
+ 'overflow-y': 'hidden',
171
+ 'max-block-size': 'none',
172
+ 'max-height': 'none',
173
+ 'max-inline-size': 'none',
174
+ 'max-width': 'none',
175
+ 'min-block-size': '0px',
176
+ 'min-height': '0px',
177
+ 'min-inline-size': '0px',
178
+ 'min-width': '0px'
179
+ };
180
+
162
181
  export const processInsertAdAction = async (action: InsertAdAction) => {
163
182
  const { wrapperStyle = {}, adsMetadata, wrapperType = 'div', colsAfter = 0, colsBefore = 0, colspan = 1 } = action;
164
183
  const isNativeAd = adsMetadata.every(adMetadata => typeof adMetadata === 'object');
@@ -189,7 +208,7 @@ export const processInsertAdAction = async (action: InsertAdAction) => {
189
208
  wrapperElement.setAttribute(TEMPLE_WALLET_NATIVE_AD_ATTRIBUTE_NAME, 'true');
190
209
  }
191
210
 
192
- overrideElementStyles(wrapperElement, { ...wrapperStyle, display: 'none' });
211
+ overrideElementStyles(wrapperElement, { ...wrapperStyle, ...hidingStylesOverrides });
193
212
 
194
213
  const hideFailedAd = () => {
195
214
  for (const child of insertionPoint.children) {
@@ -202,11 +221,14 @@ export const processInsertAdAction = async (action: InsertAdAction) => {
202
221
  wrapperElement,
203
222
  insertionPoint,
204
223
  () => {
205
- if (wrapperStyle.display) {
206
- wrapperElement.style.display = wrapperStyle.display;
207
- } else {
208
- wrapperElement.style.removeProperty('display');
224
+ for (const styleProp in hidingStylesOverrides) {
225
+ if (wrapperStyle[styleProp]) {
226
+ wrapperElement.style.setProperty(styleProp, wrapperStyle[styleProp]);
227
+ } else {
228
+ wrapperElement.style.removeProperty(styleProp);
229
+ }
209
230
  }
231
+ adjustToAd(wrapperElement, getWrapperDimensions(action));
210
232
  wrapperElement.removeAttribute(TEMPLE_WALLET_HIDDEN_AD_ATTRIBUTE_NAME);
211
233
  },
212
234
  error => {