# All files / utils browser.ts

63.89% Statements 46/72
45.33% Branches 34/75
75% Functions 3/4
63.77% Lines 44/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    1618x
    8x
    8x

    8x
    2x
    2x
    1x

    1x
    1x
    1x
    1x
    1x

    8x

    2x
    1x

    1x

    1x

    1x
    1x
    1x

    8x

    8x
    1x

    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x

    1x
    8x
    4x
    4x
    4x

    4x

    4x

    4x

    4x

    4x

    4x

    1x

    1x

     import { EVENTTYPES, ERRORTYPES } from '../common/constant'
    import { getLocationHref, getTimestamp } from './helpers'
    import { setFlag } from './global'
    import { ReportDataType, InitOptions } from '../types/index'
    import { Severity } from './Severity'

    /**
     * 返回包含id、class、innerTextde字符串的标签
     * @param target html节点
     */
    export function htmlElementAsString(target: HTMLElement): string {
      const tagName = target.tagName.toLowerCase()
      if (tagName === 'body') {
        return null
      }
      let classNames = target.classList.value
      classNames = classNames !== '' ? ` class="${classNames}"` : ''
      const id = target.id ? ` id="${target.id}"` : ''
      const innerText = target.innerText
      return `<${tagName}${id}${classNames !== '' ? classNames : ''}>${innerText}</${tagName}>`
    }

    /**
     * 将地址字符串转换成对象
     * @returns 返回一个对象
     */
    export function parseUrlToObj(
      url: string
    ): {
      host?: string
      path?: string
      protocol?: string
      relative?: string
    } {
      if (!url) {
        return {}
      }

      const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/)

      Iif (!match) {
        return {}
      }

      const query = match[6] || ''
      const fragment = match[8] || ''
      return {
        host: match[4],
        path: match[5],
        protocol: match[2],
        relative: match[5] + query + fragment // everything minus origin
      }
    }

    export function setSilentFlag(opitons: InitOptions = {}): void {
      setFlag(EVENTTYPES.XHR, !!opitons.silentXhr)
      setFlag(EVENTTYPES.FETCH, !!opitons.silentFetch)
      setFlag(EVENTTYPES.CONSOLE, !!opitons.silentConsole)
      setFlag(EVENTTYPES.DOM, !!opitons.silentDom)
      setFlag(EVENTTYPES.HISTORY, !!opitons.silentHistory)
      setFlag(EVENTTYPES.ERROR, !!opitons.silentError)
      setFlag(EVENTTYPES.HASHCHANGE, !!opitons.silentHashchange)
      setFlag(EVENTTYPES.UNHANDLEDREJECTION, !!opitons.silentUnhandledrejection)
      setFlag(EVENTTYPES.VUE, !!opitons.silentVue)
    }

    /**
     * 解析error的stack，并返回args、column、line、func、url:
     * @param ex
     * @param level
     */
    export function extractErrorStack(ex: any, level: Severity): ReportDataType {
      const normal = {
        time: getTimestamp(),
        url: getLocationHref(),
        name: ex.name,
        level,
        message: ex.message
      }
      Iif (typeof ex.stack === 'undefined' || !ex.stack) {
        return normal
      }

      const chrome = /^\s*at (.*?) ?\(((?:file|https?|blob|chrome-extension|native|eval|webpack|<anonymous>|[a-z]:|\/).*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i,
        gecko = /^\s*(.*?)(?:\((.*?)\))?(?:^|@)((?:file|https?|blob|chrome|webpack|resource|\[native).*?|[^@]*bundle)(?::(\d+))?(?::(\d+))?\s*$/i,
        winjs = /^\s*at (?:((?:\[object object\])?.+) )?\(?((?:file|ms-appx|https?|webpack|blob):.*?):(\d+)(?::(\d+))?\)?\s*$/i,
        // Used to additionally parse URL/line/column from eval frames
        geckoEval = /(\S+) line (\d+)(?: > eval line \d+)* > eval/i,
        chromeEval = /\((\S*)(?::(\d+))(?::(\d+))\)/,
        lines = ex.stack.split('\n'),
        stack = []

      let submatch, parts, element
      // reference = /^(.*) is undefined$/.exec(ex.message)

      for (let i = 0, j = lines.length; i < j; ++i) {
        if ((parts = chrome.exec(lines[i]))) {
          const isNative = parts[2] && parts[2].indexOf('native') === 0 // start of line
          const isEval = parts[2] && parts[2].indexOf('eval') === 0 // start of line
          Iif (isEval && (submatch = chromeEval.exec(parts[2]))) {
            // throw out eval line/column and use top-most line/column number
            parts[2] = submatch[1] // url
            parts[3] = submatch[2] // line
            parts[4] = submatch[3] // column
          }
          element = {
            url: !isNative ? parts[2] : null,
            func: parts[1] || ERRORTYPES.UNKNOWN_FUNCTION,
            args: isNative ? [parts[2]] : [],
            line: parts[3] ? +parts[3] : null,
            column: parts[4] ? +parts[4] : null
          }
        } else Iif ((parts = winjs.exec(lines[i]))) {
          element = {
            url: parts[2],
            func: parts[1] || ERRORTYPES.UNKNOWN_FUNCTION,
            args: [],
            line: +parts[3],
            column: parts[4] ? +parts[4] : null
          }
        } else Iif ((parts = gecko.exec(lines[i]))) {
          const isEval = parts[3] && parts[3].indexOf(' > eval') > -1
          if (isEval && (submatch = geckoEval.exec(parts[3]))) {
            // throw out eval line/coluqqqqqqqqqqqqqqqqqqqqqqqqqqqqmn and use top-most line number
            parts[3] = submatch[1]
            parts[4] = submatch[2]
            parts[5] = null // no column when eval
          } else if (i === 0 && !parts[5] && typeof ex.columnNumber !== 'undefined') {
            // FireFox uses this awesome columnNumber property for its top frame
            // Also note, Firefox's column number is 0-based and everything else expects 1-based,
            // so adding 1
            // NOTE: this hack doesn't work if top-most frame is eval
            stack[0].column = ex.columnNumber + 1
          }
          element = {
            url: parts[3],
            func: parts[1] || ERRORTYPES.UNKNOWN_FUNCTION,
            args: parts[2] ? parts[2].split(',') : [],
            line: parts[4] ? +parts[4] : null,
            column: parts[5] ? +parts[5] : null
          }
        } else {
          continue
        }

        Iif (!element.func && element.line) {
          element.func = ERRORTYPES.UNKNOWN_FUNCTION
        }

        stack.push(element)
      }

      Iif (!stack.length) {
        return null
      }
      return {
        ...normal,
        stack: stack
      }
    }