Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created November 19, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfreear/afd3b2119ed3d633e608f52a1cbc0f2f to your computer and use it in GitHub Desktop.
Save nfreear/afd3b2119ed3d633e608f52a1cbc0f2f to your computer and use it in GitHub Desktop.
'New' Accessify Wiki Javascript!
/*!
'New' Accessify Wiki Javascript!
© Nick Freear, 19-November-2018.
*/
const TEST_URL = 'https://www.google.co.uk/search?q=Allison+Stokke';
const INDEX_JSON = 'https://accessifywiki.github.io/fix/index.json';
const FIX_REGEX = /<script.+json['"]>(.+?)<\/script>/ms;
const NOT_FOUND = 'aw-fix-not-found';
fetch(INDEX_JSON)
.then(response => response.json())
.then(jsonData => {
console.warn('Accessify Wiki:', jsonData);
const INDEX = jsonData.fix_index;
for (let pattern in INDEX) {
// INDEX.forEach((pattern, v) => {
const REGEX = new RegExp(pattern.replace(/(\*\.|\*)/, '.*'));
console.warn('AW regex:', REGEX, REGEX.test(TEST_URL), INDEX[ pattern ]);
if (REGEX.test(TEST_URL)) {
const FIX_URL = jsonData.fix_url.replace(jsonData.fix_url_pattern, INDEX[ pattern ]); // + '?r=' + Math.random();
console.warn('AW url:', FIX_URL);
return fetch(FIX_URL.replace('http:', 'https:')); // HACK!
}
// });
}
throw new Error(NOT_FOUND); // NOT_REGEX.source); // 'fix-not-found'); // HACK!
})
.then(response => response.text())
.then(html => {
const RES = html.match(FIX_REGEX);
if (RES) {
console.warn('Fix JSON:', JSON.parse(RES[ 1 ]));
console.warn('...');
} else {
console.error('Error. No fix JSON. Regex/ HTML problem?!');
}
})
.catch(error => {
if (RegExp(NOT_FOUND).test(error)) {
console.warn('Accessify Wiki warning: fix not found');
} else {
console.error('Accessify Wiki error.', error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment