EmEditorみんなでまとめサイト

Urlパラメータ展開マクロ

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集
ActivePointX = document.selection.GetActivePointX(eePosView)
ActivePointY = document.selection.GetActivePointY(eePosView)
AnchorPointX = document.selection.GetAnchorPointX(eePosView)
AnchorPointY = document.selection.GetAnchorPointY(eePosView)

'選択範囲の端点とカーソル位置のどちらが小さい(左上)か
bGreater = document.selection.IsActiveEndGreater

document.selection.text = UrlParams(document.selection.text)

if bGreater then
    document.selection.SetAnchorPoint eePosView, AnchorPointX, AnchorPointY
else
    document.selection.SetAnchorPoint eePosView, ActivePointX, ActivePointY
end if

Function UrlParams(strSource)

 Dim lngSourceSize                                          'strSource の文字数
 Dim strArray()                                             '変換された文字列を格納する配列
 Dim strSingle                                              '抜き出された 1 文字を格納する変数
 Dim i                                                      'ループカウンタ
 Dim intAsc                                                 '文字コード
 Dim strHex                                                 '16 進数に変換した文字コード
 Dim lngHexLength                                           'strHex の文字数
 Dim lngCount                                               'strArray の格納位置

    lngSourceSize = Len(strSource)
    If lngSourceSize = 0 Then Exit Function
    pos = instr(strSource, "?")
    ' ~.cgi?の部分を取り除く
    If pos > 0 Then
        UrlParams = left(strSource, pos) & vbcrlf
        strSource = right(strSource, lngSourceSize - pos)
    End If
    
    Set reg = new RegExp
    reg.Pattern = "([\w]+=)([%\w]*)\&?"
    reg.Global = true
    Set ms = reg.Execute(strSource)
    For Each m In ms
        Set s = m.SubMatches
        UrlParams = UrlParams & s.Item(0) & UrlDecode(s.Item(1)) & vbcrlf
    Next
End Function

Function UrlDecode(strEncoded)

 Dim lngLength                                                          '文字列の長さを格納する
 Dim strSingle                                                        '抜き出した 1 文字を格納する
 Dim lngReadCount                                                     '文字列読み込み位置カウンタ
 Dim lngAsc                                                             '1 文字分の文字コードを格納
 Dim BCode
 
    lngLength = Len(strEncoded)                                                 'URL エンコードされている文字列の長さを得る
    If Not CBool(lngLength) Then Exit Function                                  '0 文字の場合、関数を抜ける
    ReDim bytResult(lngLength - 1)                                              'デコード結果格納バッファ領域を確保
    lngReadCount = 1                                                            '読み込みカウンタは 1 から開始
    BCode = 0
    Do                                                                          '文字列の終端までループ
        strSingle = Mid(strEncoded, lngReadCount, 1)                           '1 文字を抜き出す
        strHex = "&H"                                                             '16 進表記文字コードを格納する領域を確保
        If strSingle = "%" Then                                                 '"%" であった場合
            ret = PersentDecode(Mid(strEncoded, lngReadCount + 1, 2), lngReadCount)
            if BCode = 0 and ret >= 128 then
                BCode = ret
            else
                UrlDecode = UrlDecode & Chr(BCode*256+ret)
                BCode = 0
            end if
        ElseIf strSingle = "+" Then                                             '"+" であった場合
            UrlDecode = UrlDecode & " "                                     '半角スペース(" ")を代わりに入れる
            lngReadCount = lngReadCount + 1                                     '読み込みカウンタをインクリメント
        Else                                                                    'その他の文字であった場合
            lngAsc = CLng(Asc(strSingle)) And &HFFFF&                           '文字コードを符号無し長整数型(嘘)にキャスト
            If lngAsc <= &HFF& Then                                             '"&HFF" 以下であった場合
                UrlDecode = UrlDecode & Chr(lngAsc)                        'バイト型にキャストし、配列に代入
                lngReadCount = lngReadCount + 1                                 '読み込みカウンタをインクリメント
            Else                                                                'その他の文字(マルチバイト文字)の場合
                lngReadCount = lngReadCount + 1                                 '読み込みカウンタをインクリメント
            End If
        End If
    Loop Until lngReadCount > lngLength
    
End Function

Function PersentDecode(str, byref rc)
    for i = Len(str) to 1 step -1
        str = left(str, i)
        strHex = "&H" & str
        If IsNumeric(strHex) Then
            PersentDecode = CByte(strHex)
            rc = rc + i + 1
            exit function
        End If
    next
    PersentDecode = &H25
    rc = rc + 1
End Function
ウィキ募集バナー