0

EDIT: I managed to resolve my issue this way (not sure how bulletproof it is though)

Snippet from my content.js

 var script = document.createElement('script');
    script.src = chrome.runtime.getURL('injected.js');
    script.id = chrome.runtime.id;
    script.classList.add('rmd-block-extension');
(document.head || document.documentElement).appendChild(script);

Snippet from my injected.js

(function (XMLHttpRequest) {
    var XHR = XMLHttpRequest.prototype;
    var extensionID = '';

    XHR.send = function (postData) {
        this.addEventListener('load', function () {
                        var extensionSrc = document.querySelector('script.rmd-block-extension');

                        if (extensionSrc) {
                            extensionID = extensionSrc.id;
                            extensionSrc.remove();
                        }
                    } catch (err) {
                    }
                }
            }
        });
    };
})(XMLHttpRequest);

And it worked. I do have my extension ID inside my injected.js file :)


Alternative (better?) version:

content.js

var script = document.createElement('script');
    script.src = chrome.runtime.getURL('injected.js');
    script.id = chrome.runtime.id;

    (document.head || document.documentElement).appendChild(script);

injected.js

var extensionID = '';
var script = document.currentScript;

if (script) {
    extensionID = script.id;
    script.remove();
}

Initial question

I tried literally everything including this long essay: Access variables and functions defined in page context using a content script but nothing is working..

I use method 1:

  var script = document.createElement('script');
    script.src = chrome.runtime.getURL('injected.js');
    script.onload = function () {
        this.remove();
    };

(document.head || document.documentElement).appendChild(script);

I need to access my extension id in my web_accessible_resources files because I need to send some messages there. I can hardcode that but thats not the point. I want to use chrome.runtime.id result there.

Is there an easy way to do that? Inline scripting is not working (policy error - tried with sha etc but without any success, unless im doing something wrong.. No idea).

Any help appreciated.

Thanks.

pasteam2
  • 23
  • 3
  • It depends on the method you use. In 1) you can add `'?id=' + chrome.runtime.id` to `src` and then access it as document.currentScript.src inside. In executeScript you can use `args` parameter. – wOxxOm May 02 '23 at 18:12
  • @wOxxOm just tried that and `document.currentScript` is always `null` inside injected script. But was able to pass as argument though.. https://prnt.sc/JPWaRtnSEdrR – pasteam2 May 02 '23 at 18:17
  • It works only in method 1 as I explained. Also note that it's defined only at the start of the script, not inside a later event, so you need to save the id immediately. – wOxxOm May 02 '23 at 18:36
  • @wOxxOm I do use method 1. Edited and added snippet from my code. – pasteam2 May 02 '23 at 18:43
  • Try removing the onload block and instead use document.currentScript.remove() inside the injected code. – wOxxOm May 02 '23 at 19:36
  • Ok.. I came up with another idea and it worked! Not sure how bulletproof it is though. I have added these two lines in conent.js `script.id = chrome.runtime.id;` `script.classList.add('rmd-block-extension');` then in `injected.js` I just get that element by its class and there is an extension id that I need :) The only issue is I CAN'T remove the script like I did with the onload callback. – pasteam2 May 02 '23 at 19:50
  • Ok I fixed that as well. Edited the main thread. – pasteam2 May 02 '23 at 19:55
  • Your code is inside the listener, but I said it should be the first thing in the script e.g. just move that code to `var extensionID` scope. – wOxxOm May 03 '23 at 04:26
  • Oh.. I seeee.. Yep. Now I can access that via `document.currentScript` property. Thanks. – pasteam2 May 03 '23 at 10:17

0 Answers0