-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsri-fallback.js
More file actions
executable file
·71 lines (57 loc) · 1.47 KB
/
sri-fallback.js
File metadata and controls
executable file
·71 lines (57 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Implements non-standard "x-sri-fallback" attribute for use with SRI.
*/
(function () {
var attributeName = 'x-sri-fallback';
function processNode (node) {
var fallback = node.getAttribute && node.getAttribute(attributeName);
var tagName = (node.tagName || '').toLowerCase();
var nodeAttribute =
tagName === 'script' && node.src ?
'src' :
tagName === 'link' && node.rel === 'stylesheet' && node.href ?
'href' :
null
;
if (fallback && nodeAttribute) {
node.onerror = function () {
var newNode = document.createElement(tagName);
newNode.crossOrigin = node.crossOrigin;
newNode.integrity = node.integrity;
newNode[nodeAttribute] = fallback;
if (tagName === 'link') {
newNode.rel = node.rel;
}
newNode.onerror = function () {
console.log(
'SRI fallback ' +
fallback +
' also failed to match integrity string ' +
newNode.integrity +
'.'
);
};
document.head.appendChild(newNode);
};
}
}
new MutationObserver(function (mutations) {
for (var i = 0 ; i < mutations.length ; ++i) {
var mutation = mutations[i];
var addedNodes = mutation.addedNodes;
for (var j = 0 ; j < addedNodes.length ; ++j) {
processNode(addedNodes[j]);
}
if (mutation.attributeName === attributeName) {
processNode(mutation.target);
}
}
}).observe(document, {
childList: true,
attributes: true,
characterData: false,
subtree: true,
attributeOldValue: false,
attributeFilter: [attributeName]
});
}());