Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
97c29e6
Add images to each profile
timcleaver-techconnect Jan 29, 2019
8f51333
Guard against unmatched profile
timcleaver Jan 31, 2019
4e6d39a
Add element id and tests
timcleaver Jan 31, 2019
13cf65e
Change order of profiles since the indexes are hard-coded in tests
timcleaver Feb 1, 2019
3cafd31
Fix role count
timcleaver Feb 1, 2019
4ed88a6
Fix role counts
timcleaver Feb 1, 2019
7e578ac
Fix role counts and test new inputs
timcleaver Feb 1, 2019
e2743c6
Update role names and fix some counts in tests
timcleaver Feb 1, 2019
cc475b1
Fix rolenames
timcleaver Feb 1, 2019
48b2d6a
Fix counts and test for new roles
timcleaver Feb 1, 2019
2863103
Missed one rolename change
timcleaver Feb 1, 2019
3fa7444
Use ems for image sizes. Use 14em based on no scrollbars in both chro…
timcleaver Feb 4, 2019
dfaf925
Use style attribute so ems work correctly
timcleaver Feb 4, 2019
5bbf633
Describe optional image parameter in README.md
timcleaver Feb 11, 2019
f63c2de
Merge branch 'master' of https://github.com/tilfin/aws-extend-switch-…
timcleaver Jun 11, 2019
4904d5e
Merge branch 'master' of https://github.com/tilfin/aws-extend-switch-…
timcleaver Aug 13, 2019
5e34df5
Fix for bug #111
timcleaver Aug 30, 2019
4698807
Remove debug
timcleaver Aug 30, 2019
fed8017
Check that we find a matching profile
timcleaver Aug 30, 2019
8924b3c
Create imagedata uris in options (or allow users to set them manually…
timcleaver Aug 30, 2019
1fcfdb4
No spaces before ()
timcleaver Aug 30, 2019
ea41d66
No spaces after catch
timcleaver Aug 30, 2019
e21b21d
Add imagedata attributes to the test configuration file
timcleaver Aug 30, 2019
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
16 changes: 12 additions & 4 deletions src/lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ function loadProfiles(profileSet, list, csrf, hidesHistory, hidesAccountId) {
input = li.querySelector('input[type="submit"]');
let name = input.value;
name = name.replace(/\s+\|\s+\d+$/, '');
let profile = profileSet.destProfiles.find(function (profile) {
return profile.profile === name;
});
if (profile && profile.imagedata) {
li.querySelector('label.awsc-role-color').innerHTML = Sanitizer.escapeHTML`
<img src=${profile.imagedata} style="margin-top: -1px; margin-left: -1px; width: 17px; height: 17px">
`;
}
if (profileSet.excludedNames.includes(name)) {
const nextLi = li.nextElementSibling;
list.removeChild(li);
Expand All @@ -124,7 +132,7 @@ function loadProfiles(profileSet, list, csrf, hidesHistory, hidesAccountId) {

var color = item.color || 'aaaaaa';
var actionHost = window.location.host.endsWith('.amazonaws-us-gov.com') ? 'signin.amazonaws-us-gov.com' : 'signin.aws.amazon.com';
if (!item.image) {
if (!item.imagedata) {
list.insertAdjacentHTML('beforeend', Sanitizer.escapeHTML`<li>
<form action="https://${actionHost}/switchrole" method="POST" target="_top" data-aesr-profile="${item.profile}">
<input type="hidden" name="action" value="switchFromBasis">
Expand All @@ -150,7 +158,7 @@ function loadProfiles(profileSet, list, csrf, hidesHistory, hidesAccountId) {
<input type="hidden" name="color" value="${color}">
<input type="hidden" name="csrf" value="${csrf}">
<input type="hidden" name="redirect_uri" value="${redirectUri}">
<label for="awsc-recent-role-switch-0" class="awsc-role-color"><img src=${item.image.replace(/"/g, '')} style="margin-top: -1px; margin-left: -1px; width: 17px; height: 17px"></label>
<label for="awsc-recent-role-switch-0" class="awsc-role-color"><img src=${item.imagedata} style="margin-top: -1px; margin-left: -1px; width: 17px; height: 17px"></label>
<input type="submit" class="awsc-role-submit awsc-role-display-name" name="displayName" value="${name}"
title="${item.role_name}@${item.aws_account_id}" style="white-space:pre"></form>
</li>`);
Expand Down Expand Up @@ -221,8 +229,8 @@ function attachColorLine(profiles) {
const color = found && found.color || null;

var label = usernameMenu.querySelector('.nav-elt-label');
if (found && found.image) {
label.insertAdjacentHTML('beforebegin', Sanitizer.escapeHTML`<img id="AESW_Image" src=${found.image.replace(/"/g, '')} style="float: left; padding-right: .66em; width: 1.33em; height: 1.33em">`);
if (found && found.imagedata) {
label.insertAdjacentHTML('beforebegin', Sanitizer.escapeHTML`<img id="AESW_Image" src=${found.imagedata} style="float: left; padding-right: .66em; width: 1.33em; height: 1.33em">`);
}

if (color) {
Expand Down
122 changes: 105 additions & 17 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ function elById(id) {
return document.getElementById(id);
}

// taken from: https://davidwalsh.name/convert-image-data-uri-javascript
// converted to return a promise
function getDataUri(profile) {
return new Promise(function(resolve, reject) {
var image = new Image();
image.setAttribute('crossOrigin', 'anonymous');

image.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = 64; // or 'width' if you want a special/scaled size
canvas.height = 64; // or 'height' if you want a special/scaled size

canvas.getContext('2d').drawImage(this, 0, 0, 64, 64);

// get as Data URI
profile.imagedata = canvas.toDataURL('image/png')
console.log(`Got data uri for: ${profile.image}: ${profile.imagedata}`);
resolve(profile);
};
image.onerror = function() {
console.error(`Failed to load image: ${profile.image}`);
reject(profile)
}

console.log(`Getting data uri for: ${profile.image}`);
image.src = profile.image.replace(/"/g, '');
})
}

window.onload = function() {
var colorPicker = new ColorPicker(document);

Expand Down Expand Up @@ -34,27 +63,86 @@ window.onload = function() {
msgSpan.innerHTML = '<span style="color:#dd1111">Failed to save bacause the number of profiles exceeded maximum 200!</span>';
return;
}
// map the profiles to promises. if there is an image specification but no
// imagedata attribute then generate the data-uri otherwise resolve to the
// original profile
Promise.all(profiles.map(function(profile) {
if (profile.image && !profile.imagedata) {
return getDataUri(profile)
}
return new Promise(function(resolve, reject) {
resolve(profile)
});
})).then(values => {
// parse the rawstr into a list of lines or profiles (where each profile
// is a list of lines) preserving comments and formatting so that we can
// just add the imagedata lines. NOTE: the data parser turns comments
// and empty lines into profile separators
const lines = rawstr.split('\n');
const profileLines = [];
let output = "";
let currentProfile = undefined;
lines.forEach(function(line) {
if (r = line.match(/^\[(.+)\]$/)) {
var pname = r[1].trim();
pname = pname.replace(/^profile\s+/i, '');
currentProfile = {
profile: pname,
lines: [line]
};
profileLines.push(currentProfile);
} else {
if (currentProfile) {
currentProfile.lines.push(line);
} else {
output += line;
}
}
});

localStorage['rawdata'] = rawstr;
// re-render the profiles as string to update the UI and local storage
output += profileLines.map(function(profile) {
let match = values.find(function(value) {
return value.profile === profile.profile
});
// only add an imagedata line if there isn't one
if (!profile.lines.find(function(line) { return line.startsWith('imagedata'); })) {
console.log('no imagedata');
// add it after the image line if there is one
const index = profile.lines.findIndex(function(line) { return line.startsWith('image'); });
if (index > -1) {
profile.lines = profile.lines.slice(0, index + 1)
.concat([`imagedata = ${match.imagedata}`])
.concat(profile.lines.slice(index + 1));
}
}
return profile.lines.join('\n');
}).join('\n');

const dps = new DataProfilesSplitter();
const dataSet = dps.profilesToDataSet(profiles);
dataSet.lztext = LZString.compressToUTF16(rawstr);
localStorage['rawdata'] = output;
textArea.value = output;

chrome.storage.sync.set(dataSet,
function() {
const { lastError } = chrome.runtime || browser.runtime;
if (lastError) {
msgSpan.innerHTML = Sanitizer.escapeHTML`<span style="color:#dd1111">${lastError.message}</span>`;
return;
}
const dps = new DataProfilesSplitter();
const dataSet = dps.profilesToDataSet(values);
dataSet.lztext = LZString.compressToUTF16(output);

msgSpan.innerHTML = '<span style="color:#1111dd">Configuration has been updated!</span>';
setTimeout(function() {
msgSpan.innerHTML = '';
}, 2500);
});
} catch (e) {
chrome.storage.sync.set(dataSet,
function() {
const { lastError } = chrome.runtime || browser.runtime;
if (lastError) {
msgSpan.innerHTML = Sanitizer.escapeHTML`<span style="color:#dd1111">${lastError.message}</span>`;
return;
}

msgSpan.innerHTML = '<span style="color:#1111dd">Configuration has been updated!</span>';
setTimeout(function() {
msgSpan.innerHTML = '';
}, 2500);
});
}).catch(error => {
console.log(error.message);
});
} catch(e) {
msgSpan.innerHTML = '<span style="color:#dd1111">Failed to save because of invalid format!</span>';
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{ "profile": "b-stg", "aws_account_id": "666611113333", "role_name": "stg-role", "source_profile": "base-b" },
{ "profile": "b-prod", "aws_account_id": "666611114444", "role_name": "prod-role", "source_profile": "base-b", "color": "ffcc333" },
{ "profile": "b-renpou", "aws_account_id": "666611115555", "role_name": "renpou", "source_profile": "base-b" },
{ "profile": "a-stg-image", "aws_account_id": "555511113333", "role_name": "stg-role-image", "source_profile": "base-a", "image": "not-found.png" },
{ "profile": "b-prod-image", "aws_account_id": "666611114444", "role_name": "prod-role-image", "source_profile": "base-b", "image": "not-found.png" }
{ "profile": "a-stg-image", "aws_account_id": "555511113333", "role_name": "stg-role-image", "source_profile": "base-a", "image": "not-found.png", "imagedata": "imagedata" },
{ "profile": "b-prod-image", "aws_account_id": "666611114444", "role_name": "prod-role-image", "source_profile": "base-b", "image": "not-found.png", imagedata: "imagedata" }
]
}