Skip to content

Commit

Permalink
TW-1563: Implement theming for native ads that support it (#1227)
Browse files Browse the repository at this point in the history
* TW-1563 Implement theming for native ads that support it

* TW-1563 Revert early version increment

* Bump tw-ext-ads dep version

---------

Co-authored-by: Alex <alex.seleznov@gmail.com>
  • Loading branch information
keshan3262 and alex-tsx authored Dec 2, 2024
1 parent 6e8145e commit 86f39ce
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@
"follow-redirects": "^1.15.4"
},
"optionalDependencies": {
"@temple-wallet/extension-ads": "9.1.1-dev.1589.2"
"@temple-wallet/extension-ads": "10.0.1"
}
}
27 changes: 20 additions & 7 deletions src/content-scripts/replace-ads/ads-stack.iframe.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { configureAds } from 'lib/ads/configure-ads';
import { importExtensionAdsModule } from 'lib/ads/import-extension-ads-module';
import { ADS_META_SEARCH_PARAM_NAME, AD_CATEGORIES_PARAM_NAME, ORIGIN_SEARCH_PARAM_NAME } from 'lib/constants';
import {
ADS_META_SEARCH_PARAM_NAME,
AD_CATEGORIES_PARAM_NAME,
FONT_SIZE_SEARCH_PARAM_NAME,
LINE_HEIGHT_SEARCH_PARAM_NAME,
ORIGIN_SEARCH_PARAM_NAME,
THEME_COLOR_SEARCH_PARAM_NAME
} from 'lib/constants';

import { getRulesFromStorage } from './ads-rules';

const usp = new URLSearchParams(window.location.search);
const id = usp.get('id');
const rawThemeColor = usp.get(THEME_COLOR_SEARCH_PARAM_NAME);
const rawFontSize = usp.get(FONT_SIZE_SEARCH_PARAM_NAME);
const rawLineHeight = usp.get(LINE_HEIGHT_SEARCH_PARAM_NAME);
const origin = usp.get(ORIGIN_SEARCH_PARAM_NAME) ?? window.location.href;
const adsMetadataIds = usp.getAll(ADS_META_SEARCH_PARAM_NAME).map(value => JSON.parse(value));
const adCategories = usp.getAll(AD_CATEGORIES_PARAM_NAME);
Expand All @@ -16,13 +26,16 @@ configureAds()
.then(({ renderAdsStack, rules }) => {
const { blacklistedHypelabCampaignsSlugs, permanentAdPlacesRules, adPlacesRules } = rules;

renderAdsStack(
id ?? '',
adsMetadataIds,
renderAdsStack({
id: id ?? '',
initialAdsMetadata: adsMetadataIds,
origin,
permanentAdPlacesRules.length > 0 || adPlacesRules.length > 0,
pageHasPlacesRules: permanentAdPlacesRules.length > 0 || adPlacesRules.length > 0,
adCategories,
blacklistedHypelabCampaignsSlugs
);
hypelabBlacklistedCampaignsSlugs: blacklistedHypelabCampaignsSlugs,
themeColor: rawThemeColor ?? undefined,
fontSize: rawFontSize ? Number(rawFontSize) : undefined,
lineHeight: rawLineHeight ? Number(rawLineHeight) : undefined
});
})
.catch(error => console.error(error));
40 changes: 36 additions & 4 deletions src/lib/ads/configure-ads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import {
ADS_META_SEARCH_PARAM_NAME,
AD_CATEGORIES_PARAM_NAME,
ContentScriptType,
ORIGIN_SEARCH_PARAM_NAME
FONT_SIZE_SEARCH_PARAM_NAME,
LINE_HEIGHT_SEARCH_PARAM_NAME,
ORIGIN_SEARCH_PARAM_NAME,
THEME_COLOR_SEARCH_PARAM_NAME
} from 'lib/constants';
import { APP_VERSION, EnvVars, IS_MISES_BROWSER } from 'lib/env';
import { isTruthy } from 'lib/utils';

import { importExtensionAdsModule } from './import-extension-ads-module';

// Three interfaces below are copied from '@temple-wallet/extension-ads' to avoid importing it to ensure that a core
// Four interfaces below are copied from '@temple-wallet/extension-ads' to avoid importing it to ensure that a core
// build runs without errors.
interface AdSource {
shouldNotUseStrictContainerLimits?: boolean;
Expand All @@ -36,6 +39,16 @@ interface AdMetadata {
dimensions: AdDimensions;
}

interface AdsStackIframeURLParams {
id: string;
adsMetadataIds: any[];
origin: string;
adCategories: string[];
themeColor?: string;
fontSize?: number;
lineHeight?: number;
}

const smallTkeyInpageAdUrl = browser.runtime.getURL(`/misc/ad-banners/small-tkey-inpage-ad.png`);
const tkeyInpageAdUrl = browser.runtime.getURL(`/misc/ad-banners/tkey-inpage-ad.png`);

Expand All @@ -45,9 +58,28 @@ const swapTkeyUrl = `${browser.runtime.getURL('fullpage.html')}#/swap?${buildSwa
true
)}`;

const getAdsStackIframeURL = (id: string, adsMetadataIds: any[], origin: string, adCategories: string[]) => {
const searchParamsNames = {
id: 'id',
themeColor: THEME_COLOR_SEARCH_PARAM_NAME,
fontSize: FONT_SIZE_SEARCH_PARAM_NAME,
lineHeight: LINE_HEIGHT_SEARCH_PARAM_NAME
};

const getAdsStackIframeURL = ({
adsMetadataIds,
origin,
adCategories,
...plainSearchParamsInput
}: AdsStackIframeURLParams) => {
const url = new URL(browser.runtime.getURL('iframes/ads-stack.html'));
url.searchParams.set('id', id);
for (const searchParamsInputName in plainSearchParamsInput) {
const searchParamsInputNameTyped = searchParamsInputName as keyof typeof plainSearchParamsInput;
const key = searchParamsNames[searchParamsInputNameTyped];
const value = plainSearchParamsInput[searchParamsInputNameTyped];
if (value || value === 0) {
url.searchParams.set(key, String(value));
}
}
adsMetadataIds.forEach(adMetadataId =>
url.searchParams.append(ADS_META_SEARCH_PARAM_NAME, JSON.stringify(adMetadataId))
);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ export const searchHotkey = ` (${isMacOS ? '⌘' : 'Ctrl + '}K)`;
export const FEE_PER_GAS_UNIT = 0.1;

export const LIQUIDITY_BAKING_DEX_ADDRESS = 'KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5';

export const THEME_COLOR_SEARCH_PARAM_NAME = 'tc';
export const FONT_SIZE_SEARCH_PARAM_NAME = 'fs';
export const LINE_HEIGHT_SEARCH_PARAM_NAME = 'lh';
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3657,10 +3657,10 @@
dependencies:
nanoid "^3.1.25"

"@temple-wallet/extension-ads@9.1.1-dev.1589.2":
version "9.1.1-dev.1589.2"
resolved "https://registry.yarnpkg.com/@temple-wallet/extension-ads/-/extension-ads-9.1.1-dev.1589.2.tgz#6f57e8881016769018671ddfcaa2417647d74d45"
integrity sha512-upUeIqLT5L3z5loTM4kTEWLYKldYV7j23aa/tpL/7VNknGGvGWP1wzu3rge+FSYg7pex6swmQpV1URrdpbxLQA==
"@temple-wallet/extension-ads@10.0.1":
version "10.0.1"
resolved "https://registry.yarnpkg.com/@temple-wallet/extension-ads/-/extension-ads-10.0.1.tgz#76a05a83b037ad539d2a67a271ddb1cc35025660"
integrity sha512-rRY2xFvSwJXhLLOeOmxfJqprAGfPtpoWOmFxgF1Rx/f3ajogqNYcYH0dLn8Q3T8Vit2xzr5T4S7BdzDbLijNSQ==
dependencies:
axios "^1.7.4"
crypto-js "^4.2.0"
Expand Down

0 comments on commit 86f39ce

Please sign in to comment.