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

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

The number of nodes and edges I calculated manually and by a method provided by networkx library dont match

I am trying to analyse a graph using networkx library in Python. I have implemented a function that returns the nodes and edges of that graph. After that I counted the number of nodes and edges by implementing the len() function for nodes and edges, which gave me 17 for both nodes and edges. However, when I used the method graph.number_of_nodes() of networkx library it gave me 53. My code: import pandas as pd import networkx as nx import matplotlib.pyplot as plt header_list = ["a","b","w"] E =pd.read_csv("aves-barn-swallow-contact-network.edges",sep=" ",header= None,names=header_list) # print(E.head()) # prints first few rows of the csv file G = nx.from_pandas_edgelist(E,"a","b",["w"]) nx.draw(G) plt.show() class GraphAnalyse: #finding nodes and edges def nodeEdge(self,Graph): nodes = Graph.nodes() edges = Graph.edges() ...