@playcraft/build 0.2.20 → 0.2.21
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.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/platforms/base.d.ts +4 -0
- package/dist/platforms/base.js +15 -23
- package/dist/redirect-artifact.d.ts +30 -0
- package/dist/redirect-artifact.js +83 -0
- package/dist/redirect-policy.d.ts +23 -0
- package/dist/redirect-policy.js +25 -0
- package/dist/types.d.ts +3 -0
- package/package.json +1 -1
- package/templates/patches/playcraft-redirect-policy.js +131 -0
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ export { BaseBuilder } from './base-builder.js';
|
|
|
2
2
|
export type { BaseBuildOptions, BaseBuildOutput } from './base-builder.js';
|
|
3
3
|
export { ViteBuilder } from './vite-builder.js';
|
|
4
4
|
export type { ViteBuildOutput } from './vite-builder.js';
|
|
5
|
+
export { normalizePlayableRedirect, PLAYABLE_REDIRECT_LIMITS } from './redirect-policy.js';
|
|
6
|
+
export { injectRedirectPolicyIntoArtifact } from './redirect-artifact.js';
|
|
7
|
+
export type { PlayableStoreUrls } from './redirect-artifact.js';
|
|
8
|
+
export type { PlayableRedirectRule } from './redirect-policy.js';
|
|
5
9
|
export { PlayableBuilder } from './playable-builder.js';
|
|
6
10
|
export type { PlayableBuildOutput } from './playable-builder.js';
|
|
7
11
|
export { OnePageConverter } from './converter.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// 导出构建器
|
|
2
2
|
export { BaseBuilder } from './base-builder.js';
|
|
3
3
|
export { ViteBuilder } from './vite-builder.js';
|
|
4
|
+
export { normalizePlayableRedirect, PLAYABLE_REDIRECT_LIMITS } from './redirect-policy.js';
|
|
5
|
+
export { injectRedirectPolicyIntoArtifact } from './redirect-artifact.js';
|
|
4
6
|
export { PlayableBuilder } from './playable-builder.js';
|
|
5
7
|
export { OnePageConverter } from './converter.js';
|
|
6
8
|
// 导出引擎检测器和适配器
|
package/dist/platforms/base.d.ts
CHANGED
|
@@ -55,6 +55,10 @@ export declare abstract class PlatformAdapter {
|
|
|
55
55
|
* 在 HTML 中注入 CTA 适配器(异步版本)
|
|
56
56
|
*/
|
|
57
57
|
protected injectCTAAdapterAsync(html: string): Promise<string>;
|
|
58
|
+
/**
|
|
59
|
+
* 跳转版本规则脚本:未配置有效 `redirect` 时返回空串,普通构建的产物保持不变。
|
|
60
|
+
*/
|
|
61
|
+
protected getRedirectPolicyScriptAsync(): Promise<string>;
|
|
58
62
|
/**
|
|
59
63
|
* 在 HTML 中注入 CTA 适配器(同步版本)
|
|
60
64
|
* @deprecated 推荐使用 injectCTAAdapterAsync
|
package/dist/platforms/base.js
CHANGED
|
@@ -2,8 +2,7 @@ import fs from 'fs/promises';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { fileURLToPath } from 'url';
|
|
4
4
|
import { minifyPatchCode } from '../utils/minify.js';
|
|
5
|
-
|
|
6
|
-
let cachedMinifiedCTAAdapter = null;
|
|
5
|
+
import { ctaAdapterScript, redirectPolicyScript, storeUrlsScript } from '../redirect-artifact.js';
|
|
7
6
|
// 缓存压缩后的 Tracking 适配器代码
|
|
8
7
|
let cachedMinifiedTrackingAdapter = null;
|
|
9
8
|
// 缓存从 @playcraft/ads-tracking 提取的 tracking 实现代码
|
|
@@ -47,29 +46,14 @@ export class PlatformAdapter {
|
|
|
47
46
|
* 获取 CTA 适配器脚本(异步版本,带压缩)
|
|
48
47
|
*/
|
|
49
48
|
async getCTAAdapterScriptAsync() {
|
|
50
|
-
|
|
51
|
-
return `<script>${cachedMinifiedCTAAdapter}</script>`;
|
|
52
|
-
}
|
|
53
|
-
const ctaCode = await this.readTemplateFile('playcraft-cta-adapter.js');
|
|
54
|
-
const minified = await minifyPatchCode(ctaCode, 'playcraft-cta-adapter.js');
|
|
55
|
-
cachedMinifiedCTAAdapter = minified;
|
|
56
|
-
return `<script>${minified}</script>`;
|
|
49
|
+
return ctaAdapterScript();
|
|
57
50
|
}
|
|
58
51
|
/**
|
|
59
52
|
* 生成 Store URLs 注入脚本
|
|
60
53
|
* 将 storeUrls 以全局变量写入 HTML,供 CTA 适配器读取
|
|
61
54
|
*/
|
|
62
55
|
getStoreUrlsScript() {
|
|
63
|
-
|
|
64
|
-
// 只有存在有效 URL 时才注入
|
|
65
|
-
if (!urls.ios && !urls.android) {
|
|
66
|
-
return '';
|
|
67
|
-
}
|
|
68
|
-
const safeUrls = {
|
|
69
|
-
...(urls.ios ? { ios: urls.ios } : {}),
|
|
70
|
-
...(urls.android ? { android: urls.android } : {}),
|
|
71
|
-
};
|
|
72
|
-
return `<script>window.__PLAYCRAFT_STORE_URLS__=${JSON.stringify(safeUrls)};</script>`;
|
|
56
|
+
return storeUrlsScript(this.options.storeUrls);
|
|
73
57
|
}
|
|
74
58
|
/**
|
|
75
59
|
* 获取 CTA 适配器脚本(同步版本,使用预压缩代码)
|
|
@@ -87,18 +71,26 @@ export class PlatformAdapter {
|
|
|
87
71
|
async injectCTAAdapterAsync(html) {
|
|
88
72
|
const storeUrlsScript = this.getStoreUrlsScript();
|
|
89
73
|
const ctaScript = await this.getCTAAdapterScriptAsync();
|
|
90
|
-
|
|
91
|
-
|
|
74
|
+
// 跳转版本规则必须紧跟 CTA 适配器:它包装 window.PlayCraftCTA 做去重
|
|
75
|
+
const redirectScript = await this.getRedirectPolicyScriptAsync();
|
|
76
|
+
const combined = storeUrlsScript + ctaScript + redirectScript;
|
|
77
|
+
// 在 </head> 之前或 </body> 之前注入(函数替换:脚本内容里的 `$` 不会被当成替换模式)
|
|
92
78
|
if (html.includes('</head>')) {
|
|
93
|
-
return html.replace('</head>', `${combined}\n</head>`);
|
|
79
|
+
return html.replace('</head>', () => `${combined}\n</head>`);
|
|
94
80
|
}
|
|
95
81
|
else if (html.includes('</body>')) {
|
|
96
|
-
return html.replace('</body>', `${combined}\n</body>`);
|
|
82
|
+
return html.replace('</body>', () => `${combined}\n</body>`);
|
|
97
83
|
}
|
|
98
84
|
else {
|
|
99
85
|
return combined + '\n' + html;
|
|
100
86
|
}
|
|
101
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* 跳转版本规则脚本:未配置有效 `redirect` 时返回空串,普通构建的产物保持不变。
|
|
90
|
+
*/
|
|
91
|
+
async getRedirectPolicyScriptAsync() {
|
|
92
|
+
return redirectPolicyScript(this.options.redirect);
|
|
93
|
+
}
|
|
102
94
|
/**
|
|
103
95
|
* 在 HTML 中注入 CTA 适配器(同步版本)
|
|
104
96
|
* @deprecated 推荐使用 injectCTAAdapterAsync
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 跳转版本的注入脚本:商店链接、CTA 适配器、跳转规则。渠道适配器(`PlatformAdapter`)在打渠道包时
|
|
3
|
+
* 注入它们;不经过渠道适配、自己产出渠道包的工程(如 devkit / playable-scripts)由
|
|
4
|
+
* `injectRedirectPolicyIntoArtifact` 在拿到最终产物后补上同样的脚本。
|
|
5
|
+
*/
|
|
6
|
+
export type PlayableStoreUrls = {
|
|
7
|
+
ios?: string;
|
|
8
|
+
android?: string;
|
|
9
|
+
};
|
|
10
|
+
/** 商店链接全局变量,供 CTA 适配器读取;没有有效链接时返回空串。 */
|
|
11
|
+
export declare function storeUrlsScript(urls?: PlayableStoreUrls): string;
|
|
12
|
+
/** CTA 适配器:运行时识别渠道并提供 `window.PlayCraftCTA.jump()`。 */
|
|
13
|
+
export declare function ctaAdapterScript(): Promise<string>;
|
|
14
|
+
/** 跳转规则脚本:未配置有效 `redirect` 时返回空串,普通构建的产物保持不变。 */
|
|
15
|
+
export declare function redirectPolicyScript(redirect: unknown): Promise<string>;
|
|
16
|
+
/** 在 `</head>` 之前(没有时 `</body>` 之前,再没有时开头)插入脚本;函数替换避免 `$` 被当成替换模式。 */
|
|
17
|
+
export declare function insertScripts(html: string, scripts: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* 给已打好的渠道产物(单 HTML 或 ZIP)补上跳转版本脚本,原地改写。脚本与渠道适配器注入的相同:
|
|
20
|
+
* CTA 适配器在运行时识别渠道,跳转规则包装它的 `jump()` 并旁听点击计数。没有有效规则时不改动产物;
|
|
21
|
+
* 产物里找不到可注入的 HTML 时报错,避免导出名义上带跳转、实际没有的版本。
|
|
22
|
+
*
|
|
23
|
+
* `adapter: false` 用于渠道适配器已打好的产物:其中已有商店链接和 CTA 适配器,只补跳转规则,
|
|
24
|
+
* 插在它们之后。
|
|
25
|
+
*/
|
|
26
|
+
export declare function injectRedirectPolicyIntoArtifact(filePath: string, options: {
|
|
27
|
+
redirect?: unknown;
|
|
28
|
+
storeUrls?: PlayableStoreUrls;
|
|
29
|
+
adapter?: boolean;
|
|
30
|
+
}): Promise<boolean>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { strFromU8, strToU8, unzipSync, zipSync } from 'fflate';
|
|
5
|
+
import { minifyPatchCode } from './utils/minify.js';
|
|
6
|
+
import { normalizePlayableRedirect } from './redirect-policy.js';
|
|
7
|
+
async function patchScript(filename) {
|
|
8
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const code = await fs.readFile(path.resolve(currentDir, '../templates/patches', filename), 'utf-8');
|
|
10
|
+
return minifyPatchCode(code, filename);
|
|
11
|
+
}
|
|
12
|
+
/** 商店链接全局变量,供 CTA 适配器读取;没有有效链接时返回空串。 */
|
|
13
|
+
export function storeUrlsScript(urls) {
|
|
14
|
+
const value = urls || {};
|
|
15
|
+
if (!value.ios && !value.android)
|
|
16
|
+
return '';
|
|
17
|
+
const safeUrls = {
|
|
18
|
+
...(value.ios ? { ios: value.ios } : {}),
|
|
19
|
+
...(value.android ? { android: value.android } : {}),
|
|
20
|
+
};
|
|
21
|
+
return `<script>window.__PLAYCRAFT_STORE_URLS__=${JSON.stringify(safeUrls)};</script>`;
|
|
22
|
+
}
|
|
23
|
+
/** CTA 适配器:运行时识别渠道并提供 `window.PlayCraftCTA.jump()`。 */
|
|
24
|
+
export async function ctaAdapterScript() {
|
|
25
|
+
return `<script>${await patchScript('playcraft-cta-adapter.js')}</script>`;
|
|
26
|
+
}
|
|
27
|
+
/** 跳转规则脚本:未配置有效 `redirect` 时返回空串,普通构建的产物保持不变。 */
|
|
28
|
+
export async function redirectPolicyScript(redirect) {
|
|
29
|
+
const rule = normalizePlayableRedirect(redirect);
|
|
30
|
+
if (!rule)
|
|
31
|
+
return '';
|
|
32
|
+
return (`<script>window.__PLAYCRAFT_REDIRECT__=${JSON.stringify(rule)};</script>` +
|
|
33
|
+
`<script>${await patchScript('playcraft-redirect-policy.js')}</script>`);
|
|
34
|
+
}
|
|
35
|
+
/** 在 `</head>` 之前(没有时 `</body>` 之前,再没有时开头)插入脚本;函数替换避免 `$` 被当成替换模式。 */
|
|
36
|
+
export function insertScripts(html, scripts) {
|
|
37
|
+
if (!scripts)
|
|
38
|
+
return html;
|
|
39
|
+
if (html.includes('</head>'))
|
|
40
|
+
return html.replace('</head>', () => `${scripts}\n</head>`);
|
|
41
|
+
if (html.includes('</body>'))
|
|
42
|
+
return html.replace('</body>', () => `${scripts}\n</body>`);
|
|
43
|
+
return `${scripts}\n${html}`;
|
|
44
|
+
}
|
|
45
|
+
const UNSUPPORTED = 'redirect_versions_unsupported';
|
|
46
|
+
/** ZIP 渠道包里的入口 HTML:优先根目录 index.html,否则层级最浅的 HTML。 */
|
|
47
|
+
function zipEntryHtml(names) {
|
|
48
|
+
const html = names.filter((name) => /\.html?$/i.test(name) && !name.endsWith('/'));
|
|
49
|
+
return (html.find((name) => name.toLowerCase() === 'index.html') ??
|
|
50
|
+
html.sort((a, b) => a.split('/').length - b.split('/').length || a.length - b.length)[0]);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 给已打好的渠道产物(单 HTML 或 ZIP)补上跳转版本脚本,原地改写。脚本与渠道适配器注入的相同:
|
|
54
|
+
* CTA 适配器在运行时识别渠道,跳转规则包装它的 `jump()` 并旁听点击计数。没有有效规则时不改动产物;
|
|
55
|
+
* 产物里找不到可注入的 HTML 时报错,避免导出名义上带跳转、实际没有的版本。
|
|
56
|
+
*
|
|
57
|
+
* `adapter: false` 用于渠道适配器已打好的产物:其中已有商店链接和 CTA 适配器,只补跳转规则,
|
|
58
|
+
* 插在它们之后。
|
|
59
|
+
*/
|
|
60
|
+
export async function injectRedirectPolicyIntoArtifact(filePath, options) {
|
|
61
|
+
const policy = await redirectPolicyScript(options.redirect);
|
|
62
|
+
if (!policy)
|
|
63
|
+
return false;
|
|
64
|
+
const scripts = options.adapter === false
|
|
65
|
+
? policy
|
|
66
|
+
: storeUrlsScript(options.storeUrls) + (await ctaAdapterScript()) + policy;
|
|
67
|
+
const lower = filePath.toLowerCase();
|
|
68
|
+
if (/\.html?$/.test(lower)) {
|
|
69
|
+
const html = await fs.readFile(filePath, 'utf-8');
|
|
70
|
+
await fs.writeFile(filePath, insertScripts(html, scripts));
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (lower.endsWith('.zip')) {
|
|
74
|
+
const entries = unzipSync(new Uint8Array(await fs.readFile(filePath)));
|
|
75
|
+
const entry = zipEntryHtml(Object.keys(entries));
|
|
76
|
+
if (!entry)
|
|
77
|
+
throw new Error(`${UNSUPPORTED}: the channel package has no HTML entry`);
|
|
78
|
+
entries[entry] = strToU8(insertScripts(strFromU8(entries[entry]), scripts));
|
|
79
|
+
await fs.writeFile(filePath, zipSync(entries, { level: 9 }));
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
throw new Error(`${UNSUPPORTED}: unsupported artifact ${path.basename(filePath)}`);
|
|
83
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 跳转版本规则:同一份游戏源码按「第 N 次点击起跳商店」或「游玩满 N 秒后下一次点击跳商店」
|
|
3
|
+
* 打出多个版本。规则在构建时随渠道适配器注入 HTML,由 templates/patches/playcraft-redirect-policy.js
|
|
4
|
+
* 在运行时执行,不改动游戏代码。
|
|
5
|
+
*/
|
|
6
|
+
export type PlayableRedirectRule = {
|
|
7
|
+
/** 从第 N 次点击起,每次点击都直接跳商店 */
|
|
8
|
+
clicks?: number;
|
|
9
|
+
/** 首次触摸后累计游玩满 N 秒,下一次点击跳商店 */
|
|
10
|
+
seconds?: number;
|
|
11
|
+
};
|
|
12
|
+
export declare const PLAYABLE_REDIRECT_LIMITS: {
|
|
13
|
+
readonly clicks: {
|
|
14
|
+
readonly min: 1;
|
|
15
|
+
readonly max: 20;
|
|
16
|
+
};
|
|
17
|
+
readonly seconds: {
|
|
18
|
+
readonly min: 1;
|
|
19
|
+
readonly max: 600;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
/** 规范化跳转规则:无有效阈值时返回 undefined(不注入任何脚本)。 */
|
|
23
|
+
export declare function normalizePlayableRedirect(value: unknown): PlayableRedirectRule | undefined;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const PLAYABLE_REDIRECT_LIMITS = {
|
|
2
|
+
clicks: { min: 1, max: 20 },
|
|
3
|
+
seconds: { min: 1, max: 600 },
|
|
4
|
+
};
|
|
5
|
+
function boundedInteger(value, limits) {
|
|
6
|
+
if (typeof value !== 'number' || !Number.isInteger(value))
|
|
7
|
+
return undefined;
|
|
8
|
+
if (value < limits.min || value > limits.max)
|
|
9
|
+
return undefined;
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
/** 规范化跳转规则:无有效阈值时返回 undefined(不注入任何脚本)。 */
|
|
13
|
+
export function normalizePlayableRedirect(value) {
|
|
14
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
15
|
+
return undefined;
|
|
16
|
+
const source = value;
|
|
17
|
+
const clicks = boundedInteger(source.clicks, PLAYABLE_REDIRECT_LIMITS.clicks);
|
|
18
|
+
const seconds = boundedInteger(source.seconds, PLAYABLE_REDIRECT_LIMITS.seconds);
|
|
19
|
+
if (clicks === undefined && seconds === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
return {
|
|
22
|
+
...(clicks !== undefined ? { clicks } : {}),
|
|
23
|
+
...(seconds !== undefined ? { seconds } : {}),
|
|
24
|
+
};
|
|
25
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PlayableRedirectRule } from './redirect-policy.js';
|
|
1
2
|
export type Platform = 'playcraft' | 'facebook' | 'snapchat' | 'ironsource' | 'applovin' | 'google' | 'tiktok' | 'unity' | 'liftoff' | 'moloco' | 'bigo' | 'inmobi' | 'adikteev' | 'remerge' | 'mintegral';
|
|
2
3
|
export type OutputFormat = 'html' | 'zip';
|
|
3
4
|
/**
|
|
@@ -64,6 +65,8 @@ export interface BuildOptions {
|
|
|
64
65
|
adNetwork?: string;
|
|
65
66
|
totalInteractions?: number;
|
|
66
67
|
};
|
|
68
|
+
/** 跳转版本规则(点击次数 / 游玩时长);仅在有效时随 CTA 适配器注入 */
|
|
69
|
+
redirect?: PlayableRedirectRule;
|
|
67
70
|
}
|
|
68
71
|
export interface BaseBuildMetadata {
|
|
69
72
|
mode: BuildMode;
|
package/package.json
CHANGED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PlayCraft 跳转版本规则(构建时随 CTA 适配器注入,紧跟在 playcraft-cta-adapter.js 之后)
|
|
3
|
+
*
|
|
4
|
+
* 读取 window.__PLAYCRAFT_REDIRECT__ = { clicks?, seconds? }:
|
|
5
|
+
* - clicks:从第 N 次点击起,每次点击都直接跳商店;
|
|
6
|
+
* - seconds:首次触摸后累计游玩满 N 秒(页面隐藏时不计时),下一次点击跳商店。
|
|
7
|
+
*
|
|
8
|
+
* 约束:
|
|
9
|
+
* - 只旁听点击(捕获阶段 + passive),不拦截、不阻止事件,游戏照常收到每一次点击;
|
|
10
|
+
* - 跳转只由点击触发,从不自动跳转(渠道会拒绝无用户操作的跳转);
|
|
11
|
+
* - 1 秒内重复的 CTA 调用只执行一次,避免和游戏自己的结束卡在同一次点击里各跳一次;
|
|
12
|
+
* - 游戏自己调用过 CTA,或游戏结束(SDK 的 playcraftFinish、playcraft:finish 或
|
|
13
|
+
* playcraft:game-end 事件)后,规则停止生效:游戏先于阈值结束时,结算页交给游戏自己处理,
|
|
14
|
+
* 不再计数和计时,避免结算页上的「再玩一次」被当成第 N 次点击带去商店。
|
|
15
|
+
*/
|
|
16
|
+
(function () {
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
var rule = typeof window !== 'undefined' ? window.__PLAYCRAFT_REDIRECT__ : null;
|
|
20
|
+
if (!rule || typeof rule !== 'object') return;
|
|
21
|
+
var clicks = rule.clicks > 0 ? Math.floor(rule.clicks) : 0;
|
|
22
|
+
var seconds = rule.seconds > 0 ? Number(rule.seconds) : 0;
|
|
23
|
+
if (!clicks && !seconds) return;
|
|
24
|
+
|
|
25
|
+
var DEDUPE_MS = 1000;
|
|
26
|
+
var taps = 0;
|
|
27
|
+
var playedMs = 0;
|
|
28
|
+
var running = false;
|
|
29
|
+
var runningSince = 0;
|
|
30
|
+
var interacted = false;
|
|
31
|
+
var stopped = false;
|
|
32
|
+
var redirects = 0;
|
|
33
|
+
var lastJumpAt = -Infinity;
|
|
34
|
+
var policyJumping = false;
|
|
35
|
+
|
|
36
|
+
function now() {
|
|
37
|
+
return typeof performance !== 'undefined' && performance.now ? performance.now() : Date.now();
|
|
38
|
+
}
|
|
39
|
+
function pageVisible() {
|
|
40
|
+
return typeof document === 'undefined' || document.visibilityState !== 'hidden';
|
|
41
|
+
}
|
|
42
|
+
function played() {
|
|
43
|
+
return playedMs + (running ? now() - runningSince : 0);
|
|
44
|
+
}
|
|
45
|
+
function pause() {
|
|
46
|
+
if (running) playedMs += now() - runningSince;
|
|
47
|
+
running = false;
|
|
48
|
+
}
|
|
49
|
+
function resume() {
|
|
50
|
+
running = true;
|
|
51
|
+
runningSince = now();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
document.addEventListener('visibilitychange', function () {
|
|
55
|
+
if (!pageVisible()) pause();
|
|
56
|
+
else if (interacted && !running && !stopped) resume();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
function stop() {
|
|
60
|
+
pause();
|
|
61
|
+
stopped = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* 包住所有 CTA 入口:去重,并识别游戏自己发起的跳转 */
|
|
65
|
+
var cta = window.PlayCraftCTA;
|
|
66
|
+
function guard(original) {
|
|
67
|
+
return function () {
|
|
68
|
+
var at = now();
|
|
69
|
+
if (at - lastJumpAt < DEDUPE_MS) return;
|
|
70
|
+
lastJumpAt = at;
|
|
71
|
+
if (!policyJumping) stop();
|
|
72
|
+
return original.apply(this, arguments);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (cta && typeof cta.jump === 'function') {
|
|
76
|
+
var guardedJump = guard(cta.jump);
|
|
77
|
+
cta.jump = guardedJump;
|
|
78
|
+
cta.jump2AppStore = guardedJump;
|
|
79
|
+
window.jump2AppStore = guardedJump;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* 游戏结束:SDK 可能晚于本脚本加载,绑定不到时短暂重试 */
|
|
83
|
+
window.addEventListener('playcraft:game-end', stop);
|
|
84
|
+
document.addEventListener('playcraft:finish', stop);
|
|
85
|
+
var bindAttempts = 0;
|
|
86
|
+
function bindPlayableSdk() {
|
|
87
|
+
var sdk = window.PlayableSDK;
|
|
88
|
+
if (sdk && typeof sdk.once === 'function') {
|
|
89
|
+
if (sdk.isFinished === true) stop();
|
|
90
|
+
else sdk.once('playcraftFinish', stop);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
bindAttempts += 1;
|
|
94
|
+
if (bindAttempts < 300 && typeof setTimeout === 'function') setTimeout(bindPlayableSdk, 100);
|
|
95
|
+
}
|
|
96
|
+
bindPlayableSdk();
|
|
97
|
+
|
|
98
|
+
function onTap(event) {
|
|
99
|
+
if (stopped) return;
|
|
100
|
+
if (event && event.isPrimary === false) return;
|
|
101
|
+
if (!interacted) {
|
|
102
|
+
interacted = true;
|
|
103
|
+
if (pageVisible()) resume();
|
|
104
|
+
}
|
|
105
|
+
taps += 1;
|
|
106
|
+
var reached = (clicks && taps >= clicks) || (seconds && played() >= seconds * 1000);
|
|
107
|
+
if (!reached || !cta || typeof cta.jump !== 'function') return;
|
|
108
|
+
redirects += 1;
|
|
109
|
+
policyJumping = true;
|
|
110
|
+
try {
|
|
111
|
+
cta.jump();
|
|
112
|
+
} finally {
|
|
113
|
+
policyJumping = false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
var tapEvent =
|
|
118
|
+
typeof window.PointerEvent === 'function'
|
|
119
|
+
? 'pointerdown'
|
|
120
|
+
: 'ontouchstart' in window
|
|
121
|
+
? 'touchstart'
|
|
122
|
+
: 'mousedown';
|
|
123
|
+
window.addEventListener(tapEvent, onTap, { capture: true, passive: true });
|
|
124
|
+
|
|
125
|
+
window.PlayCraftRedirect = {
|
|
126
|
+
rule: { clicks: clicks || undefined, seconds: seconds || undefined },
|
|
127
|
+
state: function () {
|
|
128
|
+
return { taps: taps, playedMs: Math.round(played()), stopped: stopped, redirects: redirects };
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
})();
|