Skip to content

Use content script to get selection and become a pure WebExtension #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2025

Conversation

jobisoft
Copy link
Contributor

@jobisoft jobisoft commented Apr 7, 2025

With the introduction of the new Thunderbird Release channel, updating Experiment add-ons every 4 weeks will become very time-consuming. We therefore doubled our effort to identify add-ons, which could be converted to pure WebExtensions.

As it turned out, this add-on is one of them: It uses an Experiment to get the currently selected text. This PR replace the Experiment by a content script.

@marph91
Copy link
Owner

marph91 commented Apr 7, 2025

Thanks! This resolves releasing just for the version update (#10, #38).

I will do a few manual tests. If they are successful, I can fix the failing automated tests later, if you don't mind.

Resolves: #20

@jobisoft
Copy link
Contributor Author

jobisoft commented Apr 7, 2025

Yes, if you could fix the tests, that would be great.

The scripting.messageDisplay API was introduced in ESR 128
@marph91
Copy link
Owner

marph91 commented Apr 7, 2025

Somehow not all exceptions are catched. When the message area is hidden (F8) and the export button is pressed, the following exceptions are thrown:

Traceback
(intermediate value).getAttribute is not a function ExtensionParent.sys.mjs:331:38
    getTopBrowsingContextId resource://gre/modules/ExtensionParent.sys.mjs:331
    normalizeArgs resource://gre/modules/ExtensionParent.sys.mjs:350
    InterpretGeneratorResume self-hosted:1417
    AsyncFunctionNext self-hosted:804
    (Async: async)
    recvRuntimeMessage resource://gre/modules/ExtensionParent.sys.mjs:356
    _recv resource://gre/modules/ConduitsChild.sys.mjs:90
    receiveMessage resource://gre/modules/ConduitsParent.sys.mjs:470
    (Async: JSActor query)
    _doSend resource://gre/modules/ConduitsChild.sys.mjs:62
    _send resource://gre/modules/ConduitsChild.sys.mjs:125
    sendRuntimeMessage resource://gre/modules/ExtensionChild.sys.mjs:344
    sendMessage chrome://messenger/content/child/ext-tabs.js:18
    callAsyncFunction resource://gre/modules/ExtensionCommon.sys.mjs:1196
    callAsyncFunction resource://gre/modules/ExtensionChild.sys.mjs:726
    callAndLog resource://gre/modules/ExtensionChild.sys.mjs:706
    callAsyncFunction resource://gre/modules/ExtensionChild.sys.mjs:725
    stub resource://gre/modules/Schemas.sys.mjs:2954
    getSelection moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8696
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    getSelection moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8693
    processMail moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8859
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    processMail moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8806
    results moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8736
    map self-hosted:178
    getAndProcessMessages moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8736
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    getAndProcessMessages moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8721
    handleMenuButton moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8622
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    handleMenuButton moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8620
    apply self-hosted:2289
    applySafeWithoutClone resource://gre/modules/ExtensionCommon.sys.mjs:675
    fire resource://gre/modules/ExtensionChild.sys.mjs:825
    withHandlingUserInput resource://gre/modules/ExtensionCommon.sys.mjs:110
    recvRunListener resource://gre/modules/ExtensionChild.sys.mjs:828
    _recv resource://gre/modules/ConduitsChild.sys.mjs:90

bc is undefined ext-tabs-base.js:715:17
    visit chrome://extensions/content/parent/ext-tabs-base.js:715
    queryContent chrome://extensions/content/parent/ext-tabs-base.js:727
    execute chrome://extensions/content/parent/ext-scripting.js:120
    executeScriptInternal chrome://extensions/content/parent/ext-scripting.js:195
    result resource://gre/modules/ExtensionParent.sys.mjs:1221
    withCallContextData resource://gre/modules/ExtensionParent.sys.mjs:664
    result resource://gre/modules/ExtensionParent.sys.mjs:1220
    withPendingBrowser resource://gre/modules/ExtensionParent.sys.mjs:674
    result resource://gre/modules/ExtensionParent.sys.mjs:1219
    callAndLog resource://gre/modules/ExtensionParent.sys.mjs:1170
    recvAPICall resource://gre/modules/ExtensionParent.sys.mjs:1218
    InterpretGeneratorResume self-hosted:1417
    AsyncFunctionNext self-hosted:804
    (Async: async)
    _recv resource://gre/modules/ConduitsChild.sys.mjs:90
    receiveMessage resource://gre/modules/ConduitsParent.sys.mjs:470
    (Async: JSActor query)
    _doSend resource://gre/modules/ConduitsChild.sys.mjs:64
    _send resource://gre/modules/ConduitsChild.sys.mjs:125
    callParentAsyncFunction resource://gre/modules/ExtensionChild.sys.mjs:900
    executeScript chrome://extensions/content/child/ext-scripting.js:41
    callAsyncFunction resource://gre/modules/ExtensionCommon.sys.mjs:1196
    callAsyncFunction resource://gre/modules/ExtensionChild.sys.mjs:726
    callAndLog resource://gre/modules/ExtensionChild.sys.mjs:706
    callAsyncFunction resource://gre/modules/ExtensionChild.sys.mjs:725
    stub resource://gre/modules/Schemas.sys.mjs:2954
    getSelection moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8707
    throw self-hosted:1364
    rejected moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8601
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    getSelection moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8693
    processMail moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8859
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    processMail moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8806
    results moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8736
    map self-hosted:178
    getAndProcessMessages moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8736
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    fulfilled moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8600
    (Async: promise callback)
    step moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8602
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    getAndProcessMessages moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8721
    handleMenuButton moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8622
    InterpretGeneratorResume self-hosted:1417
    next self-hosted:1338
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8603
    __awaiter moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8599
    handleMenuButton moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8620
    apply self-hosted:2289
    applySafeWithoutClone resource://gre/modules/ExtensionCommon.sys.mjs:675
    fire resource://gre/modules/ExtensionChild.sys.mjs:825
    withHandlingUserInput resource://gre/modules/ExtensionCommon.sys.mjs:110
    recvRunListener resource://gre/modules/ExtensionChild.sys.mjs:828
    _recv resource://gre/modules/ConduitsChild.sys.mjs:90

Error: An unexpected error occurred undefined

getSelection moz-extension://3a3185f0-6128-4872-9aa3-cfef7a8e5232/dist/background.js:8696 refers to

return await browser.tabs.sendMessage(tab.id, {

Do you have an idea why the exception doesn't get catched?

@jobisoft
Copy link
Contributor Author

jobisoft commented Apr 7, 2025

Interesting. You are triggering the message action from a browser action button, which could therefore act on a tab, which does not have a message. Does a check if we actually display a message fix this for you?

A consequence of this is, that you have no button in a message displayed in a window. You either have to specifically request that via the manifest entry default_windows for the browser_action entry, or use a message_display_action.
https://webextension-api.thunderbird.net/en/128-esr-mv2/browserAction.html#manifest-file-properties

@jobisoft jobisoft changed the title Use content script do get selection and become a pure WebExtension Use content script to get selection and become a pure WebExtension Apr 7, 2025
@marph91
Copy link
Owner

marph91 commented Apr 7, 2025

Does a check if we actually display a message fix this this for you?

Yes, that works.

A consequence of this is, that you have no button in a message displayed in a window. You either have to specifically request that via the manifest entry default_windows for the browser_action entry, or use a message_display_action.
https://webextension-api.thunderbird.net/en/128-esr-mv2/browserAction.html#manifest-file-properties

That would be a good addition. I will check that on a separate branch.

Thanks for your efforts!

@marph91 marph91 merged commit 9db9bd8 into marph91:master Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants