MindRoller
MindRoller

Reputation: 241

Constantly execute javascript code on some webpage

What I would like to achieve is this: I load some webpage and here are some clickable contents like buttons, clickable divs etc. What I want to achieve is: I want to constantly run javascript code that will click some buttons then after that wait for 15 seconds, refresh the page and repeat. (Yeah, some kind of a bot). I know how to achieve clicks etc. I will manage with the code but how to achieve such a functionality? I know that there was some extension for Chrome to do such things, but I can't remember now. Would be grateful for some tips.

I tried creating my own html file and dynamically loading whole html of the page I want the bot to work on into div of my html file. Something like this.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Load remote content into object element</title>
  </head>
  <body>
    <!--<div id="siteloader" style="width: 1000px; height: 1000px;"></div>​-->
    <div id="siteloader"></div>
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script>
        $("#siteloader")
            .html('<object data="http://linktowebpage.com"/>');
    </script>
  </body>
</html>

The problem is: it creates an iframe and it is so small and I can not make it any bigger (it's like 150x150 px).

As I say I would like to do it without doing such stuff (creating my own page etc.).

Would be grateful for any tips ;)

Upvotes: 0

Views: 593

Answers (2)

Pugazh
Pugazh

Reputation: 9561

Use setInterval() to do the function repeatedly and ajax() to load the page.

Check below example to get started.

var i = 1;
setInterval(function() {
  $.get("http://linktowebpage.com", function(data) {
    $('#siteloader').html(data);
  });
}, 60000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>Specified URL will be loaded every 1 minutes</div>
<br>
<div id="siteloader"></div>

Upvotes: 0

The Witness
The Witness

Reputation: 920

As far as I understand, you want to run your JS on some third party page. With <iframe> it won’t work because you could read page contents, at best.

You could write a code that does everything and run it in console. But, for instance, if you wanted to use jQuery, a page would have to have it. So the safest option in this case would be to have it written in pure JavaScript. The downside and disqualifying issue, as I guess, is you have to run it everytime after refresh.

In your case you could use a tool like Tampermonkey (Chrome) or Greasemonkey (Firefox) which allows you to run your JS per domain.

Upvotes: 1

Related Questions