8/25/11

CSS Selector & XPath Expression Browser Commands

CSS selectors and XPath expressions can be used to answer queries such as
- How many scripts does this page have?
- How many are external? What are they?
- How many are inline? Show me the source.

CSS Selectors - W3
"Selectors are patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in an XML document. Selectors have been optimized for use with HTML and XML, and are designed to be usable in performance-critical code."

Firefox and Chrome support CSS and XPath commands in the console via the Firebug Command Line API.

Syntax
$$(selector)  Returns an array of elements that match the given CSS selector.
$x(xpath)     Returns an array of elements that match the given XPath expression.
$(id)         Returns a single element with the given id.
CSS Selector Examples
//scripts
$$("script")                     //script tags
$$("script").length              //count
$$("script[async]")              //async scripts
$$("script[src]")                //external scripts with src
$$("script:not([src])")          //inline scripts with no src not
$$("script:not([src])")[0].text  //show inline script source code

//list external scripts src url
var x = $$("script[src]"); console.log("page: " + window.location.href + "\n" + x.length + " external scripts"); for(var i = 0;i < x.length; i++){console.log(x[i].src)}

//list inline script source code
var x = $$("script:not([src])"); console.log("page: " + window.location.href + "\n" + x.length + " internal scripts"); for(var i = 0;i < x.length; i++){console.log("script #" + (i + 1.0) + "\n" + x[i].text)}

//*************
//anchors
$$("a")                   //anchor tags
$$("a").length            //count
$$("a[class]")            //anchors with class
$$("a[href]")             //anchors with hrefs
$$("a:not([href])")       //anchors with no hrefs not
$$("a[href*=foo]")        //anchors with href url containing "foo"
$$("a[href*=\\?]")        //anchors with href url containing querystring
$$("a[onclick]")          //anchors with onclick event
$$("a[onclick^=foobar]")  //anchors with onclick event name starts with foobar

//list anchor link href urls
var x = $$("a[href]"); console.log("page: " + window.location.href + "\n" + x.length + " anchor urls"); for(var i = 0;i < x.length; i++){console.log(x[i].href)}

//*************
//images
//list image src urls
var x = $$("img[src]"); console.log("page: " + window.location.href + "\n" + x.length + " image urls"); for(var i = 0;i < x.length; i++){console.log(x[i].src)}

//*************
//iframes
//list iframe src urls
var x = $$("iframe"); console.log("page: " + window.location.href + "\n" + x.length + " iframes"); for(var i = 0;i < x.length; i++){console.log("id: " + x[i].id +"\nsrc: "+ x[i].src)}

//*************
//meta tags
$$("meta[name^=descr]")[0].content  //description meta tag, meta tag with name starts with "descr"
$$("meta[name$=words]")[0].content  //keywords meta tag, meta tag with name ends with "words"

//list meta tags
var x = $$("meta"); console.log("page: " + window.location.href + "\n" + x.length + " meta tags"); for(var i = 0;i < x.length; i++){console.log(x[i])}

//*************
//all elements
$$("[class]")      //elements with class
$$("[class=foo]")  //elements with class name equal "foo" match
$$("[id]")         //elements with id

//list element ids
var x = $$("[id]"); console.log("page: " + window.location.href + "\n" + x.length + " ids"); for(var i = 0;i < x.length; i++){console.log(x[i].id)}

CSS Selector Syntax: www.w3.org/TR/css3-selectors/#selectors
Chrome Dev Tools Resources UI can be used to access image and external script info.
Don't even ask about ie just view source.


8/1/11

Visual Studio 2010 VB.NET Macros

'Visual Studio 2010 VB.NET Macros
'www.expressionsoftware.com
'v11.8.1

Imports System
Imports System.Collections.Generic
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100

Public Module ExpressionSoftwareMacros

    Sub PrintFilenamesForOpenFiles()
        Dim output = "macro: PrintFilenamesForOpenFiles" + vbCrLf
        Dim outputFiles As String
        Dim openFiles = GetOpenFiles()

        'openFiles.Sort()  'error: failed to compare two elements in the array
        openFiles.Sort(New EnvDteDocumentComparerClass())

        For Each file In openFiles
            outputFiles += "file: " + file.FullName + vbCrLf
        Next

        output += "date: " + Date.Now + vbCrLf
        output += "count: " + Str(openFiles.Count) + vbCrLf
        output += outputFiles
        Out(output)
    End Sub

    Sub HideLinebreakToBrace()
        Dim output = "macro: HideLinebreakToBrace" + vbCrLf
        Dim count = 0

        'vs pofs regex
        ':b whitespace or tab
        'Dim searchString = ":b*\n:b*\{"  'matches "\b\n{" which cannot be hidden, can use if count is not output, else count incorrect
        Dim searchString = "{:b+\n:b*\{}|{:b*\n:b+\{}"

        DTE.Find.FindWhat = searchString
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument  'search current file only
        DTE.Find.Action = vsFindAction.vsFindActionFind
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr  'regex search
        DTE.SuppressUI = False

        DTE.ActiveDocument.Selection.StartOfDocument()  'set cursor top of file
        While DTE.Find.Execute() <> vsFindResult.vsFindResultNotFound
            count += 1
            DTE.ActiveDocument.Selection.CharLeft(True, 1)  'unselect brace char
            DTE.ExecuteCommand("Edit.HideSelection")
        End While
        DTE.ActiveDocument.Selection.StartOfDocument()

        output += "date: " + Date.Now + vbCrLf
        output += "file: " + DTE.ActiveDocument.FullName + vbCrLf
        output += "count: " + Str(count)
        Out(output)
    End Sub

    Private Function GetOpenFiles() As List(Of EnvDTE.Document)
        Dim openFiles As New List(Of EnvDTE.Document)

        For Each doc As EnvDTE.Document In DTE.Documents
            If doc.ProjectItem IsNot Nothing Then  'todo: combine 
                If doc.ProjectItem.IsOpen() Then
                    openFiles.Add(doc)
                End If
            End If
        Next

        Return openFiles
    End Function

    Private Sub Out(ByVal output)
        GetMacrosOutputWindow().OutputString(output)
    End Sub

    'requires Microsoft Samples Utilities module,
    'copy module into MyMacros project
    Private Function GetMacrosOutputWindow() As OutputWindowPane
        Dim windowTitle = "Expression Software Macros"
        Dim window As Window
        Dim target As Object
        Dim document As EnvDTE.Document

        window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow)
        If DTE.ActiveWindow Is window Then
            target = window.Object
        Else
            target = Utilities.GetOutputWindowPane(windowTitle)
            target.clear()
        End If

        Return target
    End Function
End Module


Public Class EnvDteDocumentComparerClass
    Implements IComparer(Of EnvDTE.Document)

    'compare docs by full filename, for sorting
    Function Compare(ByVal file1 As EnvDTE.Document, _
                     ByVal file2 As EnvDTE.Document) _
                     As Integer Implements IComparer(Of EnvDTE.Document).Compare

        Return file1.FullName.CompareTo(file2.FullName)
    End Function
End Class


Public Class EnvDteDocumentComparableClass
    Implements IComparable(Of EnvDTE.Document)

    'compare docs by full filename, for sorting
    Function CompareTo(ByVal other As EnvDTE.Document) _
                       As Integer Implements IComparable(Of EnvDTE.Document).CompareTo

        Return FullName.CompareTo(other.FullName)
    End Function
End Class


'**************************************************************
'Misc
'DTE.Documents.Count includes closed docs
'DTE.ActiveDocument.FullName
'output += "date: " + Date.Now + vbCrLf
'Microsoft.VisualBasic.Constants.vbCrLf
'DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
'keyboard shortcuts
'  alt + F8    macro explorer
'  alt + F11   macros ide
'**************************************************************