0

I'm looking for a way to execute my Javascript code across a series of pages to do an automation on mass form submissions. Is there anyway I can hook my JS file in the page after it loads (a chrome extension perhaps)? Currently, I'm manually copy-pasting my JS code in the console. Is it possible to make my script run on a selected page every n seconds?

Kristofu
  • 11
  • 1

1 Answers1

0

The most commonly used methodologies to have a script you write run on specified pages are user scripts and browser extensions. For personal scripts, which you do not intend to distribute and support, that are intended to always run every time you load a page, a user script is probably the best choice, as long as what you want to do can be done within the capabilities available to a user script. User scripts take somewhat less effort to write than browser extensions.

Using a personal browser extension, which you have not chosen to distribute, will have some added annoyances. In Chrome, using unpacked extensions (any extension which you have loaded via the "Load unpacked extension..." button on chrome://extensions/), results in an additional dialog each time Chrome is started. In Firefox, to have your personal extension loaded as anything other than a temporary extension (manually loaded each time you start Firefox), you have to get the extension signed by Mozilla, or hack Firefox so that the check for extension signing is disabled.

User scripts

User scripts are usually a single script file which is executed on specified pages, explicitly to modify the page in some manner. They have less capability than browser extensions. User scripts are JavaScript based scripts that you can install into your browser to perform tasks that you desire (e.g. changing what is displayed on specific sites, adding options to pages, etc.). They are similar to extensions, or add-ons, which you can add to your browser. However, the potential for what they can do is less than an extension. In general, they are focused on changing, or enhancing, what is displayed in web pages.

The code for user scripts is fully and easily viewable and editable through either of the two user script manager extensions mentioned below. There are other user script management extensions. They probably give you a similar level of transparency wrt. being able to view and edit the user scripts you install.

User scripts are usually installed through a browser extension. The most popular are Tampermonkey (Chrome, Firefox, Opera Next, Safari, Microsoft Edge, Dolphin Browser, UC Browser, etc.) and Greasemonkey (Firefox).

Browser extensions

Browser extensions can modify (interact with) web page content using content scripts. They also have significantly more capability to modify the browsing experience by using APIs available only to extensions. In addition, they can make some limited changes to the browser's user interface.

Bookmarklets

If you are wanting code to execute only when you click something, you can also use a bookmarklet. Bookmarklets are small amounts of code which execute in the page when you click on the bookmark.

Continuously executing every N seconds

If you are wanting code to execute every n seconds, you should use setInterval(). Specifically, you could do something like:

var seconds = 5;
function myFunction() {
    //Do something
}
setInterval(myFunction, seconds * 1000);

Portions of this answer were copied from an answer of mine on meta.

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121