看到googledesktop2的插件下载页面有一个字典插件。是从dict.com网站上取得单词的解释。便自然的想到我们的金山词霸也有web版的(http://push.cb.kingsoft.com/index.htm)。于是想也做一个玩玩。
随便输入一个单词“god”,看见url变成了“http://cb.kingsoft.com/search?s=god&t=word&lang=utf-8”。我就觉得实现估计不难。
返回的html内容用正则表达式解析是最合适不过了。
delphi的正则表达式类从这里下载:http://regexpstudio.com/TRegExpr/TRegExpr.html
其他还用到Indy组件的IdHttp.估计用indy9也是没什么差别的。
如果有兴趣把这个idea做到googleDesktop2里当插件的兄弟,请跟贴。俺怕俺自己是做不出来,所以到这里抛砖引玉。开发环境:delphi7+win2k+indy10+TRegexpr
2个主要的源文件如下:(unit1.pas和unit.dfm)
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls,
regexpr,
Clipbrd, ExtCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label3: TLabel;
Memo2: TMemo;
Button1: TButton;
IdHTTP1: TIdHTTP;
Button2: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FCurrentWord : string;
{ Private declarations }
procedure ExtractItems (const AText : string; VItems : TStrings);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ExtractItems (const AText : string; VItems : TStrings);
begin //<td class="explain_item">(\w*)</td>
with TRegExpr.Create do
begin
try
Expression := 'explain_attr\">(.{0,10})</td>.{1,30}explain_item\">(.{0,30})<\/td>';
if Exec (AText) then
begin
REPEAT
VItems.Add (Match[1] + ' ' + Match[2])
UNTIL not ExecNext;
end;
finally
Free;
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
lWord,
lRequest,
lResponse,
S : string;
begin
//http://cb.kingsoft.com/search?s=beggar&t=word&lang=utf-8
lWord := Edit1.Text;// lRequest := 'http://cb.kingsoft.com/search?s=' + lWord + '&t=word&lang=utf-8';
lRequest := 'http://cb.kingsoft.com/search?s=' + lWord + '&t=word&lang=GB2312';
lResponse := IdHTTP1.Get(lRequest); S := UTF8Decode(lResponse); //<td class="explain_item">
//ExecRegExpr()
Memo2.Lines.Add('['+lWord+']');
ExtractItems(S,Memo2.Lines);end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = vk_return then
Button1Click(nil);
end;procedure TForm1.Button2Click(Sender: TObject);
var
S : string;
begin with TClipboard.Create do
begin
S := AsText;
if S <> FCurrentWord then
begin
if pos(' ',S) <1 then
begin
Edit1.Text := S;
Button1Click(nil);
end; FCurrentWord := S;
end; Free;
end;end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
Button2Click(nil);
end;end.
//////////////////////////////////////////////////
object Form1: TForm1
Left = 192
Top = 107
Width = 294
Height = 353
Caption = 'mini金山词霸 Online'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 12
Top = 24
Width = 23
Height = 13
Caption = 'word'
end
object Label3: TLabel
Left = 8
Top = 84
Width = 30
Height = 13
Caption = 'Result'
end
object Edit1: TEdit
Left = 56
Top = 24
Width = 153
Height = 21
ImeName = '紫光拼音输入法'
TabOrder = 0
Text = 'God'
OnKeyDown = Edit1KeyDown
end
object Memo2: TMemo
Left = 52
Top = 76
Width = 185
Height = 213
ImeName = '紫光拼音输入法'
ReadOnly = True
TabOrder = 1
end
object Button1: TButton
Left = 224
Top = 20
Width = 43
Height = 25
Caption = 'Search'
TabOrder = 2
OnClick = Button1Click
end
object Button2: TButton
Left = 108
Top = 292
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 3
Visible = False
OnClick = Button2Click
end
object IdHTTP1: TIdHTTP
AuthRetries = 0
AuthProxyRetries = 0
AllowCookies = True
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentLength = -1
Request.ContentRangeEnd = 0
Request.ContentRangeStart = 0
Request.ContentRangeInstanceLength = 0
Request.Accept = 'text/html, */*'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
HTTPOptions = [hoForceEncodeParams]
Left = 172
Top = 48
end
object Timer1: TTimer
OnTimer = Timer1Timer
Left = 140
Top = 48
end
end
随便输入一个单词“god”,看见url变成了“http://cb.kingsoft.com/search?s=god&t=word&lang=utf-8”。我就觉得实现估计不难。
返回的html内容用正则表达式解析是最合适不过了。
delphi的正则表达式类从这里下载:http://regexpstudio.com/TRegExpr/TRegExpr.html
其他还用到Indy组件的IdHttp.估计用indy9也是没什么差别的。
如果有兴趣把这个idea做到googleDesktop2里当插件的兄弟,请跟贴。俺怕俺自己是做不出来,所以到这里抛砖引玉。开发环境:delphi7+win2k+indy10+TRegexpr
2个主要的源文件如下:(unit1.pas和unit.dfm)
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls,
regexpr,
Clipbrd, ExtCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label3: TLabel;
Memo2: TMemo;
Button1: TButton;
IdHTTP1: TIdHTTP;
Button2: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FCurrentWord : string;
{ Private declarations }
procedure ExtractItems (const AText : string; VItems : TStrings);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ExtractItems (const AText : string; VItems : TStrings);
begin //<td class="explain_item">(\w*)</td>
with TRegExpr.Create do
begin
try
Expression := 'explain_attr\">(.{0,10})</td>.{1,30}explain_item\">(.{0,30})<\/td>';
if Exec (AText) then
begin
REPEAT
VItems.Add (Match[1] + ' ' + Match[2])
UNTIL not ExecNext;
end;
finally
Free;
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
lWord,
lRequest,
lResponse,
S : string;
begin
//http://cb.kingsoft.com/search?s=beggar&t=word&lang=utf-8
lWord := Edit1.Text;// lRequest := 'http://cb.kingsoft.com/search?s=' + lWord + '&t=word&lang=utf-8';
lRequest := 'http://cb.kingsoft.com/search?s=' + lWord + '&t=word&lang=GB2312';
lResponse := IdHTTP1.Get(lRequest); S := UTF8Decode(lResponse); //<td class="explain_item">
//ExecRegExpr()
Memo2.Lines.Add('['+lWord+']');
ExtractItems(S,Memo2.Lines);end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = vk_return then
Button1Click(nil);
end;procedure TForm1.Button2Click(Sender: TObject);
var
S : string;
begin with TClipboard.Create do
begin
S := AsText;
if S <> FCurrentWord then
begin
if pos(' ',S) <1 then
begin
Edit1.Text := S;
Button1Click(nil);
end; FCurrentWord := S;
end; Free;
end;end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
Button2Click(nil);
end;end.
//////////////////////////////////////////////////
object Form1: TForm1
Left = 192
Top = 107
Width = 294
Height = 353
Caption = 'mini金山词霸 Online'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 12
Top = 24
Width = 23
Height = 13
Caption = 'word'
end
object Label3: TLabel
Left = 8
Top = 84
Width = 30
Height = 13
Caption = 'Result'
end
object Edit1: TEdit
Left = 56
Top = 24
Width = 153
Height = 21
ImeName = '紫光拼音输入法'
TabOrder = 0
Text = 'God'
OnKeyDown = Edit1KeyDown
end
object Memo2: TMemo
Left = 52
Top = 76
Width = 185
Height = 213
ImeName = '紫光拼音输入法'
ReadOnly = True
TabOrder = 1
end
object Button1: TButton
Left = 224
Top = 20
Width = 43
Height = 25
Caption = 'Search'
TabOrder = 2
OnClick = Button1Click
end
object Button2: TButton
Left = 108
Top = 292
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 3
Visible = False
OnClick = Button2Click
end
object IdHTTP1: TIdHTTP
AuthRetries = 0
AuthProxyRetries = 0
AllowCookies = True
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentLength = -1
Request.ContentRangeEnd = 0
Request.ContentRangeStart = 0
Request.ContentRangeInstanceLength = 0
Request.Accept = 'text/html, */*'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
HTTPOptions = [hoForceEncodeParams]
Left = 172
Top = 48
end
object Timer1: TTimer
OnTimer = Timer1Timer
Left = 140
Top = 48
end
end
解决方案 »
- DataAccess / TClientDataSet 这个控件有什么作用
- 简单的登陆比较问题
- 急急请教了,用Delphi的ActiveX插件里好象无法调用js里的function 或属性
- 想设计一个小的数字键盘?
- 如何使TEdit从右向左显示?
- 如何在免费邮箱中建新文件夹?并对其操作?
- 怎样才能用messagebox函数输出变量信息
- 我一直用bde,可发觉现在大家都改用ado了。哪位能说说ado的优点吗?
- 怎样改写事件?(不知道是否overload、override等等)
- Dim doc As Object 翻译成DELPHI的语句是什么?
- Twincontrol后代控件的Parent赋值问题
- 我这样写有什么问题码?(Treeview)
把原来的GDictionary加了2-3句代码就OK了。
要怪只能怪原作者的代码的可读性写得太好了。
可惜是c#代码,似乎不适合在这里探讨。不过只要有时间,俺还是想用delphi做一个googledesktop的插件,而不是c#.有兴趣的同志可跟我要一个。毕竟是一个不要钱的金山词霸,呵呵。
[email protected]