0

I am following the instructions on developing a WebExtension for Firefox. I have it working for HTML pages. But, I would also like my script to run when the requested "page" is an image. Is this possible?

Here is what I have:

manifest.json:

{
    "manifest_version": 2,
    "name": "Test",
    "version": "1.0",
    "applications": {
        "gecko": {
            "id": "alert@example.com",
            "strict_min_version": "42.0",
            "strict_max_version": "50.*",
            "update_url": "https://example.com/updates.json"
        }
    },
    "description": "Just does an alert.",

    "icons": {
        "48": "icons/border-48.png"
    },

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["alert.js"]
        }
    ]

}

alert.js:

alert("ok");

After installing the plugin by following the instructions (by going to "about: debugging"), I get a JavaScript alert for all HTML pages. But if I navigate to an image (for example by right clicking on an image and choosing "View Image"), no alert occurs.

Makyen
  • 31,849
  • 12
  • 86
  • 121
anon
  • 4,578
  • 3
  • 35
  • 54
  • What makes you think it is not possible? What about how it is already working, as you have already written it, implies that it is not? – Makyen Feb 09 '17 at 21:26
  • Please provide examples of the URLs on which you think it is not working. Are they `http`/`https`? – Makyen Feb 09 '17 at 21:28
  • @Makyen - I have added my code. – anon Feb 09 '17 at 21:38
  • I suggest that you do not use `strict_max_version`. It will be obeyed and your add-on *will be* disabled/not permitted to load when it is used on newer versions of Firefox (at lest by FF53 or 54, I don't remember on which I encountered the disabling issue on a couple of test WebExtensions I revisited). – Makyen Feb 09 '17 at 21:57

1 Answers1

2

This is a bug in Firefox that is mostly, but not fully, resolved in Firefox 54 (currently Firefox Beta). Your script (mostly) works as expected (shows the alert when viewing an image from the context menu selection "View Image") in Firefox Beta and Firefox Developer Edition.

The issue I encountered, when testing with Beta and Developer Edition, was that the content script was intermittently not injected when using the forward and back buttons to navigate away from and back to the "View Image" page.

It is possible to be certain that your content script is loaded using tabs.executeScript(), and the events from the webNavigation, webRequest and tabs APIs. However, doing so generically for any content script on any page is...complex (a full generic implementation takes more code than the maximum characters permitted in a Stack Overflow answer).

WebExtensions is actively in development

The WebExtensions API is still in development. What is working improves with each version of Firefox. If you encounter problems, you should test your WebExtension add-on with Firefox Developer Edition, or Firefox Nightly. You should also make careful note of what version of Firefox is required for the functionality you desire to use. This information is contained in the "Browser compatibility" section of the MDN documentation pages.

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Nice! Firefox Beta is mostly working for me, to the point where it's "good enough" for my purposes. As you outlined, I did see some oddball things when using the back buttons, but it's not a huge deal. Thank you! – anon Feb 10 '17 at 17:01