I want to write an application that can detect whether a value in CSS/HTML file has changed or not, how can I do that ?

Please post example code / import as much as possible. Tkq

Dani AI

Generated

Given 's goal, detecting whether a CSS/HTML value changed depends on context: a local file on disk, a remote URL, or the live DOM inside a browser. As noted, a baseline is required (original file, stored checksum, or DOM snapshot). Decide first whether formatting-only differences (whitespace, comments, ordering) count or only semantic changes (a selector/property/value).

For local or server-side files prefer an OS watcher (e.g., FileSystemWatcher on Windows or inotify on Linux) for immediate events, and maintain a normalized cryptographic hash (SHA-256) of the file to detect real content changes. Normalization matters: strip comments, collapse whitespace, and remove irrelevant metadata before hashing if those things should be ignored. Store a filename->hash map (or use git) and compare on change events for a robust, low-footprint solution.

For remote URLs prefer conditional HTTP (HEAD or GET with If-Modified-Since / If-None-Match) so the server can return 304 Not Modified or provide an ETag. If the server does not provide reliable headers, fetch and hash the resource. Polling (as demonstrated) works but use sensible intervals, proper connection handling, and respect caching headers to avoid unnecessary downloads.

Inside the browser use MutationObserver to detect DOM or inline-style changes, and inspect document.styleSheets / cssRules for stylesheet-level differences — note cross-origin stylesheets block access to cssRules unless CORS is allowed. If the target is specific declarations, parse CSS/HTML into an AST (PostCSS/ExCSS/cssutils or an HTML parser like HtmlAgilityPack) and compare declarations by selector after normalization. Exclude known volatile pieces (timestamps, ads, session tokens) before comparing to avoid false positives.

Recommended Answers

All 9 Replies

For starters you would need to have a benchmark or base value to establish against the file to be tested.

From there you would basically have to do a line by line string by string (or char by char) comparison between the two files to determine changes between the base value and the current file (of course you can try to determine if the file is identical first but file size and creation date/time are not always good identifiers of sameness).

As for posting example code... sorry, don't have any pre-made and too tired to try to write it for you right now :twisted: but based on the information above you should be able to make a few educated steps in the right direction for what you're looking to do.

Don't be afraid to do some research on file reading to figure out how that works :)

Can I use XML reader for the CSS style file ?

You could, yes, but the principle remains the same... you need a benchmark file to compare to item by item in order to determine if a change has occurred.

Can I just use a variable to store the value at first then later check the value again, compare to the variable ?

Not sure what you mean there... Your initial question was how to check for a change in the file. In order to do that you need to have the original file vs the file being checked. Only other way that would be quicker and easier is if you had a stored table of checksum values for your files and compared against that.

I have done a simple code for you because i am new to C# as well so just took this question as a task:

public void Detect_changes_in_webpage()
        {
            String url,output,compare_output;
            Console.WriteLine("Enter the URL:");
            url = Console.ReadLine();

            Uri uri = new Uri(url);

            WebRequest RQ = WebRequest.Create(uri);
            WebResponse RE = RQ.GetResponse();
            Stream STR = RE.GetResponseStream();
 
            StreamReader SR = new StreamReader(STR);

            output = SR.ReadToEnd();

            Console.Write("\n The output of the webpage is given below:");

            Console.Write(output);

            compare_output = output;

            while (compare_output == output)
            { 
            

            //Just wait for the webpage to change

                Thread.Sleep(5000);
 
                output = SR.ReadToEnd();

                Console.WriteLine("\nChecking... Web Page not updated");
                
            
            }

            Console.WriteLine("\nThe webpage has changed");

        
        }

Ofcourse you have to add main method and put the method in the class etc.

I guess I should add another request and response in the "while" part to get it work. Anw, tkq

Yes you have to add few lines, here is the modified code, hope this helps:

public void Detect_changes_in_webpage()
        {
            String url,output,compare_output;
            int result_comp=0;
            Console.WriteLine("Enter the URL:");
            url = Console.ReadLine();

            Uri uri = new Uri(url);

            WebRequest RQ = WebRequest.Create(uri);
            WebResponse RE = RQ.GetResponse();
            Stream STR = RE.GetResponseStream();
 
            StreamReader SR = new StreamReader(STR);

            output = SR.ReadToEnd();

         //   Console.Write("\n The output of the webpage is given below:");

           // Console.Write("{0}",output);

            
            compare_output = output;
            
 
            while (result_comp < 2 || result_comp > -2)
            {
                
                //Just wait for the webpage to change
                RQ = WebRequest.Create(uri);
                RE = RQ.GetResponse();
                STR = RE.GetResponseStream();

                SR = new StreamReader(STR);

                Thread.Sleep(5000);
                output = SR.ReadToEnd();
                         
                Console.WriteLine("\nChecking... Web Page not updated");
                Console.WriteLine("{0}", result_comp);

                result_comp = String.Compare(compare_output, output);
            
            }

            Console.WriteLine("\nThe webpage has changed");
            Console.WriteLine("{0}", result_comp);

        
        }

tkq but i've modified myself anw. it works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.