DEV Community

Enoch
Enoch

Posted on

How to Write an Automatic Cashback Redirect Script in JavaScript

The way a cashback script works is simple: when you visit a shopping website, it automatically routes your session through a cashback platform, allowing you to earn a percentage of your purchase as cashback. With a simple userscript, you can automate this process on any supported site.

What is a Userscript?
A userscript is a small JavaScript program that runs in your browser, typically via extensions like Tampermonkey or Greasemonkey. It executes on specified pages after they load, and can redirect links, inject notification banners, or modify page behavior—all on the client side.

Basic Idea
When a user visits a shopping site, the script checks whether a cashback session has already been activated. If not, it redirects the user to a cashback platform (such as JTMate), where the purchase is tracked and rewards are credited back to the user.

Full Script
Paste the following code into a new script in Tampermonkey:

// ==UserScript==
// name: Cashback Auto Redirect
// version: 1.0
// description: Earn cashback automatically via JTMate
// match: ://.amazon.com/*
// match: ://.ebay.com/*
// ==/UserScript==

(function () {
'use strict';

const CASHBACK_PORTAL = 'https://jtmate.com/?ref=auto&url=';
const SESSION_KEY = 'cashback_activated';

// Prevent redirect loop
if (sessionStorage.getItem(SESSION_KEY)) return;

sessionStorage.setItem(SESSION_KEY, 'true');

// Redirect via cashback platform
const currentUrl = encodeURIComponent(window.location.href);
window.location.replace(CASHBACK_PORTAL + currentUrl);
})();
Note: In actual use, each metadata line must start with // and include the @ symbol before field names (e.g., @name, @match). This is the standard format required for Tampermonkey to recognize script metadata.

Core Logic Explanation

  1. Match Target Websites
    Use the match field in the script header to specify which shopping sites the script should run on. Wildcards are supported, and you can add as many platforms as needed.

  2. Prevent Redirect Loops
    Use sessionStorage to store a flag. Each time the script runs, it checks this flag first. If it exists, the script exits immediately to avoid infinite redirects.

  3. Redirect to Cashback Platform
    Encode the current page URL and append it to the cashback platform link, for example:

https://jtmate.com/?ref=auto&url=https%3A%2F%2Fwww.amazon.com%2F...
After receiving the request, JTMate registers the session and redirects the user back to the original shopping page, creating a seamless experience.

Installation
Install Tampermonkey in your browser

Click the extension icon → Create a new script

Paste the code above (make sure to add @ symbols where needed) and save

Visit any matched shopping website, and the cashback session will activate automatically

Optimization Suggestions
Expand platform support: Add more match lines to cover additional e-commerce sites

Show activation notice: Inject a small banner to inform users that cashback has been activated

Track data: Store activation records in localStorage to monitor cashback history

Summary
Writing a cashback userscript is an efficient way to save money during everyday online shopping. By routing sessions through a platform like JTMate, users can earn cashback without any extra effort.

Top comments (0)