mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2025-05-02 06:26:16 -04:00
Make JS function more robust
This commit is contained in:
parent
99d49a56ba
commit
c2a46b7af7
3 changed files with 38 additions and 7 deletions
|
@ -743,13 +743,44 @@ jQuery.PrivateBin = (function($, sjcl, Base64, RawDeflate) {
|
|||
*/
|
||||
me.getPasteId = function()
|
||||
{
|
||||
if (id === null) {
|
||||
// Attention: This also returns the delete token inside of the ID, if it is specified
|
||||
id = (window.location.search.match(/[a-z0-9]{16}/) || [''])[0];
|
||||
const idRegEx = /^[a-z0-9]{16}$/;
|
||||
const idRegExFind = /[a-z0-9]{16}/;
|
||||
|
||||
if (id === '') {
|
||||
throw 'no paste id given';
|
||||
// return cached value
|
||||
if (id !== null) {
|
||||
return id;
|
||||
}
|
||||
|
||||
// do use URL interface, if possible
|
||||
if (window.URL && window.URL.prototype && ('searchParams' in window.URL.prototype)) {
|
||||
try {
|
||||
const url = new URL(window.location);
|
||||
|
||||
for (const param of url.searchParams) {
|
||||
const key = param[0];
|
||||
const value = param[1];
|
||||
|
||||
if (value === '' && idRegEx.test(key)) {
|
||||
// safe, as the whole regex is matched
|
||||
id = key;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// fallback below
|
||||
console.error('URL interface not properly supported, error:', e);
|
||||
}
|
||||
} else {
|
||||
console.warn('URL interface appears not to be supported in this browser.');
|
||||
}
|
||||
|
||||
// fallback to simple RegEx
|
||||
console.warn('fallback to simple RegEx search');
|
||||
// Attention: This also returns the delete token inside of the ID, if it is specified
|
||||
id = (window.location.search.match(idRegExFind) || [''])[0];
|
||||
|
||||
if (id === '') {
|
||||
throw 'no paste id given';
|
||||
}
|
||||
|
||||
return id;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue