Slashdot is powered by your submissions, so send in your scoop


Forgot your password?
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 Internet speed test! ×

Comment Re:User scripts FTW (Score 1) 6

My point was just those features make quickie editing even quicker - your Amazon script is

var el = document.querySelector('#summaryStars, .totalReviewCount'), total = parseInt(el && el.textContent)

if (total) {
    Array.from(document.querySelectorAll('#histogramTable td:last-child a')), a => {
            var num = parseInt(a.textContent), pct = 100 * (num / total)
            a.innerHTML = `${num} - <b>${pct.toFixed(0)}%</b>`

and about two dozen of my scripts are variations on this :) Put the basic framework in the new script template and you just need to fill in a selector and what to do with each element...

There are a lot of annoying websites out there lol, hope this comes in handy next time one just needs that little fix... ;)

Comment User scripts FTW (Score 1) 6

I write a lot of user scripts and couldn't live without being able to tweak websites to add features, or more often, fix their problems - I just checked Tampermonkey and I've currently got 40 user scripts for various sites :)

There are a lot faster ways to write them now using features from HTML5 and now ES6 which weren't there even a year ago - for instance, your password script can be rewritten in one line

Array.from(document.querySelectorAll('input[type=password][autocomplete=off]')).forEach(elem => { elem.autocomplete = 'on' })

using arrow functions, Array.from and querySelectorAll .

If you use Chrome this currently only works in the development version, so for the next few months until that's released you can write

[]'input[type=password][autocomplete=off]')).forEach(function(elem) { elem.autocomplete = 'on' })


If instead you want to insert HTML, using template strings and insertAdjacentHTML again make life easier, e.g.

Array.from(document.querySelectorAll('a[href$=".jpg"]')).forEach(a => { a.insertAdjacentHTML('afterbegin', `<img src="${a.href}" style="width: 32px; height: 32px; margin-right: 0.5em">`) })

matches links with targets ending in .jpg, then prepends a 32x32 pixel sized copy of the linked image to each!

About 90% of user scripts are just

find things -> loop over them -> adjust properties and/or edit HTML

so it's handy having simpler ways of writing it :) Hope that was helpful, I keep meaning to write an article on advanced techniques for writing user scripts and not doing it...

Comment Re:Write-only code. (Score 1) 757

When first announced here Rust looked very interesting, with some bold ideas for making programs better in the lower-level problem domain. Since then it's been under tons of development and community vigour, version 1 looked very different to 0.0.1, I need to reinvestigate!

The final switch seems like it would be a useful construct. Python doesn't really have good Enums unfortunately, just some approximations such as using namedtuple._make(). Whereas in CoffeeScript you can assign the result of an if-else block to a variable, which allows for silly conditionals always a potential :)

Comment Re:Write-only code. (Score 1) 757

Well yes, but if we were Perl programmers, we'd have just both been telling jokes at each other while trying to work out what was supposed to be funny about the other's joke.

If we were PHP programmers we'd have got the punchline and the setup in the wrong order, and be getting confused over all of the similar sounding jokes that aren't funny, but never seem to go away.

If we were JavaScript programmers we'd be on StackOverflow, asking for the best joke ever and a word-by-word explanation of why it was funny.

I really will stop now.

Comment Re:Write-only code. (Score 1) 757

My view is that Python is an imperative language that is moving towards functional programming - much like both Ruby and JavaScript it's never going to get there, but it's a definite shift from when I started coding it at version 1.5, and IMO it's much cleaner to consider code as a series of transforms of varying kinds than a grab bag of tools.

I suspect switch lost out due to the C-style switch-with-fallthrough and "clever" shenanigans like Duff's Device that can make them anything but obvious what's going on, and back in 1991 avoiding that probably looked like a good idea for an easy to learn language. I once worked on an application that's core consisted of a 3,000 line switch statement for handling messages, with both fall-through all over the place, and parts of it callled back into itself in what may have been an attempt at code re-use, but was probably just as stupid as it looked... *shudder*

I like Coffeescript's switch, but that's partly because I like having every construct being an expression, removing an element of the code/data divide.

Comment Re:Write-only code. (Score 1) 757

... Python list comprehensions are not perfect and are useful only for the most common cases.

I'm sure that Guido weeps himself to sleep every night knowing that this feature is only useful for 90% of use cases and not 100% ;)

Sure they're not perfect, but as you say they work for most cases and when they don't they're easy to replace with your customised solution - perfect is the enemy of better, and list comprehensions definitely make Python "better" :)

Comment Re:Write-only code. (Score 1) 757

True, but you can generally get by with learning one way while learning, then expand options later. For a lot of Python IMO the complexity is very well-hidden from a beginner, and little things like the help() and dir() functions make life easier.

Couldn't live without IPython now though, I've been totally spoilt by auto-call, ? and ??, the history tools, the ed command, etc etc....

Slashdot Top Deals

"Open the pod bay doors, HAL." -- Dave Bowman, 2001