Refactor to JavaScript script