FBroSharpTransitionType 页面转换类型枚举
📋 概述
FBroSharpTransitionType枚举定义了浏览器页面导航的转换类型,用于标识用户如何到达当前页面。这个枚举在浏览器事件处理中非常重要,特别是在监控页面导航、实现浏览器同步控制等场景中。
🎯 适用场景
- 页面导航监控:检测用户的浏览行为模式
- 浏览器同步:实现多浏览器窗口的导航同步
- 用户行为分析:统计不同类型的页面访问方式
- 安全控制:拦截特定类型的导航操作
- 历史记录管理:区分不同的页面访问来源
📊 枚举值分类
🚀 基础导航类型 (0-10)
| 枚举值 | 数值 | 中文说明 | 使用场景 |
|---|---|---|---|
TT_LINK | 0 | 链接点击导航 | 用户点击链接或JavaScript window.open |
TT_EXPLICIT | 1 | 显式导航 | 地址栏输入URL、书签点击等 |
TT_AUTO_BOOKMARK | 2 | 自动书签导航 | UI建议的目标页面(仅Chrome) |
TT_AUTO_SUBFRAME | 3 | 自动子框架导航 | 广告等自动加载的子框架内容 |
TT_MANUAL_SUBFRAME | 4 | 手动子框架导航 | 用户主动请求的子框架导航 |
TT_GENERATED | 5 | 生成的导航 | 搜索建议等生成的URL(仅Chrome) |
TT_AUTO_TOPLEVEL | 6 | 自动顶级导航 | 屏保、开发工具等自动加载 |
TT_FORM_SUBMIT | 7 | 表单提交导航 | 用户提交表单 |
TT_RELOAD | 8 | 页面刷新 | F5刷新或重新访问相同URL |
TT_KEYWORD | 9 | 关键词导航 | 地址栏关键词搜索(仅Chrome) |
TT_KEYWORD_GENERATED | 10 | 关键词生成导航 | 关键词搜索生成的访问 |
🏷️ 限定符标志 (Flag位)
| 枚举值 | 数值 | 中文说明 | 重要程度 |
|---|---|---|---|
TT_BLOCKED_FLAG | 8388608 | 访问被阻止 | ⚠️ 安全相关 |
TT_FORWARD_BACK_FLAG | 16777216 | 前进/后退操作 | 🎯 导航同步关键 |
TT_DIRECT_LOAD_FLAG | 33554432 | 直接加载 | 📱 程序控制 |
TT_HOME_PAGE_FLAG | 67108864 | 主页导航 | 🏠 用户习惯 |
TT_FROM_API_FLAG | 134217728 | API调用导航 | 🔧 扩展系统 |
TT_CHAIN_START_FLAG | 268435456 | 导航链开始 | 🔗 重定向链 |
TT_CHAIN_END_FLAG | 536870912 | 导航链结束 | 🔗 重定向链 |
TT_CLIENT_REDIRECT_FLAG | 1073741824 | 客户端重定向 | 🔄 JavaScript/Meta |
TT_SERVER_REDIRECT_FLAG | 2147483648 | 服务器重定向 | 🔄 HTTP Header |
🎭 掩码定义
| 掩码名称 | 数值 | 用途 |
|---|---|---|
TT_SOURCE_MASK | 255 | 提取基础导航类型 |
TT_IS_REDIRECT_MASK | 3221225472 | 检测是否为重定向 |
TT_QUALIFIER_MASK | 4294967040 | 提取限定符标志 |
💡 使用示例
1. 检测前进/后退操作
csharp
public override void OnLoadStart(IFBroSharpBrowser browser, IFBroSharpFrame frame, FBroSharpTransitionType transitionType)
{
// 检查是否为前进/后退操作
if ((transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0)
{
Console.WriteLine("检测到前进/后退操作");
// 获取基础导航类型
var baseType = transitionType & FBroSharpTransitionType.TT_SOURCE_MASK;
Console.WriteLine($"基础类型: {baseType}");
// 在主从同步系统中触发同步操作
if (IsMainBrowser(browser))
{
SyncNavigationToFollowers("FORWARD_BACK");
}
}
}2. 分析导航类型
csharp
public void AnalyzeNavigationType(FBroSharpTransitionType transitionType)
{
// 获取基础类型
var sourceType = transitionType & FBroSharpTransitionType.TT_SOURCE_MASK;
// 检查各种标志
bool isForwardBack = (transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0;
bool isReload = sourceType == FBroSharpTransitionType.TT_RELOAD;
bool isFormSubmit = sourceType == FBroSharpTransitionType.TT_FORM_SUBMIT;
bool isBlocked = (transitionType & FBroSharpTransitionType.TT_BLOCKED_FLAG) != 0;
bool isRedirect = (transitionType & FBroSharpTransitionType.TT_IS_REDIRECT_MASK) != 0;
Console.WriteLine($"导航分析:");
Console.WriteLine($" 基础类型: {sourceType}");
Console.WriteLine($" 前进/后退: {isForwardBack}");
Console.WriteLine($" 页面刷新: {isReload}");
Console.WriteLine($" 表单提交: {isFormSubmit}");
Console.WriteLine($" 访问被阻止: {isBlocked}");
Console.WriteLine($" 重定向: {isRedirect}");
}3. 主从浏览器同步应用
csharp
public class NavigationSyncManager
{
public void HandleNavigationEvent(IFBroSharpBrowser browser, FBroSharpTransitionType transitionType)
{
// 只处理主控浏览器的前进/后退操作
if (!IsMainBrowser(browser)) return;
if ((transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0)
{
var baseType = transitionType & FBroSharpTransitionType.TT_SOURCE_MASK;
// 同步到所有从属浏览器
foreach (var followerBrowser in GetFollowerBrowsers())
{
try
{
// 根据导航历史执行相应操作
if (CanSyncNavigation(followerBrowser, baseType))
{
SyncBrowserNavigation(followerBrowser, transitionType);
}
}
catch (Exception ex)
{
Console.WriteLine($"同步导航失败: {ex.Message}");
}
}
}
}
private void SyncBrowserNavigation(IFBroSharpBrowser follower, FBroSharpTransitionType transitionType)
{
// 这里需要确定是前进还是后退
// 可能需要结合其他信息(如URL变化)来判断具体方向
if (follower.CanGoBack() || follower.CanGoForward())
{
// 执行相应的导航操作
// 具体实现需要根据实际需求调整
}
}
}4. 导航类型过滤器
csharp
public class NavigationTypeFilter
{
// 检查是否为用户主动导航
public static bool IsUserInitiated(FBroSharpTransitionType transitionType)
{
var sourceType = transitionType & FBroSharpTransitionType.TT_SOURCE_MASK;
return sourceType == FBroSharpTransitionType.TT_LINK ||
sourceType == FBroSharpTransitionType.TT_EXPLICIT ||
sourceType == FBroSharpTransitionType.TT_FORM_SUBMIT ||
(transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0;
}
// 检查是否为自动导航
public static bool IsAutomatic(FBroSharpTransitionType transitionType)
{
var sourceType = transitionType & FBroSharpTransitionType.TT_SOURCE_MASK;
return sourceType == FBroSharpTransitionType.TT_AUTO_SUBFRAME ||
sourceType == FBroSharpTransitionType.TT_AUTO_TOPLEVEL ||
(transitionType & FBroSharpTransitionType.TT_IS_REDIRECT_MASK) != 0;
}
// 检查是否需要同步
public static bool ShouldSync(FBroSharpTransitionType transitionType)
{
// 排除自动导航和被阻止的导航
return IsUserInitiated(transitionType) &&
(transitionType & FBroSharpTransitionType.TT_BLOCKED_FLAG) == 0;
}
}🎯 主从同步应用重点
关键标志位:TT_FORWARD_BACK_FLAG
csharp
// 在主从同步中的关键检测
if ((transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0)
{
// 这是前进/后退操作,需要同步到所有从属浏览器
Console.WriteLine("检测到历史导航操作,开始同步...");
}避免循环触发
csharp
// 设置标志避免同步操作触发新的导航事件
private bool _isSyncing = false;
public void OnNavigationEvent(IFBroSharpBrowser browser, FBroSharpTransitionType transitionType)
{
if (_isSyncing) return; // 避免循环触发
if (IsMainBrowser(browser) &&
(transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0)
{
_isSyncing = true;
try
{
// 执行同步操作
SyncToFollowers();
}
finally
{
_isSyncing = false;
}
}
}⚠️ 注意事项
- 位运算操作:使用
&操作符进行标志位检查 - 组合标志:一个导航可能同时具有多个标志位
- 平台差异:某些标志仅在Chrome运行时有效
- 性能考虑:频繁的导航事件检查可能影响性能
- 循环避免:同步操作时要防止触发新的导航事件
🔍 调试技巧
csharp
public static string GetTransitionTypeDescription(FBroSharpTransitionType transitionType)
{
var description = new List<string>();
// 基础类型
var sourceType = transitionType & FBroSharpTransitionType.TT_SOURCE_MASK;
description.Add($"源类型: {sourceType}");
// 检查所有标志位
if ((transitionType & FBroSharpTransitionType.TT_FORWARD_BACK_FLAG) != 0)
description.Add("前进/后退");
if ((transitionType & FBroSharpTransitionType.TT_RELOAD) != 0)
description.Add("刷新");
if ((transitionType & FBroSharpTransitionType.TT_BLOCKED_FLAG) != 0)
description.Add("被阻止");
if ((transitionType & FBroSharpTransitionType.TT_IS_REDIRECT_MASK) != 0)
description.Add("重定向");
return string.Join(", ", description);
}通过这个优化的文档,开发者可以更好地理解和使用FBroSharpTransitionType枚举,特别是在实现浏览器导航同步功能时。
namespace FBroSharp.Const;
public enum FBroSharpTransitionType : long
{
//
// 摘要:
// Source is a link click or the JavaScript window.open function. This is also the
// default value for requests like sub-resource loads that are not navigations.
TT_LINK = 0L,
//
// 摘要:
// Source is some other "explicit" navigation. This is the default value for navigations
// where the actual type is unknown. See also TT_DIRECT_LOAD_FLAG.
TT_EXPLICIT = 1L,
//
// 摘要:
// User got to this page through a suggestion in the UI (for example, via the destinations
// page). Chrome runtime only.
TT_AUTO_BOOKMARK = 2L,
//
// 摘要:
// Source is a subframe navigation. This is any content that is automatically loaded
// in a non-toplevel frame. For example, if a page consists of several frames containing
// ads, those ad URLs will have this transition type. The user may not even realize
// the content in these pages is a separate frame, so may not care about the URL.
TT_AUTO_SUBFRAME = 3L,
//
// 摘要:
// Source is a subframe navigation explicitly requested by the user that will generate
// new navigation entries in the back/forward list. These are probably more important
// than frames that were automatically loaded in the background because the user
// probably cares about the fact that this link was loaded.
TT_MANUAL_SUBFRAME = 4L,
//
// 摘要:
// User got to this page by typing in the URL bar and selecting an entry that did
// not look like a URL. For example, a match might have the URL of a Google search
// result page, but appear like "Search Google for ...". These are not quite the
// same as EXPLICIT navigations because the user didn't type or see the destination
// URL. Chrome runtime only. See also TT_KEYWORD.
TT_GENERATED = 5L,
//
// 摘要:
// This is a toplevel navigation. This is any content that is automatically loaded
// in a toplevel frame. For example, opening a tab to show the ASH screen saver,
// opening the devtools window, opening the NTP after the safe browsing warning,
// opening web-based dialog boxes are examples of AUTO_TOPLEVEL navigations. Chrome
// runtime only.
TT_AUTO_TOPLEVEL = 6L,
//
// 摘要:
// Source is a form submission by the user. NOTE: In some situations submitting
// a form does not result in this transition type. This can happen if the form uses
// a script to submit the contents.
TT_FORM_SUBMIT = 7L,
//
// 摘要:
// Source is a "reload" of the page via the Reload function or by re-visiting the
// same URL. NOTE: This is distinct from the concept of whether a particular load
// uses "reload semantics" (i.e. bypasses cached data).
TT_RELOAD = 8L,
//
// 摘要:
// The url was generated from a replaceable keyword other than the default search
// provider. If the user types a keyword (which also applies to tab-to-search) in
// the omnibox this qualifier is applied to the transition type of the generated
// url. TemplateURLModel then may generate an additional visit with a transition
// type of TT_KEYWORD_GENERATED against the url 'http://' + keyword. For example,
// if you do a tab-to-search against wikipedia the generated url has a transition
// qualifer of TT_KEYWORD, and TemplateURLModel generates a visit for 'wikipedia.org'
// with a transition type of TT_KEYWORD_GENERATED. Chrome runtime only.
TT_KEYWORD = 9L,
//
// 摘要:
// Corresponds to a visit generated for a keyword. See description of TT_KEYWORD
// for more details. Chrome runtime only.
TT_KEYWORD_GENERATED = 10L,
//
// 摘要:
// General mask defining the bits used for the source values.
TT_SOURCE_MASK = 255L,
//
// 摘要:
// Qualifiers. Any of the core values above can be augmented by one or more qualifiers.
// These qualifiers further define the transition. Attempted to visit a URL but
// was blocked.
TT_BLOCKED_FLAG = 8388608L,
//
// 摘要:
// Used the Forward or Back function to navigate among browsing history. Will be
// ORed to the transition type for the original load.
TT_FORWARD_BACK_FLAG = 16777216L,
//
// 摘要:
// Loaded a URL directly via CreateBrowser, LoadURL or LoadRequest.
TT_DIRECT_LOAD_FLAG = 33554432L,
//
// 摘要:
// User is navigating to the home page. Chrome runtime only.
TT_HOME_PAGE_FLAG = 67108864L,
//
// 摘要:
// The transition originated from an external application; the exact definition
// of this is embedder dependent. Chrome runtime and extension system only.
TT_FROM_API_FLAG = 134217728L,
//
// 摘要:
// The beginning of a navigation chain.
TT_CHAIN_START_FLAG = 268435456L,
//
// 摘要:
// The last transition in a redirect chain.
TT_CHAIN_END_FLAG = 536870912L,
//
// 摘要:
// Redirects caused by JavaScript or a meta refresh tag on the page.
TT_CLIENT_REDIRECT_FLAG = 1073741824L,
//
// 摘要:
// Redirects sent from the server by HTTP headers.
TT_SERVER_REDIRECT_FLAG = 2147483648L,
//
// 摘要:
// Used to test whether a transition involves a redirect.
TT_IS_REDIRECT_MASK = 3221225472L,
//
// 摘要:
// General mask defining the bits used for the qualifiers.
TT_QUALIFIER_MASK = 4294967040L
}