0

Just to make it clear I read multiple similar questions
1 2 3 4 5 etc
none of them are relevant to mine.

As I understood, WebAPI content scripts even if run in isolated environment still can manipulate page's DOM and add EventListeners, at least this is what Developer.Chrome and MDN says. However, I'm having a situation where it does not seems to be true.

Here is a sample extension:

manifest.json

{
    "manifest_version": 2,
    "name": "Foo",
    "version": "0",
    "permissions":
    [
        "storage",
        "https://steamcommunity.com/*"
    ],
    "content_scripts":
    [ {

        "matches": ["*://steamcommunity.com/groups/*"],
        "js": ["content.js"],
        "run_at":    "document_idle",
        "all_frames": true
    } ]
}

content.js

'use strict';

const butArea = document.querySelector(".grouppage_join_area");

function queueGroup()
{ 
    alert('yay!');
}

if (butArea)
{
    butArea.addEventListener("click", queueGroup, false);
}

Load unpacked and go to https://steamcommunity.com/groups/SteamClientBeta. Click he button and no alert. Add the eventListener manually through console and it just werks. I tried click/onclick properties too but same result.

Yes, I can inject my code into the page, no an issue here, but I dont really want to because it should work as it is, no? Otherwise - why not, what I'm missing?

Kirikan
  • 61
  • 4
  • "Your listener is attached after the page own listeners" /// it is not at all, getEvenListeners() returns empty array – Kirikan May 23 '19 at 12:17
  • *You just need to use getEvenListeners in the correct console context * /// could you elaborate on that? getEvenListeners returns listeners for the document/window no problem, but if, as you say, my custom listener was added - why it wont return it? I understand what do you mean by propogation but I cant even see it attached – Kirikan May 23 '19 at 13:14
  • Workaround isnt a problem. My problem is that you said, I quote - "Oh yes it is. You just need to use getEvenListeners in the correct console context", stating that EventListener is added, however, I'm incapable of proving your words experimentally by any means. Can you? – Kirikan May 23 '19 at 14:29
  • If was a pretty simple question. If you have issues answering simple questions then why bother answering at all in a first place? – Kirikan May 23 '19 at 16:19
  • Actually, my guess was irrelevant because your code works as is, nothing needs to be changed. I guess you've changed the content script or manifest.json but didn't click the reload button for your extension on chrome://extensions page or didn't reload the matching web page(s). – wOxxOm May 23 '19 at 17:16
  • No, it does not. As I mentioned - the Listener is not being added despite the element being found successfully and code being executed successfully. This was my question exactly - I literally have no idea what is happening and I can reproduce it with 100%. – Kirikan May 23 '19 at 18:57
  • Since it really really does work here the only other explanations are it's a bug in Chrome or another extension is interfering. Try in an older/newer portable version of Chrome or in a new browser profile. – wOxxOm May 23 '19 at 19:22
  • Oh, in case you navigate from another page on the same site it could be also due to the site being a SPA where navigation is performed by the page script's use of the History API or just changing the URL hash, in which case your content script won't re-run automatically, see [this answer](https://stackoverflow.com/a/39508954) for more details. – wOxxOm May 23 '19 at 19:33
  • I'd also like to reiterate the need to reload the matching web pages after you reload or install the extension because Chrome, unlike Firefox, won't run your content scripts on the existing tabs, see [Chrome extension content script re-injection after upgrade or install](//stackoverflow.com/a/11598753). – wOxxOm May 23 '19 at 19:36
  • Yes, I re-load both extension and page completely, skipping the cache. I know how to work with browser, not the first time. But what happens here baffles me. I place the breakpoint at the start of a function that assign listener, I see the object found and cached, it goes past the .addListener and... nothing. – Kirikan May 23 '19 at 20:42
  • Sounds like a bug in Chrome or in devtools, try in an older/newer portable version of Chrome or in a new browser profile. – wOxxOm May 24 '19 at 04:35

0 Answers0