[HELP] Enhance Evernote editor by using WinClip

Get help with using AutoHotkey and its commands and hotkeys
pk23
Posts: 107
Joined: 24 Apr 2015, 00:49

[HELP] Enhance Evernote editor by using WinClip

25 Apr 2015, 09:16

For adding more hotkeys with Evernote's editor, I'm using WinClip. This is effect:

Image

This is core script:

Code: Select all

	evernoteEdit(eFoward, eEnd)
	{
		clipboard =
		Send ^c
		ClipWait, ,
		t := WinClip.GetHTML()			;get html, for backup original format
		RegExMatch(t, "s)(?<=StartFragment-->)(.*?)(?=<!--EndFragment)", t)		;remove needless part, to get raw html only
		html = %eFoward%%t%%eEnd%
		WinClip.Clear()
		WinClip.SetHTML(html)
		Sleep, 300
		Send ^v
		Return
	}
	#1::evernoteEdit("<div style='color: #F02E37;'><b>", "</b></div>")
	!f::evernoteEdit("<div style='margin-top: 5px; margin-bottom: 9px; word-wrap: break-word; padding: 8.5px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.148438)'>", "</div>")
	!1::evernoteEdit("<div style='background: #FFFAA5;'>", "</div>")
But bugs exist:

Image

For bug 2, after backup raw html, it's working, but at same time introducing some gibberish, like below. By debug, guess maybe the coding issue, UTF-8/ANSI, but don't know how to fix it.

====================================
UPDATE: BUG 2 solved, thanks to tmplinshi below, by using new WinClip func with UTF-8 and GetHtml_DOM func.

But BUG 1 still need help, I think its reason is about inline style attribute has the highest priority. so If the backup html is

Code: Select all

<SPAN style="COLOR: rgb(0,0,0)">dingdongdingdong</SPAN>
then the func returned edited html below, #0F820F color will not working, even use !important attr, which I already tried:

Code: Select all

<div style='color: #0F820F;'>
  <SPAN style="COLOR: rgb(0,0,0)">dingdongdingdong</SPAN>
</div>
Have any idea?
Last edited by pk23 on 25 Apr 2015, 19:31, edited 4 times in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: [HELP] Enhance Evernote editor by using WinClip

25 Apr 2015, 13:42

WinClip.ahk 中的 GetHtml() 函数如下:

Code: Select all

GetHtml()
  {
    if !( clipSize := this._fromclipboard( clipData ) )
      return ""
    if !( out_size := this._getFormatData( out_data, clipData, clipSize, "HTML Format" ) )
      return ""
    return strget( &out_data, out_size, "CP0" )
  }
需要把 CP0 修改成 UTF-8 才能支持中文。你可以直接修改 WinClip.ahk 文件,或者这样:

Code: Select all

#Include, <WinClip>
#Include, <WinClipAPI>

WinClip.GetHtml2 := Func("GetHtml2") ; 也可以直接覆盖原来的函数 -> WinClip.GetHtml := Func("GetHtml2")
WinClip.GetHtml3 := Func("GetHtml_DOM")

GetHtml_DOM(this, Encoding := "UTF-8") {
	html := this.GetHtml2(Encoding)

	static doc := ComObjCreate("htmlFile")
	doc.Write(html), doc.Close()
	return doc.all.tags("span")[0].InnerHtml
}

GetHtml2(this, Encoding := "UTF-8")
{
  if !( clipSize := this._fromclipboard( clipData ) )
    return ""
  if !( out_size := this._getFormatData( out_data, clipData, clipSize, "HTML Format" ) )
    return ""
  return strget( &out_data, out_size, Encoding )
}

evernoteEdit(eFoward, eEnd)
{
    ;BlockInput On
    clipboard =
    Send ^c
    ClipWait, ,
    MsgBox, % WinClip.GetHtml2()
    MsgBox, % WinClip.GetHtml3()
    Return
    t := WinClip.GetHtml()
    html = %eFoward%%t%%eEnd%       ;add raw html format
    WinClip.Clear()
    WinClip.SetHTML(html)
    Sleep, 300
    Send ^v
    Return
}
#1::evernoteEdit("<span style='color: #F02E37;'><b>", "</b></span>")
至于如何修改字体样式,就完全是 html 知识了,我也不熟悉。
pk23
Posts: 107
Joined: 24 Apr 2015, 00:49

Re: [HELP] Enhance Evernote editor by using WinClip

25 Apr 2015, 18:04

tmplinshi wrote:WinClip.ahk 中的 GetHtml() 函数如下:
谢谢!果然专业,DOM比用正则好太多了。 :thumbup:
真希望有程序员,能把evernote的编辑器增强下,肯定做的比我既好又轻松。。。样式问题希望也能解决,不过乱码搞定,已经帮了大忙了。。
Soscan2062
Posts: 2
Joined: 02 Jul 2015, 14:06
GitHub: Soscan2062

Re: [HELP] Enhance Evernote editor by using WinClip

06 Jul 2015, 10:21

I see you're roughly doing the same thing I set out to do. I made a breakthrough with WinClip that I believe can help you. While Evernote is similar to HTML it is not HTML. It is Evernote Markup Language; it's own proprietary format. As you can see here (Evernote Markup Language) it is very similar. I found using ClipSpy that the text format in the clipboard is titled "ENML Format". With this I was able to make some changes in WinClip to account for this new format. Basically all I did was take all the HTML specific functions and replace HTML with ENML. Here is the code I added to the WinClip.ahk file.

Code: Select all

SetENML( enml, source = "" )
  {
    if ( enml = "" )
      return 0
    clipSize :=  this._fromclipboard( clipData )
    if !( clipSize := this._setENML( clipData, clipSize, enml, source ) )
      return 0
    return this._toclipboard( clipData, clipSize )
  }
 
   iSetENML( enml, source = "" )
  {
    if ( enml = "" )
      return 0
    this._IsInstance( A_ThisFunc )
    clipSize := this._getClipData( clipData )
    if !( clipSize := this._setENML( clipData, clipSize, enml, source ) )
      return 0
    return this._setClipData( clipData, clipSize )
  }

  _calcENMLLen( num )
  {
    while ( StrLen( num ) < 10 )
      num := "0" . num
    return num
  }
  _setENML( ByRef clipData, clipSize, enmlData, source )
  {
    objFormats := this._parseClipboardData( clipData, clipSize )
    uFmt := WinClipAPI.RegisterClipboardFormat( "ENML Format" )
    objFormats[ uFmt ] := object()
    encoding := "UTF-8"
    enmlLen := StrPut( enmlData, encoding ) - 1   ;substract null
    srcLen := 2 + 10 + StrPut( source, encoding ) - 1      ;substract null
    StartENML := this._calcENMLLen( 105 + srcLen )
    EndENML := this._calcENMLLen( StartENML + enmlLen + 76 )
    StartFragment := this._calcENMLLen( StartENML + 38 )
    EndFragment := this._calcENMLLen( StartFragment + enmlLen )
    enml =
    ( Join`r`n
<en-clipboard><en-note><span style="color: rgb(0, 0, 0); font-family: Tahoma; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; display: inline !important; float: none;">
%enmlData%
</span></en-note></en-clipboard>.
    )
    sLen := StrPut( enml, encoding )
    ObjSetCapacity( objFormats[ uFmt ], "buffer", sLen )
    StrPut( enml, ObjGetAddress( objFormats[ uFmt ], "buffer" ), sLen, encoding )
    objFormats[ uFmt ].size := sLen
    return this._compileClipData( clipData, objFormats )
  }
GetENML()
  {
    if !( clipSize := this._fromclipboard( clipData ) )
      return ""
    if !( out_size := this._getFormatData( out_data, clipData, clipSize, "ENML Format" ) )
      return ""
    return strget( &out_data, out_size, "UTF-8" ) ;Previously UTF-8 was CP0, changed to account for unicode characters
  }
 
  iGetENML()
  {
    this._IsInstance( A_ThisFunc )
    if !( clipSize := this._getClipData( clipData ) )
      return ""
    if !( out_size := this._getFormatData( out_data, clipData, clipSize, "ENML Format" ) )
      return ""
    return strget( &out_data, out_size, "UTF-8" ) ;Previously UTF-8 was CP0, changed to account for unicode characters
  }
Use the WinClip.SetText after WinClip.getENML to see exactly how Evernote is handling the formats. I tested it myself and all the formatting was there in copied text.
Per your Bug Fix 2 I managed to change the format to UTF-8 and the characters (that I copied from your comments) encoded just fine back and forth in WinClip. I noted in the code above my format changes.

Return to “Ask For Help”

Who is online

Users browsing this forum: arbibarbarona, holotaro, JAMESARLOS, miracle and 67 guests