7

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?

Mitch
  • 21,223
  • 6
  • 63
  • 86
  • 1
    As far as I know, `pageAction.onClicked()` fired when a page action icon is clicked. This event will not fire if the page action has a **popup**. You can check the solution from [SO post](https://stackoverflow.com/questions/17737263/how-to-tell-when-a-page-action-is-clicked-when-the-page-action-has-a-popup) for popup issue. – Jessica Rodriguez Jan 24 '19 at 13:39
  • @jess, Agreed, but in this case the event does not fire when the pageAction's icon is clicked. The manifest and background script of the tested code are listed exactly as tested - no popups here. – Mitch Jan 24 '19 at 16:24
  • @Mitch did you find a solution to this? I have noticed the same issue. – Rob Hawkins Oct 28 '19 at 04:50
  • @RobHawkins, I have not. It is still reproducible in latest chrome (October 2019) – Mitch Oct 28 '19 at 11:14
  • Okay, no sweat. I changed to use a browser action instead. For me, the issue presented itself when I switched from using the tabs permission to activeTab – Rob Hawkins Oct 29 '19 at 15:09
  • I think I have the same issue on macOS/chrome. – themefield Dec 23 '19 at 00:16

1 Answers1

2

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.

  • Is this meant to be an answer to this question or just a review of the requirements of a page action? I don’t see anything I’ve done wrong relative to your answer. – Mitch Nov 18 '19 at 12:23
  • __The icon lights up as if it were enabled, but the onClicked listener is not executed.__ as you said previously the listener is not executed. You ask why? I told you because you didn't use `chrome.pageAction.show(integer tabId, function callback)` all other was just in case someone else will have something similar – Oleksii Semeniuk Nov 18 '19 at 19:29
  • I still don't understand. The extension is activated and inactivated as expected, it just does not call `onClicked`. There is no popups on the extension as listed. Are you suggesting that `chrome.declarativeContent.ShowPageAction` is not appropriate? – Mitch Nov 19 '19 at 19:50
  • no. i got a same issue you have... you should use `chrome.pageAction.show(integer tabId, function callback)` then onClick will work... maybe it's some kind of bug who know, but i know exactly that in this case it will work – Oleksii Semeniuk Nov 19 '19 at 23:17
  • Interesting, thanks. What `declarativeContent` rules are you using? How are you enabling the page action only for the correct sites? – Mitch Nov 19 '19 at 23:36
  • 6
    Something like that `chrome.tabs.onActivated.addListener(function (tabs) { chrome.pageAction.show(tabs.tabId); });` – Oleksii Semeniuk Nov 20 '19 at 00:18
  • 1
    Consider to add the actual solution to the answer itself?! :D – Martin Schneider Sep 17 '20 at 13:44