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
  • Loading branch information
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");
}
Loading
Oops, something went wrong.

0 comments on commit 7eb1065

Please sign in to comment.