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

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

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...

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...