refactor content.js to remove duplicate code