5

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.

Shahryar Saljoughi
  • 2,599
  • 22
  • 41
Jekyll SONG
  • 479
  • 3
  • 6
  • 18
  • so you want something that injects javascript into pages for you? – Jaromanda X Feb 23 '18 at 05:40
  • can your "another method" be put inside Tampermonkey too? – T Tse Feb 23 '18 at 05:41
  • I mean, the only other way you could do this is to write your own webextension - but I can't see that happening if you can't do this with tampermonkey – Jaromanda X Feb 23 '18 at 05:42
  • So basically inject a class to the DOM of a foreign web page? You need an extension for this, perhaps your own extension. – Abana Clara Feb 23 '18 at 05:46
  • @JaromandaX yes, but I would like it to to be done when I load the page, without other efforts like clicking some buttons or running another script. Basically I will click a hyperlink to direct to the page. I am thinking about manipulate the hyperlink so that when the hyperlink is clicked, the script will be run and so far no effective progress – Jekyll SONG Feb 23 '18 at 05:48
  • Possible duplicate of [Automatically run JavaScript code upon loading a specific website](https://stackoverflow.com/questions/22707808/automatically-run-javascript-code-upon-loading-a-specific-website) – Shahryar Saljoughi Feb 23 '18 at 05:58
  • tampermonkey scripts get loaded without "clicking some buttons" ... oh, except the first time, where you have to click the button to install tampermonkey ... and click the button to install your script ... but then ... it's like it's not even there! – Jaromanda X Feb 23 '18 at 06:00
  • oh ... wait ... you want to inject this script for anyone clicking you "hyperlink"? – Jaromanda X Feb 23 '18 at 06:02
  • @JaromandaX yes, when user clicks the hyperlink, I want them to be directed to the webpage with important data highlighted already, that is another reason why `Tampermonkey` not very useful in my case – Jekyll SONG Feb 23 '18 at 06:06
  • I haven't seen this kind of feature implemented anywhere, doubt it's even possible to the most of my knowledge. – Abana Clara Feb 23 '18 at 06:19
  • so you want to create a hyperlink on your website that, when clicked, will open some other website, and inject some javascript in it? – Jaromanda X Feb 23 '18 at 07:13
  • @JaromandaX yes, if it's possible, this would also do the work – Jekyll SONG Feb 23 '18 at 07:15
  • No, not possible. That's called "hacking" and is quite illegal - I knew from the start that this was going to be dodgy – Jaromanda X Feb 23 '18 at 07:17
  • @JaromandaX ok, I give up lol. I guess now the best I can do is to paste the code with the link and let users run the code in the console by themselves when the new page is loaded. thx anyway – Jekyll SONG Feb 23 '18 at 07:37
  • or create a web extension they can install – Jaromanda X Feb 23 '18 at 07:40

4 Answers4

3

There are some tools for this purpose you can use:

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

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

Shahryar Saljoughi
  • 2,599
  • 22
  • 41
1

I highly recommend User CSS and Javascript chrome extension. I have been using it for years with no issues whatsoever. In fact, I have customized the vast majority of the websites I use on a daily basis using it!

Diego Fortes
  • 8,830
  • 3
  • 32
  • 42
0

You could actually use a bookmarklet which should simplify the process. https://gist.github.com/caseywatts/c0cec1f89ccdb8b469b1. If you want to fully automate it however you need to create a plugin

Jiby Jose
  • 3,745
  • 4
  • 24
  • 32
-1

yes there is, try following code.

<script>

    window.onload = function(){
        alert("ready");
        // place your code here ...
    }

</script>

or just write your code at the bottom of file, like this

<html>
    <head></head>
    <body>
        <!-- any html tags here -->


        <script>
            // any JS code here
        </script>
    </body>
</html>
Tahir
  • 54
  • 1
  • 7