Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
jcponce committed Apr 1, 2024
1 parent 66b87c3 commit 9b3d203
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
46 changes: 28 additions & 18 deletions function-plotter/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,39 +228,49 @@ $('#texture-options a').each(function () {
});
});


// When the user presses the button, show some copyable text
// First I tried it with a base64 expression
// which is easy to share in social media
// but now some algebraic expressions can be
// input in the link. It works so far :)
function showLink() {
const expressionBase64 = btoa($('#equation-input').val());
let url = `${location.protocol}//${location.host}${location.pathname}?expression=${expressionBase64}`;
let expression_base64 = btoa($('#equation-input').val());
let url = [location.protocol, '//', location.host, location.pathname].join('');
url = url + "?expression=" + expression_base64;
$('#copyable-link').val(url);
$('#link-container').show();
$('#copyable-link').select();
}

// Hide the link container when the copyable link loses focus
$('#copyable-link').blur(function () {
$('#link-container').hide();
});

// Check if the user already specified an expression in the URL
// If the user already specified
$(function () {
const expressionBase64 = getQueryVariable('expression');
if (expressionBase64) {
$('#equation-input').val(atob(expressionBase64.replace('/', '')));
let expression_base64 = getQueryVariable('expression');
//console.log(getQueryVariable('expression'))

if (expression_base64 && isBase64Encoded(getQueryVariable('expression'))) {
$('#equation-input').val(atob(expression_base64.replace('/', '')));
//console.log(isBase64Encoded(getQueryVariable('expression')))
} else if (expression_base64 && !isBase64Encoded(getQueryVariable('expression'))) {
$('#equation-input').val(decodeURIComponent(expression_base64));
//console.log(isBase64Encoded(getQueryVariable('expression')))
}
});

// Function to get the value of a query variable from the URL
function getQueryVariable(variable) {
const query = window.location.search.substring(1);
const vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) === variable) {
return decodeURIComponent(pair[1]);
}
}
return null;
function isBase64Encoded(str) {
// Remove white spaces from the string before checking
str = str.trim();

// Base64 encoded strings typically have a length that is a multiple of 4
// and only contain characters from the base64 character set, plus optional '=' padding
const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;

// Unicode-encoded strings should not match the base64 regex
return base64Regex.test(str);
}

// Get things started.
Expand Down
45 changes: 27 additions & 18 deletions function-plotter/scripts/mainhsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,38 +274,47 @@ $("#texture-options a").each(function () {
});

// When the user presses the button, show some copyable text
// First I tried it with a base64 expression
// which is easy to share in social media
// but now some algebraic expressions can be
// input in the link. It works so far :)
function showLink() {
const expressionBase64 = btoa($('#equation-input').val());
let url = `${location.protocol}//${location.host}${location.pathname}?expression=${expressionBase64}`;
let expression_base64 = btoa($('#equation-input').val());
let url = [location.protocol, '//', location.host, location.pathname].join('');
url = url + "?expression=" + expression_base64;
$('#copyable-link').val(url);
$('#link-container').show();
$('#copyable-link').select();
}

// Hide the link container when the copyable link loses focus
$('#copyable-link').blur(function () {
$('#link-container').hide();
});

// Check if the user already specified an expression in the URL
// If the user already specified
$(function () {
const expressionBase64 = getQueryVariable('expression');
if (expressionBase64) {
$('#equation-input').val(atob(expressionBase64.replace('/', '')));
let expression_base64 = getQueryVariable('expression');
//console.log(getQueryVariable('expression'))

if (expression_base64 && isBase64Encoded(getQueryVariable('expression'))) {
$('#equation-input').val(atob(expression_base64.replace('/', '')));
//console.log(isBase64Encoded(getQueryVariable('expression')))
} else if (expression_base64 && !isBase64Encoded(getQueryVariable('expression'))) {
$('#equation-input').val(decodeURIComponent(expression_base64));
//console.log(isBase64Encoded(getQueryVariable('expression')))
}
});

// Function to get the value of a query variable from the URL
function getQueryVariable(variable) {
const query = window.location.search.substring(1);
const vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) === variable) {
return decodeURIComponent(pair[1]);
}
}
return null;
function isBase64Encoded(str) {
// Remove white spaces from the string before checking
str = str.trim();

// Base64 encoded strings typically have a length that is a multiple of 4
// and only contain characters from the base64 character set, plus optional '=' padding
const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;

// Unicode-encoded strings should not match the base64 regex
return base64Regex.test(str);
}

// Get things started.
Expand Down

0 comments on commit 9b3d203

Please sign in to comment.