◀マクロトップへ
  • 197 テキストブラウザ化するマクロ
    • 198 Re:テキストブラウザ化するマクロ
  • [197] テキストブラウザ化するマクロ ぷる 2005年08月26日 21:52

    はじめまして。

    Ajaxに似たアプローチで、サクラエディタがテキストブラウザに
    ならないかと思って、ちょっとだけやってみました。


    var selection = Editor.GetSelectedString(0);
    if(selection.length == 0)
    {
    selection = "http://www.yahoo.co.jp/";
    }
    else
    {
    if(!selection.match(/:\/\/.*/))
    {
    Editor.GoFileTop();
    Editor.Down();
    Editor.GoLineEnd_Sel(0);
    selection = selection.replace(/^\//, "");
    selection = Editor.GetSelectedString(0) + selection;
    }
    }

    Editor.SelectAll();
    Editor.InsText(selection + " を読み込み中...");
    Editor.GoFileTop();
    Editor.ReDraw();

    var http = new ActiveXObject("Microsoft.XMLHTTP");

    if(http != null)
    {
    var uri = selection;
    http.open("GET", uri, false);
    http.send(null);

    var html = http.responseText;
    var result = "";
    result += "------------------------------------------------------------\r\n";
    result += selection + "\r\n";
    result += "------------------------------------------------------------\r\n";
    result += html;

    Editor.SelectAll();
    Editor.InsText(result);
    Editor.GoFileTop();
    Editor.ReDraw();
    }


    ・URLを選択して、このマクロを実行します。
    ・URLを選択しないで実行すると、Yahoo!へいきます。
    ・いわゆる「戻る」はアンドゥ2回で行えます。

    ・ただソースを表示するだけです。
    ・非同期では動かなかった(何も起こらなかった)ので、同期通信です。

    叩き台にもならないレベルで申し訳ないのです。

    簡単にでもhtmlの構文解析をして、テキストブラウザとして
    動作する程度にまでもっていきたかったのですが、
    私のスキルではこれが限界でした。

    なにかいい方法ないでしょうか・・・。
    • [198] Re:テキストブラウザ化するマクロ (全略) 2005年09月06日 00:07

      ▼ ぷるさん
      > Ajaxに似たアプローチで、サクラエディタがテキストブラウザに
      > ならないかと思って、ちょっとだけやってみました。
      AjaxってなもんじゃなくてもIEのオブジェクトを使えばもっと楽なんじゃないでしょうか?
      面白そうなのでやってみました↓vbsですが

      Dim uri
      uri = Editor.GetSelectedString(0)
      If uri = "" Then
      uri = "http://www.yahoo.co.jp/"
      Else
      If Left(uri, 7) <> "http://" And InStr(uri, "/") > 0 Then
      Editor.GoFileTop()
      Editor.GoLineEnd_Sel(0)
      uri = Right(uri, Len(uri) - InStr(uri, "/"))
      uri = Editor.GetSelectedString(0) & uri
      End If
      End If

      Editor.SelectAll()
      Editor.InsText(uri & " を読み込み中...")
      Editor.GoFileTop()
      Editor.ReDraw()

      Dim ie
      Set ie = CreateObject("InternetExplorer.Application")

      If Not ie Is Nothing Then
      ie.Visible = False
      ie.Navigate(uri)

      Do While ie.Busy
      Loop

      Dim body

      body = uri & vbCrLf
      body = body & ie.document.title & vbCrLf
      body = body & "------------------------------------------------------------" & vbCrLf
      body = body & get_html(ie.Document.Body.childNodes, "")

      Editor.SelectAll()
      Editor.InsText(body)
      Editor.GoFileTop()
      Editor.ReDraw()

      ie.Quit()
      End If

      Function get_html(ByVal document, ByVal margin)
      Dim v

      For Each v In document

      Select Case TypeName(v)
      Case "DispHTMLDOMTextNode":
      Dim dv, prev_dv
      dv = Trim(v.data)
      Do
      prev_dv = dv
      dv = Replace(dv, " ", " ")
      Loop While dv <> prev_dv

      If dv <> "" Then get_html = get_html & dv
      Case "HTMLAnchorElement":
      get_html = get_html & get_html(v.childNodes, margin)
      get_html = get_html & "<""" & v.href & """>"
      Case Else:
      Dim indent
      indent = ""

      Select Case v.tagName
      Case "P", "DIV", "TABLE", "TR":
      indent = Space(2)
      get_html = get_html & vbCrLf & margin
      End Select

      get_html = get_html & get_html(v.childNodes, margin & indent)

      If indent <> "" Then get_html = get_html & vbCrLf
      End Select
      Next
      End Function

      若干テキストブラウザっぽくはなりましたがまだまだですねぇ-_-)