Skip to content

Commit fa3c83f

Browse files
committed
feat: getAttribute prototype to handle operator values
1 parent cc7519b commit fa3c83f

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/getAttribute.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Store a reference to the original getAttribute function
2+
const originalGetAttribute = Element.prototype.getAttribute;
3+
4+
// Override the getAttribute function
5+
Element.prototype.getAttribute = function (name) {
6+
let value = originalGetAttribute.call(this, name);
7+
8+
if (value === '$organization_id')
9+
value = localStorage.getItem('organization_id')
10+
else if (value === '$user_id')
11+
value = localStorage.getItem('user_id')
12+
else if (value === '$clientId')
13+
value = localStorage.getItem('clientId')
14+
else if (value === '$session_id')
15+
value = localStorage.getItem('session_id')
16+
else if (typeof value === 'string') {
17+
if (value.startsWith('$search')) {
18+
const searchParams = new URLSearchParams(window.location.search);
19+
if (value.includes('.')) {
20+
value = searchParams.get(value.split('.')[1]);
21+
} else {
22+
const paramsObject = {};
23+
24+
// Iterate over all key-value pairs and add them to the object
25+
for (const [key, value] of searchParams) {
26+
paramsObject[key] = value;
27+
}
28+
value = paramsObject
29+
}
30+
31+
} else if ([
32+
'$href',
33+
'$origin',
34+
'$protocol',
35+
'$host',
36+
'$hostname',
37+
'$port',
38+
'$pathname',
39+
'$hash'
40+
].includes(value)) {
41+
value = window.location[value.substring(1)]
42+
}
43+
}
44+
45+
return value;
46+
};
47+

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222

2323
import { setValue } from './setValue';
2424
import { getValue } from './getValue';
25+
import { getAttribute } from './getAttribute';
2526

26-
27-
export default { getValue, setValue }
27+
export default { getValue, setValue, getAttribute }

0 commit comments

Comments
 (0)