tencent cloud

腾讯云超级应用服务

其他

Download
聚焦模式
字号
最后更新时间: 2026-07-01 18:03:03

WebView 组件中特殊链接处理

如果 web-view 组件展示的网页中存在特殊的 URL 链接,例如 tcmpp://host/path 这种以自定义 scheme 开头的链接,宿主可以接管这些链接的跳转操作,并自定义跳转行为。
接管链接跳转应当创建类实现 IWebViewLinkProxy 接口,并添加 ProxyService 注解:
@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; } }
实现方法中提供了小程序上下文以及被加载的链接。如果开发者处理了链接跳转并返回 true,web-view 将不再处理该链接。如果返回 false,web-view 将按照正常逻辑处理该链接。


小游戏动态注入 js 加载

如果你想在小游戏基础库加载完成后、game.js执行前,将自定义的 JS 注入到小游戏的GameGlobal全局对象中,那么可以实现 IJsSdkInjectProxy 接口并使用 @ProxyService注解进行注册。
具体接入方式:
1. 创建代理实现类:新建一个类实现 IJsSdkInjectProxy 接口,实现miniGameJSPreludeForAppInfo(MiniAppInfo) 方法,在该方法中返回需要注入的 JS 脚本字符串(返回null表示不注入)。
2. 添加注解注册:在类上添加@ProxyService(proxy = IJsSdkInjectProxy.class)注解,SDK 会自动识别并注册该代理。
3. 准备 JS 文件:按照业务逻辑从服务端拉取,或从 App 的 assets 目录导入。以下示例演示如何将需要注入的 JS 文件(如 track-sdk.js)放置到 App 的 assets 目录下注入。
4. 配置注入范围:通过 AppId 白名单控制注入范围——白名单为空时全量注入所有小游戏,白名单非空时仅注入命中 AppId 的小游戏。
示例:
@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"
));

@Override
public 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;
}
}
}


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈