@temple-wallet/extension-ads 7.1.0-dev.3 → 7.1.0-dev.4

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,12 +1,6 @@
1
1
  import memoizee from 'memoizee';
2
+ import * as lodash from 'lodash';
2
3
 
3
- declare enum AdsBrowserExtensionMessageType {
4
- FetchReferrals = "FetchReferrals",
5
- ReferralClick = "ReferralClick"
6
- }
7
-
8
- /** Current page's domain with 'www' stripped off */
9
- declare const CURRENT_PAGE_DOMAIN: string;
10
4
  declare function processAnchors(supportedDomains: Set<string>, AdsBrowserExtensionMessageType: AdsBrowserExtensionMessageTypeI): Promise<undefined>;
11
5
  interface AdsBrowserExtensionMessageTypeI {
12
6
  FetchReferrals: string;
@@ -33,4 +27,7 @@ interface AffiliateLink {
33
27
  imageUrl: string | null;
34
28
  }
35
29
 
36
- export { AdsBrowserExtensionMessageType, CURRENT_PAGE_DOMAIN, buildTakeadsClient, processAnchors };
30
+ /** Current page's domain with 'www' stripped off */
31
+ declare const getCurrentPageDomain: (() => string) & lodash.MemoizedFunction;
32
+
33
+ export { buildTakeadsClient, getCurrentPageDomain, processAnchors };
@@ -1,18 +1,18 @@
1
1
  'use strict';
2
2
 
3
- var h = require('webextension-polyfill');
4
- var E = require('axios');
3
+ var T = require('webextension-polyfill');
4
+ var lodash = require('lodash');
5
+ var D = require('axios');
5
6
  var I = require('memoizee');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
9
 
9
- var h__default = /*#__PURE__*/_interopDefault(h);
10
- var E__default = /*#__PURE__*/_interopDefault(E);
10
+ var T__default = /*#__PURE__*/_interopDefault(T);
11
+ var D__default = /*#__PURE__*/_interopDefault(D);
11
12
  var I__default = /*#__PURE__*/_interopDefault(I);
12
13
 
13
- var f=(r,t,e)=>new Promise((o,s)=>{var a=i=>{try{n(e.next(i));}catch(c){s(c);}},l=i=>{try{n(e.throw(i));}catch(c){s(c);}},n=i=>i.done?o(i.value):Promise.resolve(i.value).then(a,l);n((e=e.apply(r,t)).next());});var d=(e=>(e.FetchReferrals="FetchReferrals",e.ReferralClick="ReferralClick",e))(d||{});var m=navigator.userAgent.includes("Macintosh"),g=r=>!!r;var A=k(window.location.hostname,"www"),T="data-tw-referral";function w(r,t){return f(this,null,function*(){var l;let e=Array.from(document.querySelectorAll("a"));if(!e.length)throw new Error("No anchors found");let o=e.map(n=>{if(n.hasAttribute(T))return null;let i=x(n.href);if(!i||!r.has(i))return null;let c=L(n.href);return c?{iri:c,aElem:n,urlDomain:i}:null}).filter(g);if(!o.length)return void console.info("Nothing to replace");let s=o.map(n=>n.iri),a=yield h__default.default.runtime.sendMessage({type:t.FetchReferrals,links:s});if(!a.data.length)return void console.info("No referrals received");console.info("Replacing",a.data.length,"referrlas");for(let{iri:n,aElem:i,urlDomain:c}of o){let p=(l=a.data.find(R=>R.iri===n))==null?void 0:l.trackingLink;if(!p){console.warn("No affiliate link for",i);continue}y({iri:n,aElem:i,urlDomain:c,referralUrl:p},t);}})}function y(r,t){let{aElem:e,urlDomain:o,referralUrl:s}=r,a=e.href;console.info("Replacing referral:",a,"to",s,"for anchor:",e),e.setAttribute(T,"set"),e.onclick=l=>{l.preventDefault(),console.log("Referral clicked:",a,"->",s),h__default.default.runtime.sendMessage({type:t.ReferralClick,urlDomain:o,pageDomain:A});let n=m?l.metaKey:l.ctrlKey;window.open(s,n?"_blank":"_self");};}function x(r){try{return k(new URL(r).hostname,"www")}catch(t){return null}}function L(r){try{let t=new URL(r);return t.origin+t.pathname}catch(t){return null}}function k(r,t){return r.includes(`${t}.`)?r.slice(t.length+1):r}var u=class{constructor(t,e="https://api.takeads.com"){this.publicKey=t;this.apiUrl=e;this.axios=E__default.default.create({baseURL:e,headers:{Authorization:`Bearer ${this.publicKey}`}});}affiliateLinks(t,e){return f(this,null,function*(){return (yield this.axios.put("/v1/product/monetize-api/v2/resolve",{iris:t,subId:e,withImages:!1})).data})}},_=I__default.default(r=>new u(r),{max:2});
14
+ var f=(e,r,t)=>new Promise((o,s)=>{var a=i=>{try{n(t.next(i));}catch(c){s(c);}},l=i=>{try{n(t.throw(i));}catch(c){s(c);}},n=i=>i.done?o(i.value):Promise.resolve(i.value).then(a,l);n((t=t.apply(e,r)).next());});var d=navigator.userAgent.includes("Macintosh"),h=e=>!!e;var u=lodash.memoize(()=>p(window.location.hostname,"www"));function p(e,r){return e.includes(`${r}.`)?e.slice(r.length+1):e}var w="data-tw-referral";function y(e,r){return f(this,null,function*(){var l;let t=Array.from(document.querySelectorAll("a"));if(!t.length)throw new Error("No anchors found");let o=t.map(n=>{if(n.hasAttribute(w))return null;let i=R(n.href);if(!i||!e.has(i))return null;let c=L(n.href);return c?{iri:c,aElem:n,urlDomain:i}:null}).filter(h);if(!o.length)return void console.info("Nothing to replace");let s=o.map(n=>n.iri),a=yield T__default.default.runtime.sendMessage({type:r.FetchReferrals,links:s});if(!a.data.length)return void console.info("No referrals received");console.info("Replacing",a.data.length,"referrlas");for(let{iri:n,aElem:i,urlDomain:c}of o){let g=(l=a.data.find(A=>A.iri===n))==null?void 0:l.trackingLink;if(!g){console.warn("No affiliate link for",i);continue}x({iri:n,aElem:i,urlDomain:c,referralUrl:g},r);}})}function x(e,r){let{aElem:t,urlDomain:o,referralUrl:s}=e,a=t.href;console.info("Replacing referral:",a,"to",s,"for anchor:",t),t.setAttribute(w,"set"),t.onclick=l=>{l.preventDefault(),console.log("Referral clicked:",a,"->",s),T__default.default.runtime.sendMessage({type:r.ReferralClick,urlDomain:o,pageDomain:u()});let n=d?l.metaKey:l.ctrlKey;window.open(s,n?"_blank":"_self");};}function R(e){try{return p(new URL(e).hostname,"www")}catch(r){return null}}function L(e){try{let r=new URL(e);return r.origin+r.pathname}catch(r){return null}}var m=class{constructor(r,t="https://api.takeads.com"){this.publicKey=r;this.apiUrl=t;this.axios=D__default.default.create({baseURL:t,headers:{Authorization:`Bearer ${this.publicKey}`},adapter:"fetch"});}affiliateLinks(r,t){return f(this,null,function*(){return (yield this.axios.put("/v1/product/monetize-api/v2/resolve",{iris:r,subId:t,withImages:!1})).data})}},b=I__default.default(e=>new m(e),{max:2});
14
15
 
15
- exports.AdsBrowserExtensionMessageType = d;
16
- exports.CURRENT_PAGE_DOMAIN = A;
17
- exports.buildTakeadsClient = _;
18
- exports.processAnchors = w;
16
+ exports.buildTakeadsClient = b;
17
+ exports.getCurrentPageDomain = u;
18
+ exports.processAnchors = y;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temple-wallet/extension-ads",
3
- "version": "7.1.0-dev.3",
3
+ "version": "7.1.0-dev.4",
4
4
  "main": "dist/index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,5 +1,5 @@
1
- export { AdsBrowserExtensionMessageType } from './constants';
2
-
3
- export { CURRENT_PAGE_DOMAIN, processAnchors } from './replace';
1
+ export { processAnchors } from './replace';
4
2
 
5
3
  export { buildTakeadsClient } from './takeads';
4
+
5
+ export { getCurrentPageDomain } from './utils';
@@ -3,9 +3,8 @@ import browser from 'webextension-polyfill';
3
3
  import { IS_MAC_OS, isTruthy } from 'src/utils';
4
4
 
5
5
  import type { TekeadsAffiliateResponse } from './takeads';
6
+ import { getCurrentPageDomain, stripSubdomain } from './utils';
6
7
 
7
- /** Current page's domain with 'www' stripped off */
8
- export const CURRENT_PAGE_DOMAIN = stripSubdomain(window.location.hostname, 'www');
9
8
  const TEMPLE_WALLET_ANCHOR_ATTRIBUTE = 'data-tw-referral';
10
9
 
11
10
  export async function processAnchors(
@@ -86,7 +85,7 @@ function processAnchorElement(item: PreppedItem, AdsBrowserExtensionMessageType:
86
85
  browser.runtime.sendMessage({
87
86
  type: AdsBrowserExtensionMessageType.ReferralClick,
88
87
  urlDomain,
89
- pageDomain: CURRENT_PAGE_DOMAIN
88
+ pageDomain: getCurrentPageDomain()
90
89
  });
91
90
 
92
91
  const newTab = IS_MAC_OS ? event.metaKey : event.ctrlKey;
@@ -112,11 +111,3 @@ function cleanLink(href: string) {
112
111
  return null;
113
112
  }
114
113
  }
115
-
116
- function stripSubdomain(hostname: string, subdomain: string) {
117
- if (hostname.includes(`${subdomain}.`)) {
118
- return hostname.slice(subdomain.length + 1);
119
- }
120
-
121
- return hostname;
122
- }
@@ -15,7 +15,8 @@ export class TakeAdsClient {
15
15
  baseURL: apiUrl,
16
16
  headers: {
17
17
  Authorization: `Bearer ${this.publicKey}`
18
- }
18
+ },
19
+ adapter: 'fetch'
19
20
  });
20
21
  }
21
22
 
@@ -0,0 +1,14 @@
1
+ import { memoize } from 'lodash';
2
+
3
+ /** Current page's domain with 'www' stripped off */
4
+ export const getCurrentPageDomain = memoize(() => {
5
+ return stripSubdomain(window.location.hostname, 'www');
6
+ });
7
+
8
+ export function stripSubdomain(hostname: string, subdomain: string) {
9
+ if (hostname.includes(`${subdomain}.`)) {
10
+ return hostname.slice(subdomain.length + 1);
11
+ }
12
+
13
+ return hostname;
14
+ }
@@ -1,4 +0,0 @@
1
- export enum AdsBrowserExtensionMessageType {
2
- FetchReferrals = 'FetchReferrals',
3
- ReferralClick = 'ReferralClick'
4
- }