Skip to main content

How to make a single chrome extension or a javascript file run at every page of a website?

For context, I am trying to build a chrome extension for Codeforces.com which automatically enters my login credentials and logs me in and then drops me of at whatever subsection I have selected (like contests, home, profile, etc.). I have currently hacked up a solution with a seperate js file which sends me to the contests page if go to the Profile page( where I land by default after login). Once I get redirected once I dont want to get redirected again if I try to visit some other site.

I want to find a way to make a js file run for every codeforces.com subpage and conditionally exectue tasks based on what subpage I am on.

Any other solutions to my above problem will also help.

Thank you.

Json file

{
    "name": "CodeforcesAutologger",
    "description": "Automatically logs into Codeforces account",
    "author": "A",
    "version": "1.0",
    "manifest_version": 2,
    "browser_action": {
        "default_popup":"popup.html"
    },
    "content_scripts": [
        {
            "matches": ["https://codeforces.com/enter?back=%2F"], "js":["CodeforcesAutologger.js"]            
        },
        {
            "matches": ["https://codeforces.com/"], "js":["Gotologinpage.js"]
        },
        {
            "matches": ["https://codeforces.com/profile/profilename"], "js":["Gotolandingpage.js"]
        }
    ],
    "permissions": [
        "storage","tabs"
    ]
}

Gotologinpage.js window.location.href = "https://codeforces.com/enter?back=%2F";

CodeforcesAutologger.js

`console.log("CodeforcesAutologger loaded successfully");
var page_selection='contests'; 
var should_redirect=true;
var should_login=true;

if(localStorage && 'selector' in localStorage)
{
    page_selection= localStorage.getItem("selector");
    console.log(page_selection);
}
else
{
    localStorage.setItem("selector","home");
}

window.onload=function() {
    fn();
}
function fn(){
const email = document.querySelector("#handleOrEmail");
email.value= "sampleuserid";
console.log("UserID entered");
const password = document.querySelector("#password");
password.value = "samplepassword";
console.log("Password entered");
let allbuttons = document.getElementsByClassName("submit");
for(var i=0;i<allbuttons.length;i++)
    if(allbuttons[i].type=="submit" && allbuttons[i].value=="Login")
    {
        allbuttons[i].click();
        break;
    }
console.log("Login button clicked");
should_login=false;
}
`

Gotolandingpage.js

`function go_to_landing_page()
{
    if(localStorage.getItem("selector")=="contests"){    
        window.location.href = "https://codeforces.com/contests";
        console.log("Redirected to Contests page");
        should_redirect=false;
    }
    else if(localStorage.getItem("selector")=="home"){
        window.location.href = "https://codeforces.com";
        console.log("Redirected to Home page");
        should_redirect=false;
    }
}
if(should_redirect && localStorage.getItem("selector")!="profile") go_to_landing_page();`

popup.html

`<!DOCTYPE html>
<html>
    <div class="dropdown">
        <b>
          Select your landing page:
        </b>
        <select id="sel">
          <option value="home">Home</option>    
          <option value="contests">Contests</option>
        </select>
        <input id = "set" type="button" value="Set"> </input>
      </div>
      <script src="CodeforcesAutologger.js"></script>
      <script src="popup.js"></script>
</html>
`

popup.js


function update_globalvariable()
{
    if(localStorage && 'selector' in localStorage)
    {
        page_selection= localStorage.getItem("selector");
    }
}
function update_selection_popup()
{
    var selectElement = document.getElementById("sel")
    page_selection= selectElement.value;
    if (localStorage && 'selector' in localStorage)
    {
        localStorage.setItem("selector", selectElement.value);
    }
    else
    {
        localStorage.setItem("selector", "contests");
    }
}
document.getElementById("set").onclick =function () {
    update_selection_popup();
    go_to_landing_page();
};
Via Active questions tagged javascript - Stack Overflow https://ift.tt/0zNvVu1

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype