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
Post a Comment