Last active
March 4, 2017 14:48
-
-
Save TLMcode/5c9163d15d691e0051600bc72613bbb5 to your computer and use it in GitHub Desktop.
GetHTMLFragment() returns markup from the clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
F1:: | |
Fragment := GetHTMLFragment().getElementsByTagName( "html" )[ 0 ].OuterHTML | |
msgbox % SubStr( Fragment, 1, 200 ) "`n....`n" SubStr( Fragment, -200 ) | |
ExitApp | |
CheckClipboard() | |
{ | |
For Each, FmtNme in ( GetClipFormatNames( EnumClipFormats() ), HTMLFmt := false ) | |
{ | |
if ( FmtNme = "HTML Format" ) | |
{ | |
HTMLFmt := true | |
Break | |
} | |
} | |
DllCall( "CloseClipboard" ) | |
Return HTMLFmt | |
} | |
EnumClipFormats() | |
{ | |
FmtArr := [], DllCall( "OpenClipboard" ) | |
While ( DllCall( "CountClipboardFormats" ) >= a_index ) | |
FmtArr.Push( fmt := DllCall( "EnumClipboardFormats", uint, a_index = 1 ? 0 : fmt ) ) | |
Return FmtArr | |
} | |
GetClipFormatNames( FmtArr ) | |
{ | |
if ( FmtArr.Length() = False ) | |
{ | |
DllCall( "CloseClipboard" ) | |
Throw "Empty Clipboard Format Array!" | |
} | |
For Each, Fmt in ( FmtArr, FmtNmArr := [], VarSetCapacity( Buf, 256 ) ) | |
{ | |
DllCall( "GetClipboardFormatName", uInt, Fmt, str, Buf, int, 128 ) | |
if ( Asc( StrGet( &buf ) ) != False ) | |
FmtNmArr.Push( StrGet( &buf ) ) | |
} | |
Return FmtNmArr | |
} | |
GetHTMLFragment() | |
{ | |
FmtArr := EnumClipFormats(), NmeArr := GetClipFormatNames( FmtArr ) | |
While ( a_index <= NmeArr.Length() && !ClpPtr ) | |
if ( NmeArr[ a_index ] = "HTML Format" ) | |
ClpPtr := DllCall( "GetClipboardData", uInt, FmtArr[ a_index ] ) | |
DllCall( "CloseClipboard" ) | |
if ( !ClpPtr ) | |
{ | |
MsgBox, 0x10, Whoops!, Please Copy Some HTML! | |
Exit | |
} | |
Return ScrubFragmentIdents( StrGet( ClpPtr, "UTF-8" ) ) | |
} | |
ScrubFragmentIdents( HTMFrag ) | |
{ | |
Static HTMObj := "" | |
HTMObj := ComObjCreate( "HTMLFile" ), HTMObj.Write( HTMFrag ) | |
For Needle, Replace in { "(>)v\w{6}:[\d\.]+.*?--st.*?->" : "$1 <p>SourceURL: " GetSourceURL( HTMObj.getElementsByTagName( "*" )[ 0 ].outerHtml ) "</p>", "<!--(s|e).*?-->" : "" } | |
MarkUp := RegExReplace( ( a_index = 1 ? HTMObj.getElementsByTagName( "HTML" )[ 0 ].OuterHtml : MarkUp ), "si)" Needle, Replace ) | |
HTMObj.Close(), HTMObj.Write( MarkUp ) | |
Return HTMObj | |
} | |
GetSourceURL( str ) | |
{ | |
FragIdent := RegExReplace( str, "i).*<b.*?>(.*?<!--s\w{11}t-->).*", "$1" ) | |
For Each, Ident in StrSplit( FragIdent, " " ) | |
if InStr( Ident, mStr := "SourceURL:" ) | |
SourceURL := SubStr( Ident, StrLen( mStr )+1 ) | |
Return SourceURL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment