<commit_before>window.FACTLINK_START_LOADER = ->
  unsupported_browser = document.documentMode < 9
  return if unsupported_browser
  if window.FACTLINK_LOADED
    console.error 'FACTLINK already loaded!'
    return
  window.FACTLINK_LOADED = true

  window.FACTLINK = {}

  # Create proxy object that stores all calls
  # proxies calls from external content page to the js-library "jail" iframe's "FactlinkJailRoot"..
  methods = 'on,triggerClick,startHighlighting,highlightAdditionalFactlinks,startAnnotating,stopAnnotating,showLoadedNotification,scrollTo,openFactlinkModal,initializeFactlinkButton'.split(',')

  storedMethodCalls = []

  storedMethodFactory = (name) ->
    -> storedMethodCalls.push(name: name, arguments: arguments) && undefined

  for name in methods
    window.FACTLINK[name] =
      if unsupported_browser
        ->
      else
        storedMethodFactory name

  mkEl = (name, id, content) ->
    el = document.createElement(name)
    id && el.id = id
    content && el.appendChild(content)
    el

  # Add styles
  style_tag = mkEl 'style', null, document.createTextNode(style_code)
  document.getElementsByTagName('head')[0].appendChild style_tag

  #### Create iframe so jslib's namespace (window) doesn't collide with any content window.
  jslib_jail_iframe = mkEl 'iframe', 'factlink-iframe'

  # Wrapper for increased CSS specificity
  outerWrapperEl = mkEl 'div', 'factlink-containment-wrapper', jslib_jail_iframe

  document.body.appendChild outerWrapperEl

  window.FACTLINK_ON_CORE_LOAD = ->
    for name in methods
      do (name) ->
        # don't return the value, as we can't do that when storing calls
        window.FACTLINK[name] = ->
          jslib_jail_iframe.contentWindow.FactlinkJailRoot[name](arguments...)
          return

    delete window.FACTLINK_ON_CORE_LOAD

    for methodCall in storedMethodCalls
      window.FACTLINK[methodCall.name](methodCall.arguments...)
    storedMethodCalls = undefined

  jail_window = jslib_jail_iframe.contentWindow
  jail_window.FactlinkConfig = window.FactlinkConfig
  jail_window.FrameCss = frame_style_code

  #### Load iframe with script tag
  jslib_jail_doc = jail_window.document

  jslib_jail_doc.open()
  jslib_jail_doc.write '<!DOCTYPE html><title></title>'
  jslib_jail_doc.close()

  script_tag = jslib_jail_doc.createElement 'script'
  script_tag.appendChild(jslib_jail_doc.createTextNode(jslib_jail_code))
  jslib_jail_doc.documentElement.appendChild(script_tag)


jslib_jail_code = __INLINE_JS_PLACEHOLDER__
style_code = __INLINE_CSS_PLACEHOLDER__
frame_style_code = __INLINE_FRAME_CSS_PLACEHOLDER__


<commit_msg>Don't initialize js-lib until document.readyState is interactive.<commit_after>window.FACTLINK_START_LOADER = ->
  unsupported_browser = document.documentMode < 9
  return if unsupported_browser
  if window.FACTLINK_LOADED
    console.error 'FACTLINK already loaded!'
    return
  window.FACTLINK_LOADED = true

  window.FACTLINK = {}

  # Create proxy object that stores all calls
  # proxies calls from external content page to the js-library "jail" iframe's "FactlinkJailRoot"..
  methods = 'on,triggerClick,startHighlighting,highlightAdditionalFactlinks,startAnnotating,stopAnnotating,showLoadedNotification,scrollTo,openFactlinkModal,initializeFactlinkButton'.split(',')

  storedMethodCalls = []

  storedMethodFactory = (name) ->
    -> storedMethodCalls.push(name: name, arguments: arguments) && undefined

  for name in methods
    window.FACTLINK[name] =
      if unsupported_browser
        ->
      else
        storedMethodFactory name

  mkEl = (name, id, content) ->
    el = document.createElement(name)
    id && el.id = id
    content && el.appendChild(content)
    el

  onReady = ->
    # Add styles
    style_tag = mkEl 'style', null, document.createTextNode(style_code)
    document.getElementsByTagName('head')[0].appendChild style_tag

    #### Create iframe so jslib's namespace (window) doesn't collide with any content window.
    jslib_jail_iframe = mkEl 'iframe', 'factlink-iframe'

    # Wrapper for increased CSS specificity
    outerWrapperEl = mkEl 'div', 'factlink-containment-wrapper', jslib_jail_iframe

    document.body.appendChild outerWrapperEl

    window.FACTLINK_ON_CORE_LOAD = -> #called from jail-iframe when core iframe is ready.
      for name in methods
        do (name) ->
          # don't return the value, as we can't do that when storing calls
          window.FACTLINK[name] = ->
            jslib_jail_iframe.contentWindow.FactlinkJailRoot[name](arguments...)
            return

      delete window.FACTLINK_ON_CORE_LOAD

      for methodCall in storedMethodCalls
        window.FACTLINK[methodCall.name](methodCall.arguments...)
      storedMethodCalls = undefined

    jail_window = jslib_jail_iframe.contentWindow
    jail_window.FactlinkConfig = window.FactlinkConfig
    jail_window.FrameCss = frame_style_code

    #### Load iframe with script tag
    jslib_jail_doc = jail_window.document

    jslib_jail_doc.open()
    jslib_jail_doc.write '<!DOCTYPE html><title></title>'
    jslib_jail_doc.close()

    script_tag = jslib_jail_doc.createElement 'script'
    script_tag.appendChild(jslib_jail_doc.createTextNode(jslib_jail_code))
    jslib_jail_doc.documentElement.appendChild(script_tag)

  if /^(interactive|complete)$/.test(document.readyState)
    onReady()
  else
    document.addEventListener('DOMContentLoaded',onReady);


jslib_jail_code = __INLINE_JS_PLACEHOLDER__
style_code = __INLINE_CSS_PLACEHOLDER__
frame_style_code = __INLINE_FRAME_CSS_PLACEHOLDER__



{'fraction_non_alphanumeric': 0.08612612612612612, 'fraction_numerical': 0.0007207207207207207, 'mean_word_length': 4.192703461178672, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404844', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1721, 'n_words': 350}