

def InjectScript(content, content_type, script_to_inject):
    "Inject |script_to_inject| into |content| if |content_type| is 'text/html'.\n\n  Inject |script_to_inject| into |content| immediately after <head>, <html> or\n  <!doctype html>, if one of them is found. Otherwise, inject at the beginning.\n\n  Returns:\n    content, already_injected\n    |content| is the new content if script is injected, otherwise the original.\n    |already_injected| indicates if |script_to_inject| is already in |content|.\n  "
    already_injected = False
    if (content_type and (content_type == 'text/html')):
        already_injected = ((not content) or (script_to_inject in content))
        if (not already_injected):

            def InsertScriptAfter(matchobj):
                return ('%s<script>%s</script>' % (matchobj.group(0), script_to_inject))
            (content, is_injected) = HEAD_RE.subn(InsertScriptAfter, content, 1)
            if (not is_injected):
                (content, is_injected) = HTML_RE.subn(InsertScriptAfter, content, 1)
            if (not is_injected):
                (content, is_injected) = DOCTYPE_RE.subn(InsertScriptAfter, content, 1)
            if (not is_injected):
                content = ('<script>%s</script>%s' % (script_to_inject, content))
                logging.warning('Inject at the very beginning, because no tag of <head>, <html> or <!doctype html> is found.')
    return (content, already_injected)

dataset/ETHPy150Open chromium/web-page-replay/script_injector.py InjectScript/original