Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 54 additions & 79 deletions profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,8 @@ function debugLog(logData){
}

// BEGIN DOMAIN FUNCTIONS //
function getHostName(url) {
var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
debugLog(match[2]);
return match[2];
}
else {
return null;
}
}
function getDomain(url) {
var hostName = getHostName(url);
var domain = url;

if (hostName != null) {
var parts = hostName.split('.').reverse();

if (parts != null && parts.length > 1) {
domain = parts[1] + '.' + parts[0];

if (hostName.toLowerCase().indexOf('.co.uk') != -1 && parts.length > 2) {
domain = parts[2] + '.' + domain;
}
}
}
debugLog(domain);
return domain;
return (new URL(url)).hostname
}
// END DOMAIN FUNCTIONS //

Expand All @@ -49,7 +24,7 @@ function addProfileListeners(){
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', changeProfile, false);
}

var classname = document.getElementsByClassName("editProfile");

for (var i = 0; i < classname.length; i++) {
Expand All @@ -73,28 +48,28 @@ function editProfile(event){
target.removeEventListener('click', editProfile, false);
target.addEventListener('click', saveProfileName, false);
//$(target).parent().parent().parent().find('input').text(target.getAttribute('data-profileName'));




}
function saveProfileName(event){
var target = event.target;


chrome.storage.local.get('profiles', function(items){
var currentDomain = $('#domain_label').html();
var currentProfile = $('#profile_label').html();
var profile = {};
var domainProfile = {};

if(jQuery.isEmptyObject(items) || jQuery.isEmptyObject(items.profiles) || jQuery.isEmptyObject(items.profiles[currentDomain])){
domainProfile = JSON.parse('{"currentProfile":"Profile 1", "profileData":{"Profile 1": {}}}');
if(!jQuery.isEmptyObject(items.profiles)){
profile = JSON.parse(JSON.stringify(items.profiles));
}
profile[currentDomain] = domainProfile;

}
else{
profile = items.profiles;
Expand All @@ -113,8 +88,8 @@ function saveProfileName(event){
loadProfiles();
});
}


//console.log(JSON.stringify(profile));
});
}
Expand All @@ -124,16 +99,16 @@ function removeProfile(event){
var currentDomain = $('#domain_label').html();
var currentProfile = $('#profile_label').html();
var profile = items.profiles;

delete profile[currentDomain]['profileData'][target];

var newProfile = Object.keys(profile[currentDomain]['profileData'])[0];
var passedVar = {'target':{'innerHTML':newProfile},'saveData':false};

if(currentProfile == target){
profile[currentDomain]['currentProfile'] = newProfile;
}

chrome.storage.local.set({ "profiles": profile }, function(){
if(currentProfile == target){changeProfile(passedVar);}
loadProfiles();
Expand All @@ -145,9 +120,9 @@ function resetDomain(){
chrome.storage.local.get('profiles', function(items){
var currentDomain = $('#domain_label').html();
var profile = items.profiles;

delete profile[currentDomain];

chrome.storage.local.set({ "profiles": profile }, function(){
loadProfiles();
});
Expand All @@ -163,7 +138,7 @@ function loadProfiles(){
chrome.storage.local.get('profiles', function(items){
var domain = $('#domain_label').html();
var profile;

if(jQuery.isEmptyObject(items) || jQuery.isEmptyObject(items.profiles) || jQuery.isEmptyObject(items.profiles[currentDomain])){
profile = JSON.parse('{"currentProfile":"Profile 1", "profileData":{"Profile 1": {}}}');
}
Expand All @@ -172,7 +147,7 @@ function loadProfiles(){
}
$('#profile_label').html(profile['currentProfile']);
//$('#storage_label').html(JSON.stringify(profile['profileData']));

for (var profileData in profile['profileData']){
if (typeof profile['profileData'][profileData] !== 'function') {
var tableRef = document.getElementById('profileTable').getElementsByTagName('tbody')[0];
Expand All @@ -181,58 +156,58 @@ function loadProfiles(){
// Insert a cell in the row at index 0
var newCell = newRow.insertCell(0);
// Append a text node to the cell

var textbox = document.createElement('input');
textbox.setAttribute("hidden", "true");
textbox.type = "textbox";
textbox.setAttribute("value", profileData);

var a = document.createElement('a');

var linkText = document.createTextNode(profileData);

if($('#profile_label').html() != profileData){
a.appendChild(linkText);
a.href = "#";
a.className = "changeProfile";

newCell.appendChild(a);
}
else{
var span = document.createElement('a');
span.className = "profileLabel";
span.appendChild(linkText);

newCell.appendChild(span);
}

newCell.appendChild(textbox);


var newCell2 = newRow.insertCell(1);
newCell2.className = "no-wrap";
var a2 = document.createElement('a');
var link2Text = document.createTextNode("edit");

a2.appendChild(link2Text);
a2.href = "#";
a2.setAttribute('data-profileName', profileData);
a2.className = "editProfile";

var a3 = document.createElement('a');
var link3Text = document.createTextNode("remove");

a3.appendChild(link3Text);
a3.href = "#";
a3.setAttribute('data-profileName', profileData);
a3.className = "removeProfile";

var cellSpan = document.createElement('span');
cellSpan.appendChild(a2);
cellSpan.appendChild(document.createTextNode(" "));
cellSpan.appendChild(a3);
cellSpan.className = "smallText";

newCell2.appendChild(cellSpan);
}
}
Expand All @@ -251,22 +226,22 @@ function newProfile(){
var newProfileName = $('#profileName_input').val();
var profile = {};
var domainProfile = {};

if(jQuery.isEmptyObject(items) || jQuery.isEmptyObject(items.profiles) || jQuery.isEmptyObject(items.profiles[currentDomain])){
domainProfile = JSON.parse('{"currentProfile":"Profile 1", "profileData":{"Profile 1": {}}}');
}
else{
domainProfile = items.profiles[currentDomain];
profile = items['profiles'];
}

domainProfile['profileData'][newProfileName] = "";
profile[currentDomain] = domainProfile;


$('#profile_label').html(profile['currentProfile']);
//$('#storage_label').html(JSON.stringify(profile));

if(newProfileName != "")
{
chrome.storage.local.set({ "profiles": profile }, function(){
Expand All @@ -290,24 +265,24 @@ function changeProfile(event){
var currentDomain = $('#domain_label').html();
chrome.cookies.getAll({domain: currentDomain}, function(cookies) {
var currentProfile = $('#profile_label').html();

chrome.storage.local.get('profiles', function(items){
var currentDomain = $('#domain_label').html();
var oldProfileData = cookies;
var newProfileData = items.profiles[currentDomain]['profileData'][target.innerHTML];

var profile = items.profiles;
var domainProfiles = profile[currentDomain]['profileData'];

domainProfiles[currentProfile] = oldProfileData;
profile[currentDomain]['currentProfile'] = target.innerHTML;
profile[currentDomain]['profileData'] = domainProfiles;


for(var i=0; i<cookies.length;i++) {
chrome.cookies.remove({url: extrapolateUrlFromCookie(cookies[i]), name: cookies[i].name});
}

if(newProfileData.length > 0){for (var i=0; i<newProfileData.length;i++){
newProfileData[i]['url'] = "http" + (newProfileData[i]['secure'] ? "s" : "") + "://" + newProfileData[i]['domain'].replace(/^\./, "");
debugLog(newProfileData[i]['domain']);
Expand All @@ -316,25 +291,25 @@ function changeProfile(event){
debugLog(JSON.stringify(newProfileData[i]));
chrome.cookies.set(newProfileData[i]);
}}



if (typeof saveData === 'undefined' || saveData == true) {
chrome.storage.local.set({ "profiles": profile }, function(){
loadProfiles();
});
}


//$('#storage_label').text(JSON.stringify(newProfileData));

chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {
var code = 'window.location.reload();';
chrome.tabs.executeScript(arrayOfTabs[0].id, {code: code});
});

});

});
//$('#message_label').text("Profile Change Button clicked " + target.innerHTML);
}
Expand Down Expand Up @@ -380,5 +355,5 @@ document.addEventListener('DOMContentLoaded', function() {
//document.querySelector('#filter_div input').addEventListener(
// 'input', reloadCookieTable);
//document.querySelector('#filter_div button').addEventListener(
// 'click', resetFilter);
// 'click', resetFilter);
});