Skip to content

Instantly share code, notes, and snippets.

@NV
Created November 24, 2012 22:11
Show Gist options
  • Save NV/4141606 to your computer and use it in GitHub Desktop.
Save NV/4141606 to your computer and use it in GitHub Desktop.
Append style rules created in elements panel to last included CSS file
<script src="devtools.js"></script>
var styleSheets = [];
function addStyleSheet(resource) {
if (resource.type === 'stylesheet' && resource.url.lastIndexOf('inspector://', 0) === -1) {
console.log('Add', resource.url);
styleSheets.push(resource);
}
}
chrome.devtools.inspectedWindow.getResources(function(resources) {
console.log('getResources');
resources.forEach(addStyleSheet);
});
chrome.devtools.inspectedWindow.onResourceAdded.addListener(function(resource) {
console.log('onResourceAdded', resource.url);
addStyleSheet(resource);
});
chrome.devtools.inspectedWindow.onResourceContentCommitted.addListener(function(resource, content) {
if (resource.url.lastIndexOf('inspector://', 0) === -1)
return;
if (styleSheets.length === 0)
return;
var lastStyleSheet = styleSheets[styleSheets.length - 1];
lastStyleSheet.getContent(function(oldContent) {
lastStyleSheet.setContent(oldContent + '\n\n' + content, true);
resource.setContent('', false);
});
});
chrome.devtools.network.onNavigated.addListener(function() {
console.log('onNavigated');
styleSheets = [];
});
{
"name": "New styles pipe",
"version": "1.0.0",
"description": "Pipe newly created styles to last included CSS file",
"devtools_page": "devtools.html",
"manifest_version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment