Is it possible to access JavaScript fragments using the chrome.devtools API?

I want to create extensions for Chrome developers who need access to recently added snippets in the source panel.

Does the chrome.devtools API have any way to access fragments?

enter image description here

+8
google-chrome google-chrome-devtools google-chrome-extension
source share
2 answers

Yes, you can do this via chrome.devtools.inspectedWindow API ()

You can track

a) Content of all available fragments

b) When a new fragment and its contents are added

c) When Snippet is updated with new content \ modified.

As always to enable debugging, etc. You must enable the experimental developer flags .

You can take the following code as a reference, and you can renew it according to your requirement.

manifest.json

You must add

"devtools_page": "devtools.html",

to manifest.json file

Manifest.json example

{ "name":"Snippets Demo", "description":"This demonstrates How to get content from Snippets API", "devtools_page":"devtools.html", "manifest_version":2, "version":"2" } 

devtools.html

Add devtools.js to avoid inline scripts

Devtools.html example

 <html> <head> <script src="devtools.js"></script> </head> <body> </body> </html> 

devtools.js

Add related code for

a) chrome.devtools.inspectedWindow.getResources

b) chrome.devtools.inspectedWindow.onResourceAdded.addListener

c) chrome.devtools.inspectedWindow.onResourceContentCommitted.addListener ()

Devtools.js example

 //Fetching all available resources and filtering using name of script snippet added chrome.devtools.inspectedWindow.getResources(function (resources){ // This function returns array of resources available in the current window for(i=0;i<resources.length;i++){ // Matching with current snippet URL if(resources[i].url == "Script snippet #1"){ resources[i].getContent(function (content,encoding){ alert("encoding is " + encoding); alert("content is "+content); }); } } }); //This can be used for identifying when ever a new resource is added chrome.devtools.inspectedWindow.onResourceAdded.addListener(function (resource){ alert("resources added" + resource.url); alert("resources content added " + resource.content); }); //This can be used to detect when ever a resource code is changed/updated chrome.devtools.inspectedWindow.onResourceContentCommitted.addListener(function(resource,content){ alert("Resource Changed"); alert("New Content " + content); alert("New Resource Object is " + resource); }); 

After combining all three codes you will receive

Output 1)

enter image description here

Output 2)

enter image description here

Output 3)

enter image description here

Hope this helps :)

+11
source share

I was looking for this, but the accepted answer is pretty old, and since January 2016 you have not been able to access the fragments via localStorage .

also see:

https://github.com/bahmutov/code-snippets/issues/23
Which file contains fragments of the Chrome Dev Tool?

0
source share

All Articles