2

I'd like to write a bookmarklet to correct text styling on other people's pages. In particular, I'd like to find any element with "text-align: justify" and change them to "text-align: left". I'm not sure how to find those elements though. Because this is a bookmarklet to be used on arbitrary web pages, I'd rather not rely on any Javascript library (jQuery, etc) to help.

3
  • If you are trying to make content more readable you can always just use readability.com Commented Sep 16, 2012 at 21:24
  • @Mark: thanks, I know about readability.com, and Evernote has a similar service. I wanted to try my own solution. Commented Sep 16, 2012 at 21:24
  • Why no libraries? Is your own solution not going to be a library ? Commented Jun 26, 2013 at 21:56

2 Answers 2

1

This finds all elements with text-align: justify and sets the border to red. It should be plenty to get you started. Note: This is not very efficient, but there's not a good efficient solution to your request. To improve performance, try searching only relevant tags. (Maybe you only need to check Ps and DIVs?)

// Get all elements
var els = document.getElementsByTagName('*');
// Search for style: text-align = justify.
for(var i = 0, el; el = els[i]; i++) {
    if(getComputedStyle(el).textAlign == 'justify') {
        el.style.border = '1px solid #f00';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here:

var walkTheDOM = function walk ( node, func ) {
    if ( node.nodeType !== 1 ) return;
    func( node );
    node = node.firstChild;
    while ( node ) {
        walk( node, func );
        node = node.nextSibling;
    }
};

walkTheDOM( document.body, function ( elem ) {
    if ( getComputedStyle( elem ).textAlign === 'justify' ) {
        elem.style.textAlign = 'left';
    }
});

Live demo: http://jsfiddle.net/W3BUe/1/

5 Comments

Wouldn't document.getElementsByTagName be faster and/or more efficient? Not that it matters, I'm just wondering, since it's a native method
@ianpgall I don't know which one would be more efficient, but I expect both to be quite fast.
@DBJDBJ What matters is that it works in all current browsers. OP didn't specify that he wants to support legacy browsers as well.
@Šime what matters is not to reinvent the whell. This kind of code is much more complex than Your solution presented here. Rock solid solutions where tried and tested many times over elsewhere and are in widespread use everywhere. Pick Your favourite selection library. Leave it to the experts.
@DBJDBJ Shouldn't that comment be for the OP? He's the one who requested a non-library solution. I agree with you here. (Although OP states the code's for a bookmarklet, which means that libraries are out of the question.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.