0

I'm working on a Chrome extension. I'm injecting a script into pages, and injecting jquery as well to be usable in my script.

I added another extension named Boomerang, and since then, jquery is not defined. What I tried so far :

myJquery = jQuery.noConflict();

Sometimes works, sometimes doesn't. If I check for myJquery in the console it sometimes exists, sometimes it's undefined - so is $ or JQuery. Race condition ?

jQuery.noConflict();

in a function waiting for all my assets to be injected (with a `setInterval to check until I find them) works the same - sometimes yes sometimes no.

I'm a little out of ideas. It seems Boomerang is the root of the problem, but if it occured here it could occur with other extensions as well. Is there a way I'm unaware of defining my functions into their own namespace or something like that ?

[EDIT] I decompiled Boomerang's code and found out they're using exactly the same stack as me : Gmail.js and Jquery. Here is in their code what I believe to be faulty here :

function h() {
    V = String(document.location);
    "undefined" === typeof window.jQuery && 0 > V.indexOf("ContactManager") ? Eb =
        setTimeout(h, 500) : (a = ia = window.jQuery.noConflict(!0), window.b4g.$ = a, setTimeout(Lc, 0), setTimeout(z, 0))
}

I don't really understand the meaning of this window.jQuery.noConflict(!0) but it doesn't look good...

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88
  • 2
    Edit jquery.js and change its default variable name to something absolutely unique. – wOxxOm Feb 27 '17 at 09:25
  • Hey, and thanks. But I can't do that - I'm using plugins and they call jQuery, not MyLovelyJponey :) – Jeremy Belolo Feb 27 '17 at 09:46
  • 2
    It seems the problem is caused by those plugins that don't account for noConflict'ed jQuery name. You can edit plugins too. – wOxxOm Feb 27 '17 at 09:56
  • I thought about it, but no, it's not that - when trying to `noConflict` it simply logs in the console that `jQuery` or `$` is undefined. – Jeremy Belolo Feb 27 '17 at 10:12
  • 1
    Assuming you're injecting jQuery in a `script` DOM element into the page, try doing it in a content script with `"run_at": "document_start"` – wOxxOm Feb 27 '17 at 10:19
  • That was an interesting one ! Except... I need to be able to run jQuery in the page context. The scripts I run are interacting with the page JS variables ( it uses https://github.com/KartikTalwar/gmail.js which is based on jquery also ). Putting Jquery as a content script will not allow me to use it in injected scripts, as explained here : http://stackoverflow.com/questions/3209790/jquery-in-google-chrome-content-script#comment40380857_3227225 – Jeremy Belolo Feb 27 '17 at 14:57
  • 1
    No, my point is that currently you're injecting the code from a content script that runs at "document_end" or "document_idle", so try doing it at "document_start", see the content script documentation. – wOxxOm Feb 27 '17 at 14:59
  • Yes, I just tried that also, and sadly Boomerang still make it happen - jQuery is `undefined` ! And when I type jQuery in the console, it says `undefined` also – Jeremy Belolo Feb 27 '17 at 15:18
  • 1
    Well, this is as far as I can get without trying the extension myself. – wOxxOm Feb 27 '17 at 15:19
  • 1
    Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this usually means that you need to include your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Feb 27 '17 at 15:20
  • 1
    Basically, with the limited information in the question we are reduced to **guessing** and *assuming* as to what might be the problem and possible solutions. When that is the case, it almost always indicates that there is not enough information in the question. – Makyen Feb 27 '17 at 15:24
  • I think you're right. I will be providing a MCVE soon if the information I just added to the question doesn't help solve it. – Jeremy Belolo Feb 27 '17 at 15:59
  • 1
    The code you've just added doesn't modify jQuery variable, it simply invokes noConflict(true) – wOxxOm Feb 27 '17 at 19:58

1 Answers1

0

So, I worked it around. Here is what I did. I delayed the injection of jQuery and its dependencies, injecting only the main script. And with a recursive setTimeout I was able to inject jQuery from the injected script itself, by calling the centent_script file using a custom event document.dispatchEvent(new CustomEvent("eventName", {})). When the content_script captures this, it then injects jQuery to the page, and dispatches another custom event that I use to trigger the main function, inserting all I need to insert into the dom happily using jquery !

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88