【问题标题】:Edge Extension Native Communication and UWP边缘扩展本机通信和 UWP
【发布时间】:2017-11-20 23:54:23
【问题描述】:

我正在通过边缘扩展在我的 UWP 应用中提供一项新功能。 我用以下 manifest.json 编写了一个边缘扩展

{
"name": "MyExtensionName",
"version": "1.0.0.0",
"description": "MyExtension description",
"author": "Me",
"icons": {
    "16": "icons/logo_16.png",
    "48": "icons/logo_48.png",
    "128": "icons/logo_128.png"
},
"browser_action": {
    "default_icon": {
        "20": "icons/logo_20.png",
        "40": "icons/logo_40.png"
    },
    "default_title": "MyTitle"
},
"permissions": [
    "contextMenus",
    "tabs",
    "storage",
    "activeTab",
    "<all_urls>",
    "nativeMessaging",
    "background"
],
"minimum_edge_version": "37.14316.1000.0",
"background": {
    "scripts": ["js/background.js"],
    "persistent": true
},
"content_scripts": [{
    "matches": ["http://*/*", "https://*/*"],
    "css": ["css/light.css"],
    "js": ["js/content.js"],
    "run_at": "document_end"
}]
}

我可以用这个建立连接

var port = browser.runtime.connectNative("MyAppServiceName");

我已尝试与此进行交流

port.postMessage({Text: "Hello"});

我没有收到任何错误,但我的 UWP 应用中的后台任务没有收到任何响应。我不明白为什么。

在我的应用程序的 package.appmanifest 中,我添加了以下代码:

<uap:VisualElements DisplayName="MyUWPAppName" 
                          AppListEntry="none"
                          Square150x150Logo="Assets\Square150x150Logo.png" 
                          Square44x44Logo="Assets\Square44x44Logo.png" 
                          Description="MyUWPApp description" 
                          BackgroundColor="transparent">

在这

<uap:Extension Category="windows.protocol">
          <uap:Protocol Name="msghost1" />
        </uap:Extension>
        <uap:Extension Category="windows.appService" EntryPoint="MyAppService.Class1">
          <uap:AppService Name="MyAppServiceName" />
        </uap:Extension>
        <uap3:Extension Category="windows.appExtension">
          <uap3:AppExtension Name="com.test.edge.extension" 
                             Id="EdgeExtension" 
                             PublicFolder="Extension" 
                             DisplayName="ms-resource:DisplayName">
            <uap3:Properties>
              <Capabilities>
                <Capability Name="websiteContent" />
                <Capability Name="websiteInfo" />
                <Capability Name="browserStorage" />
              </Capabilities>
            </uap3:Properties>
          </uap3:AppExtension>
        </uap3:Extension>

AppService 是在同一解决方案中作为项目添加的运行时组件。 它的代码很简单

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;

namespace MyAppService
{
    public sealed class Class1: IBackgroundTask
    {
        private BackgroundTaskDeferral backgroundTaskDeferral;
        private AppServiceConnection appServiceconnection;


        public void Run(IBackgroundTaskInstance taskInstance)
        {
            this.backgroundTaskDeferral = taskInstance.GetDeferral(); // Get a deferral so that the service isn't terminated.
            taskInstance.Canceled += OnTaskCanceled; // Associate a cancellation handler with the background task.

            // Retrieve the app service connection and set up a listener for incoming app service requests.
            var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            appServiceconnection = details.AppServiceConnection;
            appServiceconnection.RequestReceived += OnRequestReceived;
        }

        private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            // This function is called when the app service receives a request
        }

        private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            if (this.backgroundTaskDeferral != null)
            {
                // Complete the service deferral.
                this.backgroundTaskDeferral.Complete();
            }
        }
    }
}

我尝试使用“不启动,但在启动时调试我的代码”选项来调试 AppService,但我注意到我没有得到任何响应(后台任务未被调用)。

有人知道哪里出了问题(或缺少什么)?

【问题讨论】:

标签: c# uwp microsoft-edge


【解决方案1】:

根据评论,您甚至无法成功运行官方示例,但实际上官方示例SecureInput 确实有效。所以恐怕你没有以正确的方式测试项目。请检查以下提示是否有帮助,示例中不推荐这些提示:

  • 您的设备装有 Windows 10 Creators Update 或更高版本,因为 Creators Update 支持 Edge 中的本机消息传递。

  • 确保已启用扩展开发人员功能。在您的边缘输入about:flags,然后选择“启用扩展开发人员功能”。详情请参考this article。

  • 托管测试网页。通常我们需要一个测试网页来测试这个功能,你可以将页面发布到 IIS 并像http://localhost/secureinput.html 一样访问它。详细如何在IIS上搭建静态网站请参考this article。

  • 首先调试扩展脚本,看看脚本是否有错误。调试方法请参考Debugging extensions。

在您的应用服务中,您没有处理收到的消息,也没有发回消息(OnRequestReceived)方法是无。您可以参考示例尝试更新。

所以部署后,检查扩展是否安装正确,你可以尝试在 Edge 中访问扩展的后台页面。然后使用测试页面确保您可以调试 content.js 。在这种情况下,您可能会发现问题所在。

【讨论】:

  • 感谢您的指点。现在我可以编译和部署官方示例了。 SecureInput 工作正常。我试图在我的项目中重现代码和配置,但我的项目不起作用。我注意到的第一件事是部署后没有安装我的边缘扩展。我认为我的项目中有一些配置是错误的。我找不到那个!
【解决方案2】:

终于找到了解决问题的方法:

  • 在标签中我必须使用 Name="com.microsoft.edge.extension";
  • 我必须导入命名空间 xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities",而无需从中添加一些功能;
  • 必须针对 x64 平台进行构建和部署。

通过这些更改,我能够部署并使其在我的应用程序和 Edge 扩展程序之间运行本机消息传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多