简体   繁体   English

Chrome 扩展弹出操作不起作用

[英]Chrome Extension Popup Action not Working

I am working on an extension for Google Chrome.我正在开发 Google Chrome 的扩展程序。 This allows our users to submit support tickets from our online product directly from the page they are on.这允许我们的用户直接从他们所在的页面提交来自我们在线产品的支持票。

I have been using page rules to limit the extension to only our products pages.我一直在使用页面规则来限制扩展到我们的产品页面。 Some of our users are experiencing an issue were they popup no longer works.我们的一些用户遇到了一个问题,因为他们的弹出窗口不再有效。

This is what some of our users are seeing when they are one our products pages.这是我们的一些用户在进入我们的产品页面时所看到的。 Also the extensions icon is in full colour when the user sees this.当用户看到这个时,扩展图标也是全彩色的。 Not sure if that is useful information.不确定这是否是有用的信息。

在此处输入图片说明

I am having trouble recreating the issue.我无法重现该问题。 Most of our users are remote making is difficult to diagnose.我们的大部分用户都是远程制作的,很难诊断。

To recreate the issue I have tried force quitting chrome and reopening.为了重现这个问题,我尝试强制退出 chrome 并重新打开。 Also a restart of my computer didn't recreate it.我的电脑重新启动也没有重新创建它。

It does seem that removing the extension and reinstalling it fixes the issue.删除扩展程序并重新安装它似乎确实可以解决问题。

This is the code that I am using to enable/disable the extension.这是我用来启用/禁用扩展的代码。

const PAGE_RULE = [{
    id: 'DISPLAY_RULE_SS',
    conditions: [
      new chrome.declarativeContent.PageStateMatcher({
        pageUrl: {hostSuffix: '.localhost.com', pathPrefix: '/app/'},
      }),
    ],
    actions: [new chrome.declarativeContent.ShowPageAction()],
  },]

chrome.runtime.onInstalled.addListener(function() {

    chrome.tabs.onActivated.addListener((activeInfo) => {

        chrome.declarativeContent.onPageChanged.getRules(['DISPLAY_RULE_SS'], (rules) => {

            if(rules.length !== 0){
                return;
            }

            chrome.declarativeContent.onPageChanged.addRules(PAGE_RULE);    
        });     
    });
});

This is my manifest.这是我的清单。

{
    "name"              : "Support App",
    "version"           : "0.0.3",

    "description"       : "Fill in a brief description of your issue, along with a few details, and our support team will be notified immediately.",
    "manifest_version"  : 2,

    "icons": {
        "128" : "./img/murmuration_square_transparent.png"
    },

    "background" : {
        "scripts"    : ["./backgrounds/default_background.js"],
        "persistent" : false
    },

    "permissions" : [
        "history",
        "tabs",
        "declarativeContent",
        "activeTab",
        "https://*.localhost.com/*",
    ],

    "page_action" : {
        "default_icon"  : "./img/murmuration_square_transparent.png",
        "default_popup" : "../default_interface.html"
    },

    "web_accessible_resources": [
        "src/default_index.js"
    ]
}

I suspect that the issue is related to the fact that all the logic is within chrome.runtime.onInstalled.addListener(function() {...} but I'm unsure.我怀疑这个问题与所有逻辑都在chrome.runtime.onInstalled.addListener(function() {...}的事实有关,但我不确定。

Any help is appreciated.任何帮助表示赞赏。

I have returned to this after some downtime and found the solution in the Google documentation.经过一段时间的停机后,我又回到了这个问题,并在 Google 文档中找到了解决方案。 The documentation on this page state此页面上的文档状态

Listeners must be registered synchronously from the start of the page.监听器必须从页面开始同步注册。

which is exactly what I was doing when setting the page rules.这正是我在设置页面规则时所做的。

I also found on this page that我也在这个页面上发现

Rules are persistent across browsing sessions.规则在浏览会话中是持久的。 Therefore, you should install rules during extension installation time using the runtime.onInstalled event.因此,您应该在扩展安装期间使用 runtime.onInstalled 事件安装规则。 Note that this event is also triggered when an extension is updated.请注意,更新扩展时也会触发此事件。 Therefore, you should first clear previously installed rules and then register new rules.因此,您应该先清除以前安装的规则,然后再注册新规则。

So I have updated my background script to run the following所以我更新了我的后台脚本来运行以下

chrome.runtime.onInstalled.addListener(details => {
   chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
       chrome.declarativeContent.onPageChanged.addRules(PAGE_RULE);
   });
});

Which is what is recommended in the documentation.这是文档中推荐的内容。 This seems to have solved the issue we were encountering.这似乎解决了我们遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM