|
23 | 23 |
|
24 | 24 | // Init REGEX |
25 | 25 | const regEx = { |
| 26 | + hash: { commit: /(@|\?v=)([^/#]+)/, sri: /[^#]+$/ }, |
26 | 27 | resName: /[^/]+\/(?:dist)?\/?[^/]+\.js(?=[?#]|$)/, |
27 | | - jsURL: /^\/\/ @require\s+(https:\/\/cdn\.jsdelivr\.net\/gh\/.+)$/, |
28 | | - commitHash: /(@|\?v=)([^/#]+)/, sriHash: /[^#]+$/ |
| 28 | + jsURL: /^\/\/ @require\s+(https:\/\/cdn\.jsdelivr\.net\/gh\/.+)$/ |
29 | 29 | } |
30 | 30 |
|
31 | 31 | // Define FUNCTIONS |
|
125 | 125 | // Compare/update commit hash |
126 | 126 | let resLatestCommitHash = latestCommitHashes[resURL.includes(repoName) ? 'chromium' : 'aiweb'] |
127 | 127 | if (resLatestCommitHash.startsWith( // compare hashes |
128 | | - regEx.commitHash.exec(resURL)?.[2] || '')) { // commit hash didn't change... |
| 128 | + regEx.hash.commit.exec(resURL)?.[2] || '')) { // commit hash didn't change... |
129 | 129 | console.log(`${resName} already up-to-date!`) ; log.endedWithLineBreak = false |
130 | 130 | continue // ...so skip resource |
131 | 131 | } |
132 | 132 | resLatestCommitHash = resLatestCommitHash.substring(0, 7) // abbr it |
133 | | - let updatedURL = resURL.replace(regEx.commitHash, `$1${resLatestCommitHash}`) // update hash |
| 133 | + let updatedURL = resURL.replace(regEx.hash.commit, `$1${resLatestCommitHash}`) // update hash |
134 | 134 | if (!await isValidResource(updatedURL)) continue // to next resource |
135 | 135 |
|
136 | 136 | // Generate/compare/update SRI hash |
137 | 137 | console.log(`${ !log.endedWithLineBreak ? '\n' : '' }Generating SRI (SHA-256) hash for ${resName}...`) |
138 | 138 | const newSRIhash = await generateSRIhash(updatedURL) |
139 | | - if (regEx.sriHash.exec(resURL)?.[0] == newSRIhash) { // SRI hash didn't change |
| 139 | + if (regEx.hash.sri.exec(resURL)?.[0] == newSRIhash) { // SRI hash didn't change |
140 | 140 | console.log(`${resName} already up-to-date!`) ; log.endedWithLineBreak = false |
141 | 141 | continue // ...so skip resource |
142 | 142 | } |
143 | | - updatedURL = updatedURL.replace(regEx.sriHash, newSRIhash) // update hash |
| 143 | + updatedURL = updatedURL.replace(regEx.hash.sri, newSRIhash) // update hash |
144 | 144 | if (!await isValidResource(updatedURL)) continue // to next resource |
145 | 145 |
|
146 | 146 | // Write updated URL to userscript |
|
0 commit comments