This commit is contained in:
2026-05-28 18:00:10 +08:00
parent 7201bf9845
commit a1d3fde774
6 changed files with 69 additions and 24 deletions

View File

@@ -75,6 +75,11 @@ export function openPlayworksConverter(_context: vscode.ExtensionContext) {
}
const results: NetworkResult[] = [];
try {
results.push({ network: 'un', file: copyUnityInput(opts), ok: true });
} catch (e: any) {
results.push({ network: 'un', file: '', ok: false, error: e.message });
}
for (const network of opts.networks) {
try {
const filePath = await convertNetwork(html, opts, network);
@@ -94,24 +99,25 @@ export function openPlayworksConverter(_context: vscode.ExtensionContext) {
});
}
const ZIPPED_NETWORKS = new Set(['gg', 'vu', 'mtg']);
const SUPPORTED_NETWORKS = new Set(['al', 'is', 'fb', 'gg', 'mo', 'vu', 'mtg', 'un', 'tt']);
const MRAID_NETWORKS = new Set(['al', 'is', 'un']);
const ZIPPED_NETWORKS = new Set(['gg', 'ap', 'vu', 'mtg']);
const SUPPORTED_NETWORKS = new Set(['al', 'is', 'fb', 'gg', 'ap', 'mo', 'vu', 'mtg', 'tt']);
const MRAID_NETWORKS = new Set(['al', 'is']);
const EXITAPI_NETWORKS = new Set(['gg', 'ap']);
const NETWORK_OUTPUT_FOLDERS: Record<string, string> = {
al: 'Applovin',
is: 'Ironsource',
fb: 'Facebook',
gg: 'GoogleAds',
ap: 'Appreciate',
mo: 'Moloco',
vu: 'Vungle',
mtg: 'Mintegral',
un: 'Unity',
tt: 'TikTok',
};
function sourceBaseName(sourcePath: string): string {
return path.basename(sourcePath, path.extname(sourcePath))
.replace(/_(?:un|al|is|fb|gg|mo|vu|mtg|tt)$/, '');
.replace(/_(?:un|al|is|fb|gg|ap|mo|vu|mtg|tt)$/, '');
}
function outputBaseName(opts: ConvertOptions): string {
@@ -149,6 +155,18 @@ async function convertNetwork(html: string, opts: ConvertOptions, network: strin
return outPath;
}
function copyUnityInput(opts: ConvertOptions): string {
const baseName = outputBaseName(opts);
const outputDir = path.join(opts.outputDir, 'Unity');
const outPath = path.join(outputDir, `${baseName}_un.html`);
fs.mkdirSync(outputDir, { recursive: true });
if (path.resolve(opts.sourcePath) === path.resolve(outPath)) {
return outPath;
}
fs.copyFileSync(opts.sourcePath, outPath);
return outPath;
}
function transformHtml(html: string, network: string): string {
let result = sanitizePlayworksHtml(html);
@@ -159,7 +177,7 @@ function transformHtml(html: string, network: string): string {
let headInject = buildLifecycleScript();
if (MRAID_NETWORKS.has(network)) {
headInject += '<script src="mraid.js"></script>' + buildMraidComplianceScript();
} else if (network === 'gg') {
} else if (EXITAPI_NETWORKS.has(network)) {
headInject += '<script src="exitapi.js"></script>';
}
result = injectBeforeHeadClose(result, headInject);
@@ -205,7 +223,7 @@ function trimAfterFirstHtmlClose(html: string): string {
function finalizeNetworkHtml(html: string, network: string): string {
let result = cleanupPlayworksHtml(html);
result = dedupeScriptSrc(result, 'mraid.js', MRAID_NETWORKS.has(network));
result = dedupeScriptSrc(result, 'exitapi.js', network === 'gg');
result = dedupeScriptSrc(result, 'exitapi.js', EXITAPI_NETWORKS.has(network));
return result;
}
@@ -349,7 +367,6 @@ function buildCTAScript(network: string): string {
switch (network) {
case 'al':
case 'is':
case 'un':
// MRAID — mraid.js injected in <head>
ctaLogic = `${closeCall}if(typeof mraid!=="undefined"&&typeof mraid.open==="function"){var s=typeof mraid.getState==="function"?mraid.getState():"default";if(s!=="loading"){mraid.open(o);return;}}window.open(o,"_blank");`;
break;
@@ -357,6 +374,7 @@ function buildCTAScript(network: string): string {
ctaLogic = `${closeCall}if(typeof FbPlayableAd!=="undefined"&&typeof FbPlayableAd.onCTAClick==="function"){FbPlayableAd.onCTAClick();return;}window.open(o,"_blank");`;
break;
case 'gg':
case 'ap':
// Google — exitapi.js injected in <head>
ctaLogic = `${closeCall}if(typeof ExitApi!=="undefined"&&typeof ExitApi.exit==="function"){ExitApi.exit();return;}window.open(o,"_blank");`;
break;
@@ -418,10 +436,10 @@ function getHtml(): string {
{ tag: 'is', label: 'Ironsource', note: 'HTML + mraid.js in head' },
{ tag: 'fb', label: 'Facebook', note: 'HTML' },
{ tag: 'gg', label: 'Google Ads', note: 'ZIP + exitapi.js in head' },
{ tag: 'ap', label: 'Appreciate', note: 'ZIP + exitapi.js in head' },
{ tag: 'mo', label: 'Moloco', note: 'HTML + FbPlayableAd CTA' },
{ tag: 'vu', label: 'Vungle', note: 'ZIP + __VUNGLE__ flag' },
{ tag: 'mtg', label: 'Mintegral', note: 'ZIP + onload="gameReady()"' },
{ tag: 'un', label: 'Unity', note: 'HTML + mraid.js in head' },
{ tag: 'tt', label: 'TikTok', note: 'HTML + __TIKTOK__ flag' },
];