Refactoring browser action JavaScript