1

HTML : <html> <script scr="http://someurl.com/jscript.js"></script></html>

I'm trying to extract source code of jscript.js in chrome extension.

But there is no attribute of DOM Object holding source of js.

Is there a way to extract the source code of JavaScript which is loaded on page? (By using DOM object or some internal object, except re-downloading the script)

Because.. Some web-server returns different source code depends on the request-packet (Usually.. BAD servers do that). So If I tried to download it with different request, I can't get the same one that was loaded on the browser.

Xan
  • 74,770
  • 16
  • 179
  • 206
nemo
  • 171
  • 2
  • 12
  • 1
    Not sure I understand the question. In devtools the source is shown in Sources panel. In extension you can use XMLHttpRequest or various analogs to redownload the script. – wOxxOm Sep 29 '16 at 09:45
  • @wOxxOm I guess the question can be read "is it possible to get the contents using only DOM". And, implicitly, "if not, how to deal with it". – Xan Sep 29 '16 at 10:10
  • @wOxxOM Thank you for the comment. Yes I saw the way to redownload them. But I just wanted to get them with DOM or some Internal Object of browser :) – nemo Sep 30 '16 at 05:28
  • @Xan Right. You refined my question :). Thank you. – nemo Sep 30 '16 at 05:28

2 Answers2

3

According to Is external JavaScript source available to scripting context inside HTML page?, it's not normally possible without redownloading since it's not exposed to the DOM.

An extension, however, can hook into information available to the browser.

The simplest would be to create a DevTools extension. It would only work when the DevTools are open on the page, but then you can easily access the source with chrome.devtools.inspectedWindow.getResources().

Somewhat harder, but one can use chrome.debugger API to achieve the same while DevTools are closed. It's a low-level API, but it allows doing everything DevTools can do. I don't have a ready example, but Debugger Protocol docs will help.

Neither is possible from a content script.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Thank you :) It is very helpful. – nemo Sep 30 '16 at 08:56
  • You can also look at the Sources section of dev tools. To simplify the problem, create a dummy website of hello world – Michael Dimmitt Dec 26 '20 at 07:03
  • If you want the background script info, you have to go to the extension and click inspect the background page, sometimes you will find information on both the content script of the sources section of the page that you are on and the contents of the sources section of the background page which will contain the background script. – Michael Dimmitt Dec 26 '20 at 07:06
  • You might need to change the from the page tab to the content script tab in the sources section of dev tools. – Michael Dimmitt Dec 26 '20 at 07:12
0

You can also go directly to the extension on your file system:

Where to find extensions installed folder for Google Chrome on Mac?

for example on my mac book computer:

pwd

output: ~/Library/Application\ Support/Google/Chrome/Default/Extensions/hkbhjllliedcceblibllaodamehmbfgm/1.7.1_0

Michael Dimmitt
  • 826
  • 10
  • 23