Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically run custom JavaScript code when loading a webpage?

Now I have a block of JavaScript code to change the display of a webpage, say to highlight some data so that when I read the webpage I can easily find them.

The code is super simple, you can just imagine it as

document.getElementById(index).classList.add("highlight")

index is used to find the content needing to be changed. I will append highlight class to the class attribute of found content so that the display would be changed (just for illustration of the purpose)

Now the problem is, whenever I open the webpage, I need to copy and paste this code to console in Chrome and run it, which is troublesome. So I am wondering whether it is possible that the Chrome will run this script automatically when I load the webpage. Say for example, I have a hyperlink directs to the webpage and when I click the hyperlink, the script will be run and the display of the webpage will be changed.

Tampermonkey is not really helpful in my case. I have another method to generate the JavaScript for different webpages so the script I want to run will change when I load different webpages.

like image 973
Jekyll SONG Avatar asked Jun 30 '26 06:06

Jekyll SONG


1 Answers

There are some tools for this purpose you can use:

  • TamperMonkey

Tampermonkey is the most popular userscript manager, with over 10 million users.

  • RunJS

RunJS is a developer tool that allows users to debug pages by running specified JS across all page loads.

like image 99
Shahryar Saljoughi Avatar answered Jul 01 '26 19:07

Shahryar Saljoughi