Monday, 28 November 2011

Building an IE Add-On - Part 2

Picking up from my previous post Site-specific stylesheets in Internet Explorer, we have a registered browser add-on that forces every website to have links appearing in red.

The next step is adding something that could be argued to be site-specific behaviour, so we first need to figure out how we'll match a website against our custom styles.  As we don't have a nice conditional syntax (@-moz-document domain(bing.com){...}) we'll go with the simplest possible thing for now, and have zero or one stylesheet per site.

Putting that into code we get the following:

        void _webBrowser2Events_DocumentComplete(object pDisp, ref object URL) {
            //grab the document
            HTMLDocument document = _webBrowser2.Document;

            //grab the css text for the document url
            var cssText = GetCssText(document.url);
            if (cssText == null) return;

            //add the css text
            var css = document.createStyleSheet("", 0);
            css.cssText = cssText;
        }

        static string GetCssText(string url) {
            var uri = new Uri(url);
            switch(uri.Host.ToLowerInvariant()) {
                case "www.bing.com":
                    return "a{ color:red !important; }";
                case "www.google.co.uk":
                    return "a{ color:purple !important; }";
            }
            return null;
        }


Which now only affects bing and google, with google now using purple for links.  Definitely an improvement and another step towards site-specific stylesheets.


0 comments:

Post a Comment