| import rescape from '@stdlib/utils-escape-regexp-string'; |
| import {correctArrayKeys} from './protojs'; |
| export function parseView(viewVar) { |
| const escapedViewVar = rescape(viewVar); |
| const viewRegex = new RegExp(`var ${escapedViewVar} ?= ?'([^']+)';`); |
| const scripts = document.querySelectorAll('script'); |
| for (let i = 0; i < scripts.length; ++i) { |
| const matches = scripts[i].textContent.match(viewRegex); |
| let rawJsonStringContents = |
| return String.fromCharCode(parseInt(pair, 16)); |
| let rawJsonString = `"${rawJsonStringContents}"`; |
| let rawJson = JSON.parse(rawJsonString); |
| viewData = JSON.parse(rawJson); |
| if (!viewData) throw new Error(`Could not find ${viewVar} view data.`); |
| return correctArrayKeys(viewData); |