Commit 163cb93d authored by peachaku's avatar peachaku
Browse files

chrome v0.4

parent da5f8f11
This diff is collapsed.
# UBUEasyAccess > lama
KISS rewriting for lama AMU
ready to push on https://github.com/SCD-Aix-Marseille-Universite/lama-addons
no more changes here
This diff is collapsed.
This diff is collapsed.
/**
* Create an iframe with the notification
*/
if (typeof chrome !== "undefined" && chrome) {
browser = chrome
}
//check if iframe is already present
if (document.getElementById("lama_notification_frame")) {
//do nothing
} else {
var url = window.location.href;
var iframe = document.createElement('iframe');
iframe.id = 'lama_notification_frame';
iframe.className = 'css-lama-notification';
iframe.frameBorder = 0;
iframe.style.position = "fixed";
iframe.style.bottom = "0px";
iframe.style.right = "0px";
iframe.style.zIndex = 1000;
iframe.style.width = "65px";
iframe.style.height = "65px";
iframe.src = browser.extension.getURL("notification.html?" + url);
document.body.appendChild(iframe);
}
/**
* Listen to events in the iframe
*/
browser.runtime.onMessage.addListener(function(message) {
iframe.style.display = 'none';
if (message.demand && message.demand == 'redirect') {
var newUrl = message.text;
window.location.href = newUrl;
}
if (message.demand && message.demand == 'hide_popup') {
iframe.style.display = 'none';
}
});
\ No newline at end of file
/**
* this page will listen to things that interest us
* in practice that means: whenever the URL of the page changes
* if it does: check if it is in the list of URLs to proxy
* if yes: show notification
* and show a login button on the popup screen
*/
if (typeof chrome !== "undefined" && chrome) {
browser = chrome
}
/**
* Build the redirect URL
*
* @param url
* @returns {string}
*/
var getRedirectUrl = function(url) {
//url to the login page
var proxy = "https://lama.univ-amu.fr/login?qurl=";
/**
* Special cases
*/
//off campus access to WebOfKnowledge will send you to a login page that can't be proxied
//so redirect to proxied start page
if (url.indexOf('login.webofknowledge.com') > -1) {
url = 'http://apps.webofknowledge.com';
}
return proxy + encodeURIComponent(url);
};
/**
* urlsList is defined in urls.min.js
* edit that list to reflect your institutions licenses
*
* @type {Array}
*/
// var urlsToProxy = urlsList;
/**
* Determine the domain (which you can then check agains urls.min.js)
*/
function getDomain(url) {
var doubleSlash = url.indexOf('//');
var unHttp = url.substr(doubleSlash + 2);
var domainSlash = unHttp.indexOf('/');
var domain = unHttp.substr(0, domainSlash);
return domain;
}
/**
* function to see if the url can be proxied
*
* @param searchUrl
* @param callback
*/
function getProxied(searchUrl, callback) {
var toProxy = '';
var doubleslash = searchUrl.indexOf('//');
var unhttp = searchUrl.substr(doubleslash + 2);
var domainslash = unhttp.indexOf('/');
var domain = unhttp.substr(0, domainslash);
if (domain.indexOf('.') === -1) {
return null;
}
var tldomain = domain.match(/[\w]+\.[\w]+$/)[0];
var parentDomain = domain.substr(domain.indexOf('.') + 1);
/**
* Save in storage
*/
// Check if we are browsing a site via the proxy
saveStorageOnProxy(domain.indexOf('lama.univ-amu.fr') !== -1);
if (urlsToProxy.indexOf(domain) != -1 || urlsToProxy.indexOf(parentDomain) != -1 || urlsToProxy.indexOf('www.' + parentDomain) != -1) {
toProxy = 'yes';
saveStorageToProxy('yes');
callback(toProxy);
} else {
toProxy = 'no';
saveStorageToProxy('no');
callback(toProxy);
}
}
/**
* Initalize storage values
*
*/
function saveStorageQualify(value) {
localStorage['qualify'] = value;
}
function saveStorageToProxy(value) {
localStorage['toproxy'] = value;
}
function saveStorageOnProxy(value) {
localStorage['onproxy'] = (value == true) ? 'on' : 'off';
}
function saveStorageNotificationType(value) {
localStorage['notetype'] = value;
}
function getUserPreferences(callback) {
browser.storage.sync.get({
showNotification: true,
},
function(items) {
callback(items);
});
}
/**
* This is where the main logic happens
* We depending on the page's domain
* we determine whether we should change the extension icon,
* show a login button in the pop-up and, depending on the user's
* preferences, an inpage notification
*
* @param checkResult
* @param url
* @param tabId
* @param userPreferences
*/
function checkRedirect(checkResult, url, tabId, userPreferences) {
if (checkResult == 'yes') {
// Record a visit on a detected site
//change to icon to "on"
browser.browserAction.setIcon({ path: { "18": "img/64-go.png" } });
//we want to make the login button visible in the icon popup
//we can't send a message, because the popup may not be active
//instead, let's try setting something in localstorage
saveStorageQualify('yes');
//if the user has notifications on
//show notification
if (userPreferences.showNotification) {
saveStorageNotificationType('mini');
browser.tabs.executeScript(tabId, {
code: 'var notetype="mini";'
},
function() {
browser.tabs.executeScript(tabId, { file: "js/content.js" }, function() {});
});
}; //end of if shownotification
} //end
else if (localStorage['onproxy'] == 'on') {
//if you are on the proxy, show a "green" icon
browser.browserAction.setIcon({ path: { "18": "img/64-on.png" } });
} else {
saveStorageQualify('no');
browser.browserAction.setIcon({ path: { "18": "img/64-no.png" } });
}
}
/**
* Get the current URL.
*
* @param {function(string)} callback - called when the URL of the current tab
* is found.
*/
function getCurrentTabUrl(callback) {
// Query filter to be passed to chrome.tabs.query - see
// https://developer.chrome.com/extensions/tabs#method-query
var queryInfo = {
active: true,
currentWindow: true
};
browser.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
if (typeof(tab) === 'undefined') {
return;
}
var url = tab.url;
callback(url);
});
}
/**
* Start: do this whenever the browser requests a page
*
*/
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status === 'complete') {
var url = tab.url;
//start by setting all local storage variables to null
saveStorageQualify(null);
saveStorageToProxy(null);
saveStorageOnProxy(null);
getProxied(url, function(checkResult) {
getUserPreferences(function(userPreferences) {
checkRedirect(checkResult, url, tabId, userPreferences);
});
});
}
});
/**
* Called when the user switches to the current tab
*/
browser.tabs.onActivated.addListener(function(activeInfo) {
// Set default icon for new tab
browser.browserAction.setIcon({ path: { "18": "img/64-no.png" } });
//start by setting all local storage variables to null
saveStorageQualify(null);
saveStorageToProxy(null);
saveStorageOnProxy(null);
getCurrentTabUrl(function(url) {
getProxied(url, function(checkResult) {
getUserPreferences(function(userPreferences) {
checkRedirect(checkResult, url, activeInfo.tabId, userPreferences);
});
});
});
});
/**
* function if user has clicked on "connexion" menu entry
* The user is redirected to the the appropriate page,
* usually one where they can log in to gain access
*/
browser.notifications.onButtonClicked.addListener(function(ntId, btnIdx) {
if (ntId == myNotificationID && btnIdx === 0) {
//go to current tab, find that url and redirect
getCurrentTabUrl(function(url) {
var redirectUrl = getRedirectUrl(url);
browser.tabs.update({ url: redirectUrl });
});
}
});
\ No newline at end of file
/**
* Fetch the URL domains
*/
if (typeof chrome !== "undefined" && chrome) {
browser = chrome
}
browser.runtime.onInstalled.addListener(function() {
validateCacheEzProxy();
});
browser.runtime.onStartup.addListener(function() {
validateCacheEzProxy();
});
function validateCacheEzProxy() {
var oReq = new XMLHttpRequest();
oReq.onload = function(e) {
// Alert on URL update (delete on prod)
console.log("refresh urlsToProxy");
// We get the json for this extension, we can save it as storage
urlsToProxy = JSON.parse(oReq.response);
};
// Get URL list ezproxy/pages/public/urls.json
oReq.open("get", "http://lama.univ-amu.fr/public/urls.json", true);
oReq.send();
}
\ No newline at end of file
This diff is collapsed.
/**
* Build the redirect URL
*
* @param url
* @returns {string}
*/
var getRedirectUrl = function(url) {
//The URL to the relevant login page. The one for the AMU proxy is shown below as an example
var proxy = "https://lama.univ-amu.fr/login?qurl=";
/**
* Special cases
*/
//off campus access to WebOfKnowledge will send you to a login page that can't be proxied
//so redirect to proxied start page
if (url.indexOf('login.webofknowledge.com') > -1) {
url = 'http://apps.webofknowledge.com';
}
return proxy + encodeURIComponent(url);
};
/**
* Get the current Url
*/
function getCurrentTabUrl(callback) {
var url = window.location.search;
var use_url = url.substr(1);
callback(use_url);
}
\ No newline at end of file
/**
* Show the relevant parts of the notification
*/
document.addEventListener('DOMContentLoaded', function() {
var qualify = localStorage['qualify'],
toProxy = localStorage['toproxy'],
onProxy = localStorage['onproxy'],
noteType = localStorage['notetype'];
if (onProxy == 'on') {
$('#proxy').hide();
} else {
if (toProxy == "yes" && qualify == "yes") {
document.getElementById('proxy').style.display = "block";
} else {
document.getElementById('proxy').style.display = "none";
}
if (qualify == 'yes') {
document.getElementById("small_note_loginbutton").style.display = "block";
getCurrentTabUrl(function(url) {
document.getElementById("small_note_loginbutton").href = getRedirectUrl(url);
});
} else {
document.getElementById("small_note_loginbutton").style.display = "none";
}
}
});
/**
* Listen to the login menu entry
*/
document.getElementById("small_note_loginbutton").addEventListener("click", function() {
getCurrentTabUrl(function(url) {});
var redirectUrl = document.getElementById("small_note_loginbutton").href;
chrome.runtime.sendMessage({
demand: "redirect",
text: redirectUrl
});
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment