Tải bản đầy đủ - 0 (trang)
Hack 63. Embed Content from Any Web Site in a Gadget

Hack 63. Embed Content from Any Web Site in a Gadget

Tải bản đầy đủ - 0trang

Thisway,untilyourscriptfinishes,a"Loading..."messageis

displayed.NowgoovertoGoogleBlogSearchat

http://blogsearch.google.com,andperformasearchthat's

restrictedtomentionsfromthepastweek(orday,ormonth)

whileusingthe"link:"operatortofindlinkstoyoursiteonly.

Forinstance,youcansearchforlink:example.comor

link:blogoscoped.com,asshowninFigure6-21.Betweenthe

words"Results1–10of"and"linkingto"tothetoprightof

GoogleBlogSearchresults,youcanseethenumberoflinksto

theblog.Youcannowfetchthisvalueforthegadgetusinga

JavaScriptblock.Insertthefollowingbelowthelinethatreads"

Loading...

":





main();

functionmain(){

vartoTrack='blogoscoped.com';

varurl='http://blogsearch.google.com/blogsearch?'+

'hl=en&ie=UTF-8&q=link:'+

_esc(toTrack)+'&as_drrb=q&as_qdr=w';

_IG_FetchContent(url,function(response){

varblogCount=getTextBetween(response,"of","


blogCount=_trim(_hesc(blogCount));

echo("Recentbloglinksto"+toTrack+":"+

""+blogCount+"");

});

}



Thisscriptfirstcallsthemain()function.Itthendefinesa

domaintotrack—blogoscoped.com,inthiscase—andconstructs

theGoogleBlogsearchURLneeded.(Insteadofscreenscraping

aGoogledomain,youcanalsoaccessanyotherURL,from

nasa.govtodigg.com,andmore.)Afterwards,

_IG_FetchContent()iscalledwiththatURL.Withinthe

responsetext,youcannowlookfortheHTMLimmediately

surroundingthetargettextyouwanttodisplay.The_hesc

functionescapesthisvalue,convertingitfromHTMLtotext,

and_trimremovesblanksfromtheleftandright.Finally,the

echo()functionoutputstheresultintothegadgetbody.Here

arethegetTextBetween()andecho()functionsthatmakeup

therestofthescriptblock.Ifyouprefer,youcanalsousea

regularexpressionforgetTextBetween[Hack#13].Insert

thesejustbeforethelinethatreads"]]>":

TheresultsofaGoogleBlogsearch



functiongetTextBetween(sAll,sFrom,sTo){

varsPart="";

variFrom=sAll.indexOf(sFrom);

if(iFrom>=1){

variTo=sAll.indexOf(sTo,iFrom);

iFrom+=sFrom.length;

if(iTo>=1){

sPart=sAll.substring(iFrom,iTo);

}

}

returnsPart;

}



functionecho(s){

varelm=_gel("content");

if(elm){

elm.innerHTML=s;

}

}





Youcanalsogowiththefollowingalternative.Insteadofthe

getTextBetweencall,write:

varblogCount=getMatch(response,"of([^<]*)");



NowincludethegetMatchfunction:

functiongetMatch(sAll,sExpr){

varregex=newRegExp(sExpr);

varmatches=regex.exec(sAll);

return(matches&&matches[1])?matches[1]:"";

}



Yourgadgetwillnowdisplaysomethinglike"Recentbloglinks

toexample.com:40,"whereexample.comisthedomainyou

searchedfor.TheiGooglegadgetframeworkwillcachethese

resultsforonehourperURL,soyouwon'tseedatafromthe

lastfewminutes,butthevalueisrecentenoughtoprovideyou

withagooddailyoverviewofactivity.

Notethatifyouwantlesscachingforspeedierresults,youcan

usetherefreshIntervalparameterdefiningacachingtime

frameinseconds,asillustratedinthefollowingline,whichis

setto10minutes:



_IG_FetchContent(url,function(response){

varblogCount=getTextBetween(response,"of",""

blogCount=_trim(_hesc(blogCount));

echo("Recentbloglinksto"+toTrack+":"+

""+blogCount+"");

},{refreshInterval:(60*10)});



Youcanevensetthisparametertoavalueof0todisable

cachingcompletely,butyoushouldavoidusingsuchfrequent

intervals,asitputstoomuchtrafficstressonthetarget

servers.

—PhilippLenssen&BrianJepson



Hack64.AddAnyFlashGameasa

Gadget

NotcontentwithaddinggamesavailableintheofficialGoogle

gadgetsdirectory?Alittletrickallowsyoutoaddalmostany

Flashgameyoucanfind.

Googlegadgetscomeintwomainflavors:thosethatareselfcontained,includingthenecessaryHTMLandJavaScriptwithin

thegadgetXMLitself,andthosethatsimplypointtoanoutside

URLtodisplaycontent.Usingthelatterformat,youcanhave

thegadgetpointtoagamecreatedfortheFlashplug-in,too.

ThenicethingaboutmostFlashapplicationsisthattheyresize

dynamically,perfectlyfittingyourchosengadgetsize.

TolookforFlashgames,enterflashgamesintoaGoogle

search.Youwillstumbleuponmanydirectories,mostofthem

presentingloudandwildlyanimatedgames.Onceyoudiscover

theFlashgameyouwanttoadd,opentheHTMLsourceofthe

pageit'sembeddedonandsearchforoccurrencesofthe

extension".swf".Forsamplepurposes,Ipickedasimple

mouse-basedshootinggame,perfecttobalanceyourstress

levelduringlunch,fromasitecalledFlashGames247.com.

OpeningtheHTMLpage,IcanseethatthesourceURLis

http://flashgames247.com/13770/games/blastem/blastem.swf.

WiththatURLinhand,it'snowtimetocreateyourgadget.

OpentheGoogleGadgetEditor[Hack#60]oranyothereditor

ofchoiceandstartwiththe"hello.xml"sampleframework.

Noticethatthemainpartofthegadgetcurrentlyreadsthe

following:

...




Hello,world!

]]>

...



CreatingagadgetthatpointstoanotherURL



Thisformatisforself-containedgadgets,andasyoucansee,it

comescompletewithsomeHTML.Butyouwanttoswitchthis

typeofgadgettoonethatpointstoanexternalwebsite,so

youneedtoamendtheexamplecodetoreadasfollows,as

showninFigure6-22:



...




href="http://flashgames247.com/13770/games/blastem/blastem.swf"

...



Notethatyouarecompletelyremovingthe
alsodroppingtheclosingtag.Alsonotethata

heightattributewasaddedtotheModulePrefselementand

setto450pixelstoallowmoredisplayspaceforthegame.

Saveyourgadget,copytheURLoftheXMLfile,andadditto

iGoogle.Youcancreateanewtabdedicatedtojustthegame,

andeditthetabpropertiestoallowyourgadgettousethefull



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 63. Embed Content from Any Web Site in a Gadget

Tải bản đầy đủ ngay(0 tr)

×