async function processJSWithSrc()

in src/js/contentUtils.js [574:628]


async function processJSWithSrc(script, origin, version) {
  // fetch the script from page context, not the extension context.
  try {
    const sourceResponse = await fetch(script.src, { method: 'GET' });
    let sourceText = await sourceResponse.text();
    let fbOrigin = [ORIGIN_TYPE.FACEBOOK].includes(origin);

    if (fbOrigin && sourceText.indexOf('if (self.CavalryLogger) {') === 0) {
      sourceText = sourceText.slice(82).trim();
    }
    // we want to slice out the source URL from the source
    const sourceURLIndex = sourceText.indexOf('//# sourceURL');
    if (sourceURLIndex >= 0) {
      // doing minus 1 because there's usually either a space or new line
      sourceText = sourceText.slice(0, sourceURLIndex - 1);
    }
    // strip i18n delimiters
    // eslint-disable-next-line no-useless-escape
    const i18nRegexp = /\/\*FBT_CALL\*\/.*?\/\*FBT_CALL\*\//g;
    let i18nStripped = sourceText;
    if (fbOrigin) {
      i18nStripped = sourceText.replace(i18nRegexp, '');
    }
    // split package up if necessary
    const packages = i18nStripped.split('/*FB_PKG_DELIM*/\n');
    const packagePromises = packages.map(jsPackage => {
      return new Promise((resolve, reject) => {
        chrome.runtime.sendMessage(
          {
            type: MESSAGE_TYPE.RAW_JS,
            rawjs: jsPackage,
            origin: origin,
            version: version,
          },
          response => {
            if (response.valid) {
              resolve();
            } else {
              reject(response.type);
            }
          }
        );
      });
    });
    await Promise.all(packagePromises);
    return {
      valid: true,
    };
  } catch (scriptProcessingError) {
    return {
      valid: false,
      type: scriptProcessingError,
    };
  }
}