You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于VB.NET实现Firefox URL追加?cutout=20classic参数刷新页面的需求

VB.NET实现Firefox一键切换到20块经典拼图

以下是针对需求的完整实现步骤,基于Visual Studio 2022 Community和VB.NET:

1. 创建Windows Forms项目

打开Visual Studio 2022,新建Windows Forms App(.NET Framework)(或.NET 6+版本的Windows Forms App,两者代码兼容)。

2. 添加UI自动化引用

右键项目 → 添加 → 引用 → 切换到COM标签页,找到并勾选:

  • Microsoft UI Automation Client
  • Microsoft UI Automation Types
    点击确定完成引用添加。

3. 设计窗体界面

在窗体设计器中:

  • 拖入一个Button控件
  • 设置控件属性:
    • Text:切换到20块经典拼图
    • Font:选大字号(如20号微软雅黑)
    • Size:设置为大尺寸(如300, 100)
    • 将按钮居中放置在窗体上

4. 编写功能代码

在窗体的代码文件中,替换原有代码为以下内容:

Imports System.Windows.Automation
Imports System.Diagnostics
Imports System.Text.RegularExpressions

Public Class Form1
    Private Sub btnSwitchTo20Classic_Click(sender As Object, e As EventArgs) Handles btnSwitchTo20Classic.Click
        ' 定位Firefox进程
        Dim firefoxProcesses = Process.GetProcessesByName("firefox")
        If firefoxProcesses.Length = 0 Then
            MessageBox.Show("未找到运行中的Firefox浏览器!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' 获取Firefox主窗口的自动化元素
        Dim firefoxWindow = AutomationElement.FromHandle(firefoxProcesses(0).MainWindowHandle)
        If firefoxWindow Is Nothing Then
            MessageBox.Show("无法连接到Firefox窗口!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' 查找Firefox地址栏(AutomationId为urlbar-input,适配多数Firefox版本)
        Dim addressBarCondition = New PropertyCondition(AutomationElement.AutomationIdProperty, "urlbar-input")
        Dim addressBar = firefoxWindow.FindFirst(TreeScope.Descendants, addressBarCondition)
        If addressBar Is Nothing Then
            MessageBox.Show("无法定位Firefox地址栏!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' 获取当前页面URL
        Dim valuePattern = DirectCast(addressBar.GetCurrentPattern(ValuePattern.Pattern), ValuePattern)
        Dim currentUrl = valuePattern.Current.Value
        If String.IsNullOrEmpty(currentUrl) Then
            MessageBox.Show("当前页面URL为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' 处理URL:追加/替换cutout参数
        Dim urlParts = currentUrl.Split("#"c)
        Dim baseUrl = urlParts(0)
        Dim newBaseUrl As String

        If baseUrl.Contains("?") Then
            ' 已有查询参数,替换或追加cutout参数
            newBaseUrl = If(baseUrl.Contains("cutout="),
                           Regex.Replace(baseUrl, "cutout=[^&]*", "cutout=20classic"),
                           baseUrl & "&cutout=20classic")
        Else
            ' 无查询参数,直接追加
            newBaseUrl = baseUrl & "?cutout=20classic"
        End If

        ' 拼接回锚点(如果原URL有锚点)
        Dim newUrl = newBaseUrl & If(urlParts.Length > 1, "#" & urlParts(1), "")

        ' 导航到新URL
        valuePattern.SetValue(newUrl)
        SendKeys.SendWait("{ENTER}")

        MessageBox.Show("已切换到20块经典拼图模式!", "操作完成", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class

5. 注意事项

  • Firefox版本适配:如果后续Firefox更新导致地址栏的AutomationId变化,可以使用Windows SDK中的Inspect.exe工具查看最新的地址栏AutomationId,替换代码中对应的字符串。
  • 平台目标匹配:在项目属性→生成→平台目标中,选择与Firefox一致的架构(通常为x64),避免进程查找失败。
  • 权限要求:程序需要正常的用户权限,无需管理员权限即可运行。

内容的提问来源于stack exchange,提问作者funkymunkey99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 23:40:35