/    Sign up×
Community /Pin to ProfileBookmark

Underline Bookmarklet

I am developing an underline bookmarklet. It was designed to work in any text box but it only seems to work in Yahoo mail. I have no idea why. I have included it below. Line breaks and comments have been added. In reality it is one line that goes in the address of the bookmark. When it works inputString has a value. When it doesn’t work (which is everywhere but Yahoo mail) inputString is blank.
It is available for testing at [url]http://dmmarks.com/bookmarklets.html[/url]

[CODE]javascript:
/* function to convert HTML entities to text */
function%20get_ents(str)
{var%20temp=document.createElement(‘pre’);
temp.innerHTML=str;
return%20temp.firstChild.nodeValue;};

/* get the selected text */
inputString=document.getSelection().getRangeAt(0); <—– problem seems to be here
inputChar=”;
outputChar=”;

/* convert to string so length will work */
selectedText%20=%20inputString.toString();

/* add the underline charaters */
for(i=0;i<selectedText.length;i++)
{outputChar=outputChar+selectedText.charAt(i).concat(‘%CC%B2’)};

/* replace the selected text */
inputString.deleteContents();
inputString.insertNode(document.createTextNode(get_ents(outputChar)));
void(0);[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@jalarieNov 02.2013 — You need a bit more work to properly get the selected text. I use this subroutine:
<i>
</i>// getselec.js Get selected text
// Written: 2013-05-30 by James Alarie
// http://spruce.flint.umich.edu/~jalarie/
// Title=Pick up highlighted text from the web page.
// Keywords=select,highlight,text.
//
// Functions:
// GetSelected(What)
// What optional The prompt to use if nothing is selected.
// if nothing is selected and "What" is null, a null value is returned.

function GetSelected(What) {
var Value;
if (window.getSelection) {
Value=window.getSelection();
} else {
if (document.getSelection) {
Value=document.getSelection();
} else {
Value=document.selection.createRange().text;
}
}
if (Value == '') {
if (What) {
Value=prompt(What,'');
}
}
return Value+'';
} // GetSelected
×

Success!

Help @denmarks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...