search: factor out javascript from macro

Less inline scripts.

**nb** it doesn't fix: #1968
console output on purpose?