Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my chrome pageAction not fire onClicked when clicked?

I have a trivial chrome extension which is mean to present a pageAction when on a specific domain.

Manifest.json:

{
  "name" : "Page action by content",
  "version" : "1.1",
  "description" : "Shows a page action for HTML pages containing a video",
  "background" : {
    "scripts": ["background.js"],
    "persistent": false
  },
  "page_action" :
  {
    "default_icon" : "video-19.png",
    "default_title" : "There's a <video> in this page!"
  },
  "permissions": [ "declarativeContent" ],
  "icons" : {
    "48" : "video-48.png",
    "128" : "video-128.png"
  },
  "manifest_version": 2
}

background.js:

chrome.runtime.onInstalled.addListener(function () {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
      chrome.declarativeContent.onPageChanged.addRules([
          {
              conditions: [
                  new chrome.declarativeContent.PageStateMatcher({
                      pageUrl: { hostSuffix: 'stackoverflow.com' },
                  })
              ],
              // And shows the extension's page action.
              actions: [new chrome.declarativeContent.ShowPageAction()]
          }
      ]);
  });
});

chrome.pageAction.onClicked.addListener(function (tab) {
  alert('hello!');
});

On most computers, this works fine. On a small minority, instead of seeing the expected alert(), the user sees the right-click menu for the extension (that would normally be displayed if the action was disabled or right clicked). The icon lights up as if it were enabled, but the onClicked listener is not executed.

Even more wierdly, when the extension is initially installed it works as expected even on the minority computers. Only after a restart of Chrome does the errant behavior present. This occurs whether the extension is loaded from the Chrome Store or whether it is loaded unpacked.

It can also be reproduced with other extensions (page-action-demo in particular with demo site baidu.com). browserActions are unaffected.

Is there an error lurking in the deceptively simple manifest.json or do I need to be submitting a bug?

like image 378
Mitch Avatar asked Jan 24 '19 04:01

Mitch


1 Answers

As chrome.pageAction.onClicked.addListener saying

This event will not fire if the page action has a popup.

That's mean your manifest.json should contain something like that(you have it)

"page_action": {
     "default_title": "Google Mail",      // optional;
     "default_icon": "images/icon32.png"  // optional;
} 

"default_popup": "popup.html" <---------------- Should be removed

The next step is to show the "page action" for particular tab. I don't know why but for some reasons it's visible and active and when you clicking on it it will show you context popup menu

chrome.pageAction.show(integer tabId, function callback)

After you do that in your background script ⏫

 `chrome.pageAction.onClicked` 

should fire the event. I hope it's will help you.

like image 79
Oleksii Semeniuk Avatar answered Nov 15 '22 01:11

Oleksii Semeniuk