|
81 | 81 | // Get URL of the raw file |
82 | 82 | const rawFileUrl = getRawFileUrl(); |
83 | 83 |
|
84 | | - const replaceAssets = function () { |
85 | | - let i, href, src; |
86 | | - const links = []; |
87 | | - const scripts = []; |
88 | | - // Framesets |
89 | | - if (document.querySelectorAll('frameset').length) { |
90 | | - // Don't replace CSS/JS if it's a frameset, |
91 | | - // because it will be erased by document.write() |
92 | | - return; |
93 | | - } |
94 | | - // Frames |
| 84 | + const replaceFrames = function () { |
95 | 85 | const frame = document.querySelectorAll('iframe[src],frame[src]'); |
96 | | - for (i = 0; i < frame.length; ++i) { |
| 86 | + for (let i = 0; i < frame.length; ++i) { |
97 | 87 | rewriteCond(frame[i], "src"); |
98 | 88 | } |
99 | | - // Objects |
| 89 | + }; |
| 90 | + |
| 91 | + const replaceObjects = function () { |
100 | 92 | const object = document.querySelectorAll('object[data]'); |
101 | | - for (i = 0; i < object.length; ++i) { |
| 93 | + for (let i = 0; i < object.length; ++i) { |
102 | 94 | rewriteCond(object[i], "data"); |
103 | 95 | } |
104 | | - // Links |
| 96 | + }; |
| 97 | + |
| 98 | + const replaceLinks = function () { |
105 | 99 | const a = document.querySelectorAll('a[href]'); |
106 | | - for (i = 0; i < a.length; ++i) { |
| 100 | + let href; |
| 101 | + for (let i = 0; i < a.length; ++i) { |
107 | 102 | // Get absolute URL |
108 | 103 | href = a[i].href; |
109 | 104 | // Check if it's an anchor |
110 | 105 | if (href.indexOf('#') > 0) { |
111 | 106 | // Rewrite links to this document only |
112 | 107 | if ((a[i].protocol + '//' + a[i].hostname + a[i].pathname) == rawFileUrl) { |
113 | 108 | // Then rewrite URL with support for empty anchor |
114 | | - a[i].href = |
115 | | - location.origin + location.pathname + location.search |
| 109 | + a[i].href |
| 110 | + = location.origin + location.pathname + location.search |
116 | 111 | + '#' + a[i].hash.substring(1); |
117 | 112 | } |
118 | 113 | // Do not modify external URLs with fragment |
|
121 | 116 | a[i].href = rewrite(href); |
122 | 117 | } |
123 | 118 | } |
124 | | - // Stylesheets |
| 119 | + }; |
| 120 | + |
| 121 | + const replaceStylesheets = function () { |
125 | 122 | const link = document.querySelectorAll('link[rel=stylesheet]'); |
126 | | - for (i = 0; i < link.length; ++i) { |
| 123 | + const links = []; |
| 124 | + let href; |
| 125 | + for (let i = 0; i < link.length; ++i) { |
127 | 126 | // Get absolute URL |
128 | 127 | href = link[i].href; |
129 | 128 | if (isGitForgeFileUrl(href)) { |
|
132 | 131 | } |
133 | 132 | } |
134 | 133 | Promise.all(links).then(function (res) { |
135 | | - for (i = 0; i < res.length; ++i) { |
| 134 | + for (let i = 0; i < res.length; ++i) { |
136 | 135 | loadCSS(res[i]); |
137 | 136 | } |
138 | 137 | }); |
139 | | - // Scripts |
140 | | - const script = document.querySelectorAll( |
141 | | - 'script[type="text/htmlpreview"]' |
142 | | - ); |
143 | | - for (i = 0; i < script.length; ++i) { |
| 138 | + }; |
| 139 | + |
| 140 | + const replaceScripts = function () { |
| 141 | + // eslint-disable-next-line max-len |
| 142 | + const script = document.querySelectorAll('script[type="text/htmlpreview"]'); |
| 143 | + const scripts = []; |
| 144 | + let src; |
| 145 | + for (let i = 0; i < script.length; ++i) { |
144 | 146 | // Get absolute URL |
145 | 147 | src = script[i].src; |
146 | 148 | if (isGitForgeFileUrl(src)) { |
|
153 | 155 | } |
154 | 156 | } |
155 | 157 | Promise.all(scripts).then(function (res) { |
156 | | - for (i = 0; i < res.length; ++i) { |
| 158 | + for (let i = 0; i < res.length; ++i) { |
157 | 159 | loadJS(res[i]); |
158 | 160 | } |
159 | 161 | // Dispatch DOMContentLoaded event after loading all scripts |
|
164 | 166 | }); |
165 | 167 | }; |
166 | 168 |
|
| 169 | + const replaceAssets = function () { |
| 170 | + // Framesets |
| 171 | + if (document.querySelectorAll('frameset').length) { |
| 172 | + // Don't replace CSS/JS if it's a frameset, |
| 173 | + // because it will be erased by document.write() |
| 174 | + return; |
| 175 | + } |
| 176 | + replaceFrames(); |
| 177 | + replaceObjects(); |
| 178 | + replaceLinks(); |
| 179 | + replaceStylesheets(); |
| 180 | + replaceScripts(); |
| 181 | + }; |
| 182 | + |
167 | 183 | const loadHTML = function (data) { |
168 | 184 | if (data) { |
169 | 185 | // Add <base> just after <head> |
|
0 commit comments