Skip to content

Commit

Permalink
Fixe Spoj - #20
Browse files Browse the repository at this point in the history
Generalize languages - #18
JS Logic separate - #19
ashish1294 committed Feb 12, 2016
1 parent 713771f commit 7eb1065
Showing 8 changed files with 246 additions and 227 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@ Install/*
*.DS_Store
*.sublime-project
pylintrc
*.sublime-*
30 changes: 30 additions & 0 deletions chrome-plugin/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var languages = [
{
ext : "c",
name : "C"
},
{
ext : "cpp",
name : "C++"
},
{
ext : "java",
name : "Java"
},
{
ext : "py",
name : "Python"
}
];

//Getting the URL of the HTMl Page
var url = document.URL;

//Removing the trailing punctuation marks
while(url[url.length - 1] === "/" || url[url.length - 1] === "#")
{
url = url.slice(0, url.length - 1);
}

//Spliting the URL to get path
var us = url.split("/");
225 changes: 0 additions & 225 deletions chrome-plugin/code_later.js

This file was deleted.

63 changes: 63 additions & 0 deletions chrome-plugin/codechef_problem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//Helper function to get click_functions
function click_codechef(language)
{
return function () {
console.log(language + " Code Now Clicked. Opening " + language + " IDE");
var prob_name = $('title')[0].text.split('|')[0].trim();
var prob_url = url;
var u_name = "";
//Checking if User is LoggedIn
if($('#user-bar span.login-box').length > 0)
{
//No Logged In User Detected
u_name = "No User";
}
else
{
//A user is logged in
u_name = $('#user-bar span').text().split('!')[0].replace('Hello', '').trim();
}
//Sending Message to the native host with a json object of problem details
chrome.runtime.sendMessage({problem_name: prob_name, problem_url: prob_url, user_name: u_name, lang : language});
};
}

// Helper function to add codechef buttons
var codechef_interval = null;
function add_codechef_buttons()
{
if ($('#problem-page-top').length === 0)
{
// This means that the page hasn't loaded completely yet. So Wait
return;
}
else
{
// Dynamic DOM Created by codechef
var button_list = $("#problem-page-top").find("ul");
var html = button_list.html();
// Adding Language Button
for (var i = 0; i < languages.length; i++)
{
html += "<li><a href='#' id='code_now_id_" + languages[i].ext + "_button'>Code in " + languages[i].name + "</a></li>";
}
button_list.html(html);

//Adding Listeners
for(var j = 0; j < languages.length; j++)
{
$("#code_now_id_" + languages[j].ext + "_button").click(click_codechef(languages[j].ext));
}
console.log("Code Now Button Added !! ~ Code Now Extension");

// Clear interval, so that buttons are not added repeatedly
clearInterval(codechef_interval);
}
}

var code = us[us.length - 1];
if(us[us.length - 2] === "problems" && code !== "easy" && code !== "medium" && code !== "hard" && code !== "challenge" && code !== "extcontest" && code !== "school")
{
// Adding A little sleep here because codechef mutates DOM to add all elements dynamically
codechef_interval = window.setInterval(add_codechef_buttons, 5000);
}
53 changes: 53 additions & 0 deletions chrome-plugin/codeforces_problem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function click_codeforces(language)
{
return function() {
console.log(language + " Code Now Clicked. Opening " + language + " IDE");
var prob_name = $('#pageContent .title').html().split('.')[1].trim();
var prob_url = url;
var u_name = "";
var reg_link = $('#header .lang-chooser div')[1].children[1].text.toLowerCase();
if(reg_link == "register")
{
u_name = "No User";
}
else
{
u_name = $('#header .lang-chooser div')[1].children[0].text.toLowerCase();
}
chrome.runtime.sendMessage({problem_name: prob_name, problem_url: prob_url, user_name: u_name, lang : language});
};
}

if(us.length > 4 && (us[us.length - 2] === "problem" || us[us.length - 3] === "problem"))
{
//Creating and Adding A whole new Sidebox with 3 buttons
$('#sidebar').html(
'<div class="roundbox sidebox" style="">' +
'<div class="roundbox-lt">&nbsp;</div>' +
'<div class="roundbox-rt">&nbsp;</div>' +
'<table class="rtable ">' +
'<tbody>' +
'<tr><th class="left" style="width:100%;"><a style="color: black" href="">Code Now</a></th></tr>' +
'<tr>' +
'<td class="left bottom" colspan="1">' +
'<div style="text-align:center;margin:1em;" id="code_now_button_box">' +
'</div>' +
'</td>' +
'</tr>' +
'</tbody>' +
'</table>' +
'</div>' +
$('#sidebar').html);

for(var i = 0; i < languages.length; i++)
{
var button = $('<button/>', {
text : 'Code in ' + languages[i].name,
click : click_codeforces(languages[i].ext)
});
$('#code_now_button_box').append(button);
$('#code_now_button_box').append($('<br/>'));
$('#code_now_button_box').append($('<br/>'));
}
console.log("Code Now Button Added !! ~ Code Now Extension");
}
39 changes: 39 additions & 0 deletions chrome-plugin/hackerearth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function click_hackerearth(language)
{
return function() {
console.log(language + " Code Now Clicked. Opening " + language + " IDE");
var prob_name = $('.problem-title').first().text().trim();
var prob_url = url;
var u_name = "";
var login_link = document.getElementsByClassName('track-header-login show-modal');

// If login link exists
if($('.track-header-login.show-modal').length !== 0)
{
u_name = "No User";
}
else
{
u_name = $('a.track-header-profile-box')[0].href.split('@')[1];
}
chrome.runtime.sendMessage({problem_name: prob_name, problem_url: prob_url, user_name: u_name, lang : language});
};
}

if (us[3] === "problem") {
var new_div = $('.editor-header').first().clone(false);
new_div.html('');
console.log(languages);
for(var i = 0; i < languages.length; i++)
{
var button = $('<button/>', {
class : 'button btn-blue',
text : 'Code In ' + languages[i].name,
style : 'margin: 5px 5px 0px 0px;',
click : click_hackerearth(languages[i].ext)
});
new_div.append(button);
}
$('.editor-header').first().after(new_div);
console.log("Code Now Button Added !! ~ Code Now Extension");
}
19 changes: 17 additions & 2 deletions chrome-plugin/manifest.json
Original file line number Diff line number Diff line change
@@ -8,8 +8,23 @@
"content_scripts": [
{
"run_at" : "document_end",
"matches" : ["https://www.codechef.com/*", "http://www.spoj.com/*", "http://codeforces.com/*", "https://www.hackerearth.com/*"],
"js" : ["code_later.js"]
"matches" : ["https://www.codechef.com/*"],
"js" : ["jquery.js", "basic.js", "codechef_problem.js"]
},
{
"run_at" : "document_end",
"matches" : ["http://www.spoj.com/problems/*"],
"js" : ["jquery.js", "basic.js", "spoj_problem.js"]
},
{
"run_at" : "document_end",
"matches" : ["http://codeforces.com/*"],
"js" : ["jquery.js", "basic.js", "codeforces_problem.js"]
},
{
"run_at" : "document_end",
"matches" : ["https://www.hackerearth.com/problem/*"],
"js" : ["jquery.js", "basic.js", "hackerearth.js"]
}
],
"background": {
43 changes: 43 additions & 0 deletions chrome-plugin/spoj_problem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function click_spoj(language)
{
return function() {
console.log(language + " Code Now Clicked. Opening " + language + " IDE");
var prob_name = $('.prob h2').html().split('-')[1].trim();
var prob_url = url;
var u_name = "";
if($('.submenu .navbar-right')[0].children.length == 2)
{
u_name = "No User";
}
else
{
u_name = $('.submenu .navbar-right a')[0].href.split(',')[1].replace('/', '');
}
chrome.runtime.sendMessage({problem_name: prob_name, problem_url: prob_url, user_name: u_name, lang : language});
};
}

var spoj_interval = null;
function add_spoj_buttons()
{
var submit_box = $('#problem-btn-submit-box');
for(var i = 0; i < languages.length; i++)
{
var button = $('<button/>', {
text : 'Code in ' + languages[i].name,
class : 'btn btn-default',
style : 'width: 100%; margin-top: 20px;',
click : click_spoj(languages[i].ext)
});
submit_box.append(button);
}
console.log("Code Now Button Added !! ~ Code Now Extension");
clearInterval(spoj_interval);
}

//Again Avoiding Special URLs
var code = us[us.length - 2];
if(code !== "classical" && code !== "challenge" && code !== "partial" && code !== "tutorial" && code !== "riddle")
{
spoj_interval = setInterval(add_spoj_buttons, 200);
}

0 comments on commit 7eb1065

Please sign in to comment.