I don't understand the page action / declarative content interaction

240 views
Skip to first unread message

Likely Logic

unread,
Feb 1, 2021, 7:03:27 PM2/1/21
to Chromium Extensions
Not sure if is the right place to post, but I can't seem to make head nor tail of the docs around page actions and the declarative content permission.

I have gotten something to work, but I think it's just copy / paste luck, and I am not that kind of developer.

The sample files on GitHub are a little ambiguous:
The first script also has a content script which runs on page_idle, but is also run from the popup using executeScript which seems kind of ambiguous.

pageAction.onClicked() also doesn't have a tab parameter like browserAction.onClicked() so confused (other than querying tabs for the active, focused tab) how to send data to any content script.

It's a lot of reloading and mucking about, just to get nowhere.

Can anyone help / point me to any good examples?

Thanks.

Jackie Han

unread,
Feb 1, 2021, 10:41:54 PM2/1/21
to Likely Logic, Chromium Extensions
My suggestion is: don't use pageAction now. 
You can use browserAction instead. browserAction and pageAction will be merged into one chrome.action API in manifest v3.
chrome.declarativeContent is also not very useful, because it doesn't support RequestContentScript in Chrome stable.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/c1892f26-8fb2-48f9-bbe0-cbd1cc9a5cdbn%40chromium.org.


--
Jackie Han

wOxxOm

unread,
Feb 2, 2021, 9:36:12 AM2/2/21
to Chromium Extensions, likel...@gmail.com
>One has declarativeContent and URL permissions

URL permissions are required to access the URL (or a tab with this URL loaded) from an extension page/script, specifically executeScript in this case. A much better solution is to use activeTab permission instead. This example was created before that permission was implemented.

>The first script also has a content script which runs on page_idle, but is also run from the popup using executeScript which seems kind of ambiguous.

Nothing too weird: it runs when the page loads (automatically) and also when the user clicks the extension icon (explicitly). A better solution would be to use messaging though so that the script isn't re-evaluated but in this case it's so simple and rarely used that the overhead is acceptable.

>pageAction.onClicked() also doesn't have a tab parameter

It does have it but many programmers don't use it because they don't look in the documentation.

>how to send data to any content script.

Use chrome.tabs.sendMessage as explained in Message Passing documentation.

> any good examples?

You can find examples by googling, also on StackOverflow, but maybe declarativeContent isn't the right tool at all so it's best if you describe what you want to achieve in more detail.

Likely Logic

unread,
Feb 15, 2021, 3:21:24 PM2/15/21
to Chromium Extensions, wOxxOm, Likely Logic
Thanks for the info everyone.

The problem was that I didn't understand that when you use a popup, you don't get an event, and so no tab.

As pointed out, the docs do actually say this, but to be fair a) this is rather counter-intuitive and b) right at the end of a very long page.

I think this should be included in the guide section.

Anyway, I got the current tab by using the activeTab permission and `chrome.tabs.query({ active: true, currentWindow: true }`
Reply all
Reply to author
Forward
0 new messages