@searchspring/snap-toolbox 0.73.6 → 0.73.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/getContext/getContext.d.ts +1 -1
- package/dist/cjs/getContext/getContext.d.ts.map +1 -1
- package/dist/cjs/getContext/getContext.js +18 -17
- package/dist/esm/getContext/getContext.d.ts +1 -1
- package/dist/esm/getContext/getContext.d.ts.map +1 -1
- package/dist/esm/getContext/getContext.js +14 -13
- package/package.json +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type ContextVariables = {
|
|
2
2
|
[variable: string]: any;
|
|
3
3
|
};
|
|
4
|
-
export declare function getContext(evaluate?: string[],
|
|
4
|
+
export declare function getContext(evaluate?: string[], scriptOrSelector?: HTMLScriptElement | string): ContextVariables;
|
|
5
5
|
export {};
|
|
6
6
|
//# sourceMappingURL=getContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAgDF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAgDF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,gBAAgB,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAsGnH"}
|
|
@@ -57,11 +57,12 @@ var JAVASCRIPT_KEYWORDS = new Set([
|
|
|
57
57
|
'private',
|
|
58
58
|
'public',
|
|
59
59
|
]);
|
|
60
|
-
function getContext(evaluate,
|
|
61
|
-
var _a, _b, _c, _d
|
|
60
|
+
function getContext(evaluate, scriptOrSelector) {
|
|
61
|
+
var _a, _b, _c, _d;
|
|
62
62
|
if (evaluate === void 0) { evaluate = []; }
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
var script;
|
|
64
|
+
if (!scriptOrSelector || typeof scriptOrSelector === 'string') {
|
|
65
|
+
var scripts = Array.from(document.querySelectorAll(scriptOrSelector || 'script[id^=searchspring], script[src*="snapui.searchspring.io"]'));
|
|
65
66
|
script = scripts
|
|
66
67
|
.sort(function (a, b) {
|
|
67
68
|
// order them by innerHTML (so that popped script has innerHTML)
|
|
@@ -69,15 +70,15 @@ function getContext(evaluate, script) {
|
|
|
69
70
|
})
|
|
70
71
|
.pop();
|
|
71
72
|
}
|
|
72
|
-
if (
|
|
73
|
+
else if (scriptOrSelector && scriptOrSelector.tagName === 'SCRIPT') {
|
|
74
|
+
// script is a 'script element'
|
|
75
|
+
script = scriptOrSelector;
|
|
76
|
+
}
|
|
77
|
+
if (!script) {
|
|
73
78
|
throw new Error('getContext: did not find a script tag');
|
|
74
79
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (!((_a = scriptElem.getAttribute('type')) === null || _a === void 0 ? void 0 : _a.match(/^searchspring/i)) &&
|
|
78
|
-
!((_b = scriptElem.id) === null || _b === void 0 ? void 0 : _b.match(/^searchspring/i)) &&
|
|
79
|
-
!((_c = scriptElem.src) === null || _c === void 0 ? void 0 : _c.match(/\/\/snapui.searchspring.io/i))) {
|
|
80
|
-
throw new Error('getContext: did not find a script from Snap CDN or with attribute (type, id) starting with "searchspring"');
|
|
80
|
+
if (!scriptOrSelector && !((_a = script.id) === null || _a === void 0 ? void 0 : _a.match(/^searchspring/i)) && !((_b = script.src) === null || _b === void 0 ? void 0 : _b.match(/\/\/snapui.searchspring.io/i))) {
|
|
81
|
+
throw new Error('getContext: did not find a script from Snap CDN or with attribute \'id\' starting with "searchspring"');
|
|
81
82
|
}
|
|
82
83
|
if ((evaluate && !Array.isArray(evaluate)) || (evaluate && !evaluate.reduce(function (accu, name) { return accu && typeof name === 'string'; }, true))) {
|
|
83
84
|
throw new Error('getContext: first parameter must be an array of strings');
|
|
@@ -85,20 +86,20 @@ function getContext(evaluate, script) {
|
|
|
85
86
|
var siteIdString = 'siteId';
|
|
86
87
|
var attributeVariables = {};
|
|
87
88
|
// grab element attributes and put into variables
|
|
88
|
-
Object.values(
|
|
89
|
+
Object.values(script === null || script === void 0 ? void 0 : script.attributes).map(function (attr) {
|
|
89
90
|
var name = attr.nodeName;
|
|
90
91
|
if (evaluate.includes(name)) {
|
|
91
|
-
attributeVariables[name] =
|
|
92
|
+
attributeVariables[name] = script === null || script === void 0 ? void 0 : script.getAttribute(name);
|
|
92
93
|
}
|
|
93
94
|
});
|
|
94
95
|
var scriptVariables = {};
|
|
95
|
-
var scriptInnerHTML =
|
|
96
|
+
var scriptInnerHTML = script === null || script === void 0 ? void 0 : script.innerHTML;
|
|
96
97
|
// attempt to grab inner HTML variables
|
|
97
|
-
var scriptInnerVars = (
|
|
98
|
+
var scriptInnerVars = (_c = scriptInnerHTML
|
|
98
99
|
// first remove all string literals (including template literals) to avoid false matches
|
|
99
100
|
.replace(/`(?:\\[\s\S]|[^`\\])*`|'(?:\\[\s\S]|[^'\\])*'|"(?:\\[\s\S]|[^"\\])*"/g, '')
|
|
100
101
|
// then find variable assignments
|
|
101
|
-
.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s*=/g)) === null ||
|
|
102
|
+
.match(/([a-zA-Z_$][a-zA-Z_$0-9]*)\s*=/g)) === null || _c === void 0 ? void 0 : _c.map(function (match) { return match.replace(/[\s=]/g, ''); });
|
|
102
103
|
var combinedVars = evaluate.concat(scriptInnerVars || []);
|
|
103
104
|
// de-dupe vars
|
|
104
105
|
var evaluateVars = combinedVars.filter(function (item, index) {
|
|
@@ -129,7 +130,7 @@ function getContext(evaluate, script) {
|
|
|
129
130
|
if (evaluate.includes(siteIdString)) {
|
|
130
131
|
// if we didnt find a siteId in the context, lets grab the id from the src url.
|
|
131
132
|
if (!variables[siteIdString]) {
|
|
132
|
-
var siteId = (
|
|
133
|
+
var siteId = (_d = script.getAttribute('src')) === null || _d === void 0 ? void 0 : _d.match(/.*snapui.searchspring.io\/([a-zA-Z0-9]{6})\//);
|
|
133
134
|
if (siteId && siteId.length > 1) {
|
|
134
135
|
variables.siteId = siteId[1];
|
|
135
136
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type ContextVariables = {
|
|
2
2
|
[variable: string]: any;
|
|
3
3
|
};
|
|
4
|
-
export declare function getContext(evaluate?: string[],
|
|
4
|
+
export declare function getContext(evaluate?: string[], scriptOrSelector?: HTMLScriptElement | string): ContextVariables;
|
|
5
5
|
export {};
|
|
6
6
|
//# sourceMappingURL=getContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAgDF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAgDF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,gBAAgB,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAsGnH"}
|
|
@@ -43,9 +43,10 @@ const JAVASCRIPT_KEYWORDS = new Set([
|
|
|
43
43
|
'private',
|
|
44
44
|
'public',
|
|
45
45
|
]);
|
|
46
|
-
export function getContext(evaluate = [],
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
export function getContext(evaluate = [], scriptOrSelector) {
|
|
47
|
+
let script;
|
|
48
|
+
if (!scriptOrSelector || typeof scriptOrSelector === 'string') {
|
|
49
|
+
const scripts = Array.from(document.querySelectorAll(scriptOrSelector || 'script[id^=searchspring], script[src*="snapui.searchspring.io"]'));
|
|
49
50
|
script = scripts
|
|
50
51
|
.sort((a, b) => {
|
|
51
52
|
// order them by innerHTML (so that popped script has innerHTML)
|
|
@@ -53,15 +54,15 @@ export function getContext(evaluate = [], script) {
|
|
|
53
54
|
})
|
|
54
55
|
.pop();
|
|
55
56
|
}
|
|
56
|
-
if (
|
|
57
|
+
else if (scriptOrSelector && scriptOrSelector.tagName === 'SCRIPT') {
|
|
58
|
+
// script is a 'script element'
|
|
59
|
+
script = scriptOrSelector;
|
|
60
|
+
}
|
|
61
|
+
if (!script) {
|
|
57
62
|
throw new Error('getContext: did not find a script tag');
|
|
58
63
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (!scriptElem.getAttribute('type')?.match(/^searchspring/i) &&
|
|
62
|
-
!scriptElem.id?.match(/^searchspring/i) &&
|
|
63
|
-
!scriptElem.src?.match(/\/\/snapui.searchspring.io/i)) {
|
|
64
|
-
throw new Error('getContext: did not find a script from Snap CDN or with attribute (type, id) starting with "searchspring"');
|
|
64
|
+
if (!scriptOrSelector && !script.id?.match(/^searchspring/i) && !script.src?.match(/\/\/snapui.searchspring.io/i)) {
|
|
65
|
+
throw new Error('getContext: did not find a script from Snap CDN or with attribute \'id\' starting with "searchspring"');
|
|
65
66
|
}
|
|
66
67
|
if ((evaluate && !Array.isArray(evaluate)) || (evaluate && !evaluate.reduce((accu, name) => accu && typeof name === 'string', true))) {
|
|
67
68
|
throw new Error('getContext: first parameter must be an array of strings');
|
|
@@ -69,14 +70,14 @@ export function getContext(evaluate = [], script) {
|
|
|
69
70
|
const siteIdString = 'siteId';
|
|
70
71
|
const attributeVariables = {};
|
|
71
72
|
// grab element attributes and put into variables
|
|
72
|
-
Object.values(
|
|
73
|
+
Object.values(script?.attributes).map((attr) => {
|
|
73
74
|
const name = attr.nodeName;
|
|
74
75
|
if (evaluate.includes(name)) {
|
|
75
|
-
attributeVariables[name] =
|
|
76
|
+
attributeVariables[name] = script?.getAttribute(name);
|
|
76
77
|
}
|
|
77
78
|
});
|
|
78
79
|
const scriptVariables = {};
|
|
79
|
-
const scriptInnerHTML =
|
|
80
|
+
const scriptInnerHTML = script?.innerHTML;
|
|
80
81
|
// attempt to grab inner HTML variables
|
|
81
82
|
const scriptInnerVars = scriptInnerHTML
|
|
82
83
|
// first remove all string literals (including template literals) to avoid false matches
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@searchspring/snap-toolbox",
|
|
3
|
-
"version": "0.73.
|
|
3
|
+
"version": "0.73.7",
|
|
4
4
|
"description": "Snap Toolbox",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -23,5 +23,5 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"dist/**/*"
|
|
25
25
|
],
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "8da3a6b990f69a5cea07aa4570db108a83952f4c"
|
|
27
27
|
}
|