Skip to content

Commit

Permalink
Finalized info display with popup settings. Updated version to 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sid-kap committed Jul 1, 2014
1 parent 13f6442 commit 915c0f3
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 79 deletions.
30 changes: 30 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

// Check whether new version is installed
chrome.runtime.onInstalled.addListener(function(details) {
var settings = ['concatRegistrarResults', 'showRegistrarInfoIcons', 'makeCisCharts'];

chrome.storage.sync.get(settings, function(items) {
var setting;

for (var i in settings) {
setting = settings[i];
if (!items.hasOwnProperty(setting)) {
// Enable all settings that haven't been explicitly enabled or disabled before.
chrome.storage.sync.set({setting: true}, function() { });
}
}
});

chrome.tabs.create({
url: chrome.extension.getURL('options.html')
});

// Stackoverflow copypasta:

// if(details.reason == "install"){
// console.log("This is a first install!");
// } else if(details.reason == "update"){
// var thisVersion = chrome.runtime.getManifest().version;
// console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
// }
});
18 changes: 3 additions & 15 deletions src/cis/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@ var $ = jQuery;

$(function() {

chrome.storage.sync.get('runScriptOnCisPage', function(items) {
var justEnabledSetting = false;
chrome.storage.sync.get('makeCisCharts', function(items) {

if (!items.hasOwnProperty('runScriptOnCisPage')) {
// First time using the extension
// Let's enable it by default.
chrome.storage.sync.set({'runScriptOnCisPage': true}, function() {
console.log('Run script on Registrar Page enabled by default.');
});

justEnabledSetting = true;
}

// Run the script only if the user has checked the box in the extension options,
// or if the script has been enabled by default the first time this page was opened.
if (items.runScriptOnCisPage || justEnabledSetting) {
// Run the script only if the user has checked the box in the extension options
if (items.makeCisCharts) {
makeCharts();
}
});
Expand Down
14 changes: 12 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "UT Course Registration",
"description": "Adds features to UT's course catalog.",
"version": "1.0",
"version": "1.1",

"permissions": [
"https://utdirect.utexas.edu/registrar/",
Expand All @@ -13,7 +13,8 @@
"content_scripts": [
{
"matches": ["https://utdirect.utexas.edu/registrar/nrclav/results.WBX*"],
"js": ["jquery-2.1.1.min.js", "registrar/contentScript.js", "common.js"]
"js": ["jquery-2.1.1.min.js", "registrar/contentScript.js", "common.js"],
"css": ["registrar/registrar.css"]
},
{
"matches": ["https://utdirect.utexas.edu/ctl/ecis/results/view_results.WBX*"],
Expand All @@ -22,5 +23,14 @@
}
],

"web_accessible_resources":[
"resources/*"
],

"background": {
"scripts": ["background.js"],
"persistent": false
},

"options_page": "options.html"
}
24 changes: 21 additions & 3 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@
<title>Options - UT Course Registration Extension</title>
<link rel="stylesheet" type="text/css" href="options.css">
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css">
<!-- <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"> -->
<script src="options.js"></script>

</head>
<body>
<div id="content">
<h1>Options - UT Course Registration Extension</h1>
<input type="checkbox" id="registrarCheckbox"><span class="option">Concatenate pages on <a href="https://utdirect.utexas.edu/registrar/nrclav/">registrar site</a></span></input>
<input type="checkbox" id="concatCheckbox">
<span class="option">
Concatenate pages on <a href="https://utdirect.utexas.edu/registrar/nrclav/" target="_blank">registrar site</a>
</span>
</input>

<br>
<input type="checkbox" id="cisCheckbox"><span class="option">Draw bar charts on <a href="https://utdirect.utexas.edu/ctl/ecis/results/search.WBX">Course Instructor Survey site</a></span></input>

<input type="checkbox" id="infoIconsCheckbox">
<span class="option">
Make info icons for class details on <a href="https://utdirect.utexas.edu/registrar/nrclav/" target="_blank">registrar site</a>
</span>
</input>

<br>

<input type="checkbox" id="cisCheckbox">
<span class="option">
Draw bar charts on <a href="https://utdirect.utexas.edu/ctl/ecis/results/search.WBX" target="_blank">Course Instructor Survey site</a>
</span>
</input>
</div>
</body>
</html>
40 changes: 14 additions & 26 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@

document.addEventListener('DOMContentLoaded', function() {
chrome.storage.sync.get(['runScriptOnRegistrarPage', 'runScriptOnCisPage'], function(items) {
chrome.storage.sync.get(['concatRegistrarResults', 'showRegistrarInfoIcons', 'makeCisCharts'], function(items) {

if (items.hasOwnProperty('runScriptOnRegistrarPage')) {
document.getElementById('registrarCheckbox').checked = items['runScriptOnRegistrarPage'];
} else {
// On first run of application, turn features on by default.
document.getElementById('registrarCheckbox').checked = true;
}
document.getElementById('concatCheckbox').checked = items['concatRegistrarResults'];
document.getElementById('infoIconsCheckbox').checked = items['showRegistrarInfoIcons'];
document.getElementById('cisCheckbox').checked = items['makeCisCharts'];

if (items.hasOwnProperty('runScriptOnCisPage')) {
document.getElementById('cisCheckbox').checked = items['runScriptOnCisPage'];
} else {
// On first run of application, turn features on by default.
document.getElementById('cisCheckbox').checked = true;
}

// We may have just checked the checkboxes by default on the first run.
// Update preferences here just to make sure that this change is saved.
updatePrefs();
});

document.getElementById('registrarCheckbox').addEventListener('change', updatePrefs);
document.getElementById('concatCheckbox').addEventListener('change', updatePrefs);
document.getElementById('infoIconsCheckbox').addEventListener('change', updatePrefs);
document.getElementById('cisCheckbox').addEventListener('change', updatePrefs);
});

function updatePrefs() {
var isRegistrarOptionChecked, isCisOptionChecked, settings;
var isConcatOptionChecked = document.getElementById('concatCheckbox').checked,
isInfoIconsOptionChecked = document.getElementById('infoIconsCheckbox').checked,
isCisChartsOptionChecked = document.getElementById('cisCheckbox').checked,
settings = {
'concatRegistrarResults': isConcatOptionChecked,
'showRegistrarInfoIcons': isInfoIconsOptionChecked,
'makeCisCharts': isCisChartsOptionChecked
};

isRegistrarOptionChecked = document.getElementById('registrarCheckbox').checked;
isCisOptionChecked = document.getElementById('cisCheckbox').checked;

settings = {
'runScriptOnRegistrarPage': isRegistrarOptionChecked,
'runScriptOnCisPage': isCisOptionChecked
};
chrome.storage.sync.set(settings, function() {
console.log('settings updated');
});
Expand Down
153 changes: 120 additions & 33 deletions src/registrar/contentScript.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,137 @@

var $ = jQuery;
var concatRegistrarResults;
var doMakeInfoIcons;

$(function() {

chrome.storage.sync.get('runScriptOnRegistrarPage', function(items) {
var $body;
var justEnabledSetting = false;
chrome.storage.sync.get(['concatRegistrarResults', 'showRegistrarInfoIcons'], function(items) {

if (!items.hasOwnProperty('runScriptOnRegistrarPage')) {
// First time using the extension
// Let's enable it by default.
chrome.storage.sync.set({'runScriptOnRegistrarPage': true}, function() {
console.log('Run script on Registrar Page enabled by default.');
});
concatRegistrarResults = items.concatRegistrarResults;
doMakeInfoIcons = items.showRegistrarInfoIcons;

justEnabledSetting = true;
}

// Run the script only if the user has checked the box in the extension options,
// or if the script has been enabled by default the first time this page was opened.
if (items.runScriptOnRegistrarPage || justEnabledSetting) {

$body = $('body');
// Run the script only if the user has checked the box in the extension options
if (items.concatRegistrarResults) {

$body.append($('<div id="hiddenPages" hidden></div>'))
loadMorePages($body);
// Use setTimeout() instead of explicitly calling run() because
// errors are formatted weirdly when errors
// happen inside of a chrome.storage.sync.get() callback

injectFontAwesome();
// Concat the pages, and if user wants info icons to be made, make user icons.
setTimeout(function() { run(items.showRegistrarInfoIcons); }, 0);

} else if (items.showRegistrarInfoIcons) {
// Only make info icons, do not concat pages.
setTimeout(makeInfoIcons, 0);
}

});
});

function makeInfoIcons(context) {
$('tr.tbon > span.em', context).prepend('hi'); //<i class="fa fa-camera-retro fa-3x"></i>');
/**
* Concatenate the pages.
* @param showInfoIcons whether to show the info icons after concatenating the pages.
*/
function run(showInfoIcons) {
var $body = $('body');

$body.append('<div id="hiddenPages" hidden></div>');

loadMorePages($body);
}

function makeInfoIcons() {
var $row,
$a,
$link,
$rows = $('tr.tbon td span.em').parent().parent(),
min,
max;

if (concatRegistrarResults) {
min = 0;
max = $rows.length - 1;
} else {
min = 1;
max = $rows.length - 2;
}

for (var i = min; i <= max; i++) {
makeIcon($($rows[i]));
}
}

function makeIcon($row) {
$a = $row.parent().next().children(':first').children(':first').children(':first')
$link = $('<a class="info"></a>');

$row.prepend($link);
$link.attr('data-href', $a.attr('href'));

// Weird-looking way of making sure that $link's onclick method
// calls showDescription with itself as the parameter.
$link.click(
function($$link) { return function(){ showDescription($$link); }; }($link)
);
}

function loadMorePages (body) {
function showDescription($link) {

if ($link.attr('data-loaded')) {

// Description has already been loaded.
// Show/hide the description
$link.next().toggle();

} else {

$.ajax({

url: $link.attr('data-href'),

success: function(data, textStatus, jqXHR) {
var text = '',
regex = /<p class="space">([\s\S]*?)<\/p>/g,
match;

// Find all the matches in the received data
while (match = regex.exec(data)) {
text += match[1];
text += '<br><br>';
}

$link.after('<div class="description">' + text + '</div>');
$link.attr('data-loaded', true);
},

error: function(jqXHR, textStatus, errorThrown) { }

});
}

}

/**
* Steps that must be taken after all the pages have been loaded.
*/
function finishUp() {

// Get rid of the Previous Page/Next Page buttons
$('div.otherClasses').parent().parent().parent().parent().parent().parent().remove();
$('a.otherClasses').parent().parent().parent().remove();

if (doMakeInfoIcons) {
makeInfoIcons();
}
}

function loadMorePages(body) {

var form = body.find('#next_page');

if (form.length === 0) {
// This must be the last page.
//console.log("Form not found on this page.");

// Get rid of the Previous Page/Next Page buttons
$('div.otherClasses').parent().parent().parent().parent().parent().parent().remove();
$('a.otherClasses').parent().parent().parent().remove();
finishUp();
return;
}

Expand All @@ -69,6 +154,12 @@ function loadMorePages (body) {
success: function(data, textStatus, jqXHR) {
var table, rows, len, targetTable;

// Do not load the scripts in the header
// This reduces the number of web requests.
data = data.replace(/<script([\s\S]*?)>[\s\S]*?<\/script>/g, '')

console.log(data);

// Put the background-loaded page into the hidden div
hiddenPages.html(data);

Expand All @@ -79,8 +170,6 @@ function loadMorePages (body) {

targetTable = $('body #classList');

makeInfoIcons(table);

// Copy over rows from the table of classes from the hidden div
$.each(rows, function(index, row) {
// Do not copy the first and last row of each table
Expand All @@ -94,9 +183,7 @@ function loadMorePages (body) {
// Load more pages!
setTimeout(function(){ loadMorePages(hiddenPages); }, 0);
},
error: function(jqXHR, textStatus, errorThrown) {
//if fails
}
error: function(jqXHR, textStatus, errorThrown) { }
});

e.preventDefault(); //STOP default action
Expand Down
Loading

0 comments on commit 915c0f3

Please sign in to comment.