Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript listen for style changes made in developer-tools

Is there a way to detect style changes made only in the developer-tools (inspect element)?

I want to allow to save them if the user is logged in as admin.

like image 968
neoexpert Avatar asked Sep 20 '17 12:09

neoexpert


1 Answers

You can access the styles of a HTMLElement using element.style in JavaScript. So you could probably save the styles on page load, check them again when the user initiates a save and compare -- the difference would be the users changes.

As for detecting them as soon as they happen, you can use MutationObserver to get notified of attribute changes on the page. This works for when you add styles to a specific element in the Devtools, but not for editing existing styles and (I think) adding styles to existing selectors. I'm not sure if there is a way to get an event when these change.

Edit: actually, it seems as if checking element.style has the same limits as using MutationObserver: only styles added for the specific element (that is, styles that show up as inline styles in the DOM) show up. There might be a way to actually access all styles, but I'm not aware of it.

like image 126
klumme Avatar answered Oct 28 '22 14:10

klumme