Tải bản đầy đủ - 0 (trang)
Hack 28. Add a Yahoo! Bookmark with One Click

Hack 28. Add a Yahoo! Bookmark with One Click

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

AbookmarkletisabitofJavaScriptcodestoredinaweb

browserbookmark.Bookmarkletsgiveyouawaytoruncode

thatcaninteractwiththecurrentpageinthebrowser.For

example,bookmarkletscanchangethesizeandcolorsoffonts

onapage,opennewbrowserwindows,orextractinformation

aboutthecurrentpage.Withbookmarklets,you'reincontrolof

thescript,becauseitrunswhenyouclickthebookmark.

Inordertoimplementthishack,theonlythingyou'llneedisa

browserthathasbookmarksandunderstandsJavaScript.Don't

worry,thatcoversjustabouteverywebbrowser!



2.7.2.TheCode

Here'salookatsomenicelyformattedJavaScriptthatgetsthe

title,URL,andselectedtextfromthecurrentwebpageatthe

timethebookmarkletisclicked.ItthenbuildstheproperURL

foraddingaYahoo!Bookmarkandopensitinanewbrowser

window.Keepinmindthatthiscodeisnicelyformattedsoyou

canseehowitoperates;thefunctioningbookmarkletcodewill

beformattedwithoutlinebreaksorspaces.











//DissectedJavaScriptbookmarkletforone-clickYahoo









//Setttothecurrentlyselectedtext,ifavailable

vart=d.selection?d.selection.createRange().text:d.g

















//BuildtheURLthatwilladdabookmarktoYahoo!Boo

varurl='http://e.my.yahoo.com/config/edit_bookmark?'

url+='.src=bookmarks&';

url+='.done=http%253a%2F%2Fe.my.yahoo.com/config/set_

url+='.folder=1&';

url+='.action=ab&';



//Setdtothedocumentobjectasashortcut

vard=document;









url+='.display=1&';

url+='.protocol=http%3A%2F%2F&';









//includetheURLofthecurrentpage

url+='.url='+escape(d.location.href)+'&';









//includethetitleofthecurrentpage

url+='.name='+escape(d.title)+'&';











//includeanyselectedtextofthecurrentpageasac

url+='.comment='+escape(t)+'&';

url+='.save=+Save+';













//openanewwindowtoaddthebookmarkandshowther

window.open(url,







'_blank',







'width=640,height=440,status=ye



Takealookattheboldquerystringvariablesinthecode.These

aretheprimaryelementsoftheYahoo!URLwe'reconcerned

with.Here'saquicklookatwhateachvariablerepresents:



.done

TheURLtodisplayaftertheactioniscompleted.



.folder

TheIDofthefolderinwhichyou'dlikethebookmarktobe

included.Ifyoudon'thavemultiplefolders,use1,whichis

thedefault.



.url

TheURLofthesiteyou'readdingasabookmark.



.name

Thenameofthesiteyou'readdingasabookmark.



.comment

Somearbitrarytextthatisassociatedwiththebookmark.

Notealsothatvaluesforthesequerystringvariableshavebeen

escapedforuseinaURLeitherbyhand,asinthecaseof.done,

orwiththeJavaScriptescape()function.Thisensuresthatany

charactersthatareillegalinURLshavebeenconvertedtotheir

hexadecimalequivalent.

Unfortunately,abookmarkletisnoplaceforreadablecodewith

commentsandlinebreaks.Instead,thecodeneedstobe

smashedintoitsmostcompactform.Here'salookatthecode

reformattedforuseinabookmarklet:





javascript:d=document;t=d.selection?d.selection.createR







getSelection();void(window.open('http://e.my.yahoo.com







bookmark?.src=bookmarks&&.done=http%253a%2F%2Fe.my.yaho







bookmark&.folder=1&.action=abamp;.display=1&.protocol=h







url='+escape(d.location.href)+'&.name='+escape(d.title)







'+escape(t)+'&.save=+Save+','blank','width=640,height=4







resizable=yes,scrollbars=yes'))



Asyoucansee,itlookssimilartotheprecedingcode,butwith

someimportantchanges.Thejavascript:atthebeginningtells

thebrowsertoexecutewhatfollowsasabookmarkletrather

thanastandardbookmarkwithaURL.Also,thevoid()operator

oftencomesinhandyinbookmarklets,becauseitstopsthe

expressionitsurroundsfromreturningavalue.Inthiscase,we

don'treallycarewhatvalueisreturnedwhenthewindow

opens;wejustwantthewindowtoopen,andvoid()doesthe

trick.



2.7.3.RunningtheHack

Theinstallationprocessforthebookmarkletwillbeuniqueto

thebrowseryou'dliketouseitwith.Ifyouknowhowtocreate

andeditabookmark,youknowhowtoinstallabookmarklet.

Simplycreateanewbookmarkandaddthecodeinplaceofa

URL.Somebrowserswillwarnyouthatjavascript:isnotavalid

protocol,butyoucanignorethatmessage.You'llalsowantto

giveyourbookmarkletasnappy,shortname,suchasAdd

Y!Bookmark,asshowninthelinksbarinFigure2-16.

Oncethebookmarkisinplace,browsetoanypage,highlight

sometextonthepage,andclickaway!Onceyouclick,anew

windowwillletyouknowthebookmarkhasbeenaddedtoyour

list,asshowninFigure2-17.

Fromhere,youcaneditthenewlyaddedbookmarkbyclicking



onthepencilicon.Simplyclosethewindowwhenyou're

finished.



Yahoo!removesanyapostrophesfromBookmarkComments,soyou'll

havetoputupwithmisspellingshereandthere,especiallyifyou're

addingtextthatwashighlightedonawebpage.



Figure2-16.TheAddY!Bookmarkbookmarkletin

thebrowsertoolbar



Figure2-17.ThenewbookmarkaddedtoYahoo!

Bookmarks



ThebestpartofkeepingyourbookmarksatYahoo!isthatthe

nexttimepeoplearegatheredaroundsomeoneelse'slaptop

tryingtorememberthelocationofsomecoolsiteyouvisited

lastweek,youcansavethedaybysaying,"Ihavethatsite

bookmarkedatYahoo!"



Hack29.ImportExistingBookmarksintoYahoo!

Bookmarks



ToconsolidateorbuildaBookmarklibraryquickly,you

canimportexistingbookmarksfromapplicationssuchas

Firefox,InternetExplorer,orwebapplicationsthat

supportOPML.

Yahoo!letsyoustoreupto1,000bookmarksontheirserver,

makingthemeasilyaccessiblefromanycomputer.But

transferringorcreating1,000bookmarksbyhandcouldtake

youafewmonthsofconcentratedeffortandsomevacation

timefromwork.Evenwithone-clickadding[Hack#28],

you'relookingatafewhoursofsolidclickingtoevenapproach

thelimit.

Luckily,youcantakeadvantageofthebookmarksyoualready

havebyimportingthem.Yahoo!providessometoolstomake

theprocessofimportingbookmarkstakeafewminutesrather

thanafewmonths.Also,ifyouuseotherbookmark-like

applicationsontheWeb(suchasBlogrolling,Bloglines,or

Kinja)youcanprobablyfindawaytoimportthemintoYahoo!

Bookmarksbyusingexportingfeaturesthoseapplications

provide.

EachofthesehacksreliesontheImportBookmarksfeature

thatyou'llfindatyourYahoo!Bookmarks(at

http://bookmarks.yahoo.com,towardthetopofthepage).



2.8.1.InternetExplorerBookmarks



ImportingyourInternetExplorerbookmarksisafairlypainless

process,thankstoanActiveXControlYahoo!hasputtogether

that'sappropriatelycalledYahoo!FavoritesImporter.Togetto

theimportpage,clickImportBookmarksfromtheYahoo!

BookmarkspageandchoosetheInternetExplorerforWindows

link.

AnActiveXControlisabitofcodethatrunsinyourbrowser

andthatcaninteractwithyourdesktopapplications.Asyou

mightguess,therearesomesecurityconcernswiththistypeof

code,soyoumightseeawarninginyourbrowserliketheone

showninFigure2-18.

Goaheadandclickontheyellow-highlightedarea,andthen

chooseInstallActiveXControl….Onceit'sinstalled,clickthe

ImportFavoritesbutton,andallofyourfoldersandbookmarks

willbeaddedtoyourYahoo!Bookmarks.



Figure2-18.ImportBookmarksActiveXwarning



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

Hack 28. Add a Yahoo! Bookmark with One Click

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

×