@ProxyService(proxy = IWebViewLinkProxy.class) public class CustomWebViewLinkProxy implements IWebViewLinkProxy { /** * Handle custom url loading in web-view component. * Custom url is url with any custom scheme (scheme not network protocol such as http/https) * @param miniContext context of mini-program * @param url the url which is loading * @return return true if the url loading is handled, otherwise false the web-view will handle url loading */ @Override public boolean webViewCustomUrlLoading(IMiniAppContext miniContext, String url) { Uri uri = Uri.parse(url); if ("tcmpp".equals(uri.getScheme())) { Intent intent = new Intent(Intent.ACTION_VIEW, uri); miniContext.getAttachedActivity().startActivity(intent); return true; } return false; } }
game.js执行前,将自定义的 JS 注入到小游戏的GameGlobal全局对象中,那么可以实现 IJsSdkInjectProxy 接口并使用 @ProxyService注解进行注册。miniGameJSPreludeForAppInfo(MiniAppInfo) 方法,在该方法中返回需要注入的 JS 脚本字符串(返回null表示不注入)。@ProxyService(proxy = IJsSdkInjectProxy.class)注解,SDK 会自动识别并注册该代理。@ProxyService(proxy = IJsSdkInjectProxy.class)public class MiniGameJsSdkInjectProxyImpl implements IJsSdkInjectProxy {private static final String TAG = "JsSdkInjectProxy";/*** 需要注入打点 SDK 的小游戏 appId 白名单。* 留空(new HashSet<>())即表示对所有小游戏全量注入。*/private static final Set<String> INJECT_JS_APP_IDS = new HashSet<>(Arrays.asList("appid_01","appid_02"));@Overridepublic String miniGameJSPreludeForAppInfo(MiniAppInfo miniAppInfo) {if (miniAppInfo == null) return null;// 白名单非空且未命中 -> 跳过注入if (!INJECT_JS_APP_IDS.isEmpty() && !INJECT_JS_APP_IDS.contains(miniAppInfo.appId)) {return null;}return buildPrelude(miniAppInfo.appId);}/*** 读取 assets/track-sdk.js 并拼接配置头,构造最终注入脚本。*/private String buildPrelude(String appId) {try {InputStream is = CommonApp.get().getApplication().getAssets().open("track-sdk.js");ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buf = new byte[4096];int n;while ((n = is.read(buf)) != -1) out.write(buf, 0, n);is.close();String prelude = "GameGlobal.__APP_INJECT__ = { appId: '" + appId + "', debug: true };\\n";return prelude + out.toString("UTF-8");} catch (Throwable t) {Log.e(TAG, "inject track-sdk.js fail", t);return null;}}}
文档反馈