How to view css stylesheet injected by a Google Chrome extension using dev tools?
D

3

25

I'm injecting a css file from my chrome extension using the manifest.json (full source):

  "content_scripts": [
    {
      "matches": [
        "http://*/*"
      ],
      "css":["src/inject/gfi_extension.css"],
      "js": [/*...*/]
    }
  ] 

In Chrome Dev Tools, if I inspect an element which is affected by the injected css, the rules are visible, but in the upper right where the source filename would normally be, it just says "injected stylesheet." I'd like to view ALL of the rules being injected, even those that affect elements which do not currently exist in the DOM.

I would have expected the .css to appear in "sources" under "content scripts" with the .js files, but they aren't there.

Is there a way to view the .css file through dev tools? If not, please explain why there isn't.

Edit: I've just found that this question also appears as a "sub-question" of this one, however no one over there has attempted to answer it, even though there is an accepted answer.

Derivation answered 9/2, 2015 at 3:50 Comment(12)
The chrome.devtools.* API modules are available only to the pages loaded within the DevTools window. Content scripts and other extension pages do not have these APIs. Thus, the APIs are available only through the lifetime of the DevTools window. To see the link for more details: developer.chrome.com/extensions/devtoolsNyctaginaceous
gui47, thanks, but I still don't understand. Why can I see the injected .js files but not the .css? Is this because the css is somehow injected outside of the "lifetime of the DevTools window"? Also, if you've got an answer, post it as an answer!Derivation
You can see the injected.js under content scripts tag is because content scripts run in the context of webpages which are inside of the lifetime of the DevTools window. While as you mentioned the css is not through the lifetime of the DevTools window.Nyctaginaceous
Ok, thanks... that's my upvote on your comment, because you are trying to help and seem like maybe you're making sense, but I still don't quite get it. I mean, clearly DevTools can access things which are loaded and applied to the DOM long before DevTools was opened, such as "normal" css files. So why doesn't Chrome just apply it like a normal css file?Derivation
According to Match Pattern documentation, maybe it is because the schema section is very strict in what kind of URL's it allows, and "chrome-devtools" is not one of them. developer.chrome.com/extensions/match_patternsNyctaginaceous
Neither of your explanations explains the fact that injected .js shows up, but injected .css does not. Therefore, your explanations are incorrect. I wish you had made it clear that you were just guessing -- that would have saved me a lot of time trying to figure out what you mean!Derivation
Apologize for the misleading. Content scripts are JavaScript files contained within Chrome extensions that run in the context of web pages: developer.chrome.com/devtools/docs/settings while injected.css isn't showing up: #18534320Nyctaginaceous
Ok, that question you linked to is helpful. Was there supposed to be something useful in the settings link? And yeah... I know what content scripts are, since I write them!Derivation
@PaulSweatte So, do we have a little edit war here? Please explain how tags css and content-script are not relevant here. More importantly, explain how super-generic tags manifest and file-browser are relevant here.Honghonied
@Xan. The question includes manifest.json and view the .css file.Mendiola
@PaulSweatte True. Please read the tag description of the manifest tag - it's recommended not to use it as it's very generic. The only relevant portion of the manifest to the actual question is that it's a content script, and a Chrome extension always includes a manifest, so it's redundant. Would you agree to swap manifest for content-script, and the extremely generic user-interface to a more relevant css?Honghonied
@xan I'd agree with content-script and file-browser, since this question is essentially about the how to browse files in the chrome developer tools that are declared in the manifest.json content_scripts array.Mendiola
T
9

Looks like there's no way to do this if you inject the CSS via content_scripts. I filed a bug here: https://crbug.com/800070

When the extension is in your control, Paul Irish suggests using this code pattern in order to make your styles inspectable: https://github.com/lateral/chrome-extension-blogpost/compare/master...paulirish:master

For other people's extensions, as far as I can tell there's no way to view the source code of the injected stylesheets in DevTools, if you go the content_scripts route.

Trollop answered 8/1, 2018 at 20:42 Comment(0)
C
-4

Go to Sources and then Content Scripts. You have to go to the extension name and then you'll see the injected files.

enter image description here

Comte answered 8/6, 2016 at 11:12 Comment(1)
This tab shows only Javascript files, CSS style sheets are not shown here.Anisette
M
-6

Use the Chrome App and Extensions Developer Tool on an extension which injects CSS, such as Bootstrap Grid Overlay:

Chrome App Extensions Developer Tool

the injected URL can be used on the console tab on the app to get the runtime URL using the getURL method:

chrome.runtime.getURL("src/grid.css")

and produce the source:

grid.css

References

Mendiola answered 30/7, 2015 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.