示例#1
0
        /// <summary>
        /// Adds a <c><![CDATA[<link rel="stylesheet">]]></c> tag into the page with the desired url or a <c><![CDATA[<link rel="stylesheet">]]></c> tag with the content
        /// </summary>
        /// <param name="options">add style tag options</param>
        /// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
        /// <seealso cref="Page.AddStyleTagAsync(AddTagOptions)"/>
        /// <seealso cref="Page.AddStyleTagAsync(string)"/>
        public async Task <ElementHandle> AddStyleTag(AddTagOptions options)
        {
            const string addStyleUrl     = @"async function addStyleUrl(url) {
              const link = document.createElement('link');
              link.rel = 'stylesheet';
              link.href = url;
              const promise = new Promise((res, rej) => {
                link.onload = res;
                link.onerror = rej;
              });
              document.head.appendChild(link);
              await promise;
              return link;
            }";
            const string addStyleContent = @"async function addStyleContent(content) {
              const style = document.createElement('style');
              style.type = 'text/css';
              style.appendChild(document.createTextNode(content));
              const promise = new Promise((res, rej) => {
                style.onload = res;
                style.onerror = rej;
              });
              document.head.appendChild(style);
              await promise;
              return style;
            }";

            if (!string.IsNullOrEmpty(options.Url))
            {
                var url = options.Url;
                try
                {
                    var context = await GetExecutionContextAsync();

                    return((await context.EvaluateFunctionHandleAsync(addStyleUrl, url)) as ElementHandle);
                }
                catch (PuppeteerException)
                {
                    throw new PuppeteerException($"Loading style from {url} failed");
                }
            }

            if (!string.IsNullOrEmpty(options.Path))
            {
                var contents = File.ReadAllText(options.Path, Encoding.UTF8);
                contents += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
                var context = await GetExecutionContextAsync();

                return((await context.EvaluateFunctionHandleAsync(addStyleContent, contents)) as ElementHandle);
            }

            if (!string.IsNullOrEmpty(options.Content))
            {
                var context = await GetExecutionContextAsync();

                return((await context.EvaluateFunctionHandleAsync(addStyleContent, options.Content)) as ElementHandle);
            }

            throw new ArgumentException("Provide options with a `Url`, `Path` or `Content` property");
        }
示例#2
0
        /// <summary>
        /// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
        /// </summary>
        /// <param name="options">add script tag options</param>
        /// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
        /// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
        /// <seealso cref="Page.AddScriptTagAsync(string)"/>
        public Task <ElementHandle> AddScriptTagAsync(AddTagOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(MainWorld.AddScriptTagAsync(options));
        }
示例#3
0
        /// <summary>
        /// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
        /// </summary>
        /// <param name="options">add script tag options</param>
        /// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
        /// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
        /// <seealso cref="Page.AddScriptTagAsync(string)"/>
        public async Task <ElementHandle> AddScriptTag(AddTagOptions options)
        {
            const string addScriptUrl     = @"async function addScriptUrl(url) {
              const script = document.createElement('script');
              script.src = url;
              document.head.appendChild(script);
              await new Promise((res, rej) => {
                script.onload = res;
                script.onerror = rej;
              });
              return script;
            }";
            const string addScriptContent = @"function addScriptContent(content) {
              const script = document.createElement('script');
              script.type = 'text/javascript';
              script.text = content;
              document.head.appendChild(script);
              return script;
            }";

            if (!string.IsNullOrEmpty(options.Url))
            {
                var url = options.Url;
                try
                {
                    var context = await GetExecutionContextAsync();

                    return((await context.EvaluateFunctionHandleAsync(addScriptUrl, url)) as ElementHandle);
                }
                catch (PuppeteerException)
                {
                    throw new PuppeteerException($"Loading script from {url} failed");
                }
            }

            if (!string.IsNullOrEmpty(options.Path))
            {
                var contents = File.ReadAllText(options.Path, Encoding.UTF8);
                contents += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
                var context = await GetExecutionContextAsync();

                return((await context.EvaluateFunctionHandleAsync(addScriptContent, contents)) as ElementHandle);
            }

            if (!string.IsNullOrEmpty(options.Content))
            {
                var context = await GetExecutionContextAsync();

                return((await context.EvaluateFunctionHandleAsync(addScriptContent, options.Content)) as ElementHandle);
            }

            throw new ArgumentException("Provide options with a `Url`, `Path` or `Content` property");
        }
示例#4
0
        internal async Task <ElementHandle> AddStyleTag(AddTagOptions options)
        {
            const string addStyleUrl = @"async (url) => {
                const link = document.createElement('link');
                link.rel = 'stylesheet';
                link.href = url;
                document.head.appendChild(link);
                await new Promise((res, rej) => {
                    link.onload = res;
                    link.onerror = rej;
                });
                return link;
            }";

            const string addStyleContent = @"(content) => {
                const style = document.createElement('style');
                style.type = 'text/css';
                style.appendChild(document.createTextNode(content));
                document.head.appendChild(style);
                return style;
            }";

            if (options.Url != null)
            {
                try {
                    return(await EvaluateFunctionAsync <ElementHandle>(addStyleUrl, options.Url));
                } catch (PuppeteerException) {
                    throw new PuppeteerException($"Loading style from {options.Url} failed");
                }
            }

            if (options.Path != null)
            {
                var contents = File.ReadAllText(options.Path, Encoding.UTF8);
                contents += $"/*# sourceURL={Regex.Replace(options.Path, "\n", "")}*/";

                return(await EvaluateFunctionAsync <ElementHandle>(addStyleContent, contents));
            }

            if (options.Content != null)
            {
                return(await EvaluateFunctionAsync <ElementHandle>(addStyleContent, options.Content));
            }

            throw new PuppeteerException("Provide an object with a `Url`, `Path` or `Content` property");
        }
示例#5
0
 /// <summary>
 /// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
 /// </summary>
 /// <param name="options">add script tag options</param>
 /// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
 /// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
 /// <seealso cref="Page.AddScriptTagAsync(string)"/>
 public Task <ElementHandle> AddScriptTagAsync(AddTagOptions options) => MainWorld.AddScriptTagAsync(options);
示例#6
0
        internal async Task <ElementHandle> AddScriptTagAsync(AddTagOptions options)
        {
            const string addScriptUrl     = @"async function addScriptUrl(url, type) {
              const script = document.createElement('script');
              script.src = url;
              if(type)
                script.type = type;
              const promise = new Promise((res, rej) => {
                script.onload = res;
                script.onerror = rej;
              });
              document.head.appendChild(script);
              await promise;
              return script;
            }";
            const string addScriptContent = @"function addScriptContent(content, type = 'text/javascript') {
              const script = document.createElement('script');
              script.type = type;
              script.text = content;
              let error = null;
              script.onerror = e => error = e;
              document.head.appendChild(script);
              if (error)
                throw error;
              return script;
            }";

            async Task <ElementHandle> AddScriptTagPrivate(string script, string urlOrContent, string type)
            {
                var context = await GetExecutionContextAsync().ConfigureAwait(false);

                return((string.IsNullOrEmpty(type)
                        ? await context.EvaluateFunctionHandleAsync(script, urlOrContent).ConfigureAwait(false)
                        : await context.EvaluateFunctionHandleAsync(script, urlOrContent, type).ConfigureAwait(false)) as ElementHandle);
            }

            if (!string.IsNullOrEmpty(options.Url))
            {
                var url = options.Url;
                try
                {
                    return(await AddScriptTagPrivate(addScriptUrl, url, options.Type).ConfigureAwait(false));
                }
                catch (PuppeteerException)
                {
                    throw new PuppeteerException($"Loading script from {url} failed");
                }
            }

            if (!string.IsNullOrEmpty(options.Path))
            {
                var contents = await AsyncFileHelper.ReadAllText(options.Path).ConfigureAwait(false);

                contents += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
                return(await AddScriptTagPrivate(addScriptContent, contents, options.Type).ConfigureAwait(false));
            }

            if (!string.IsNullOrEmpty(options.Content))
            {
                return(await AddScriptTagPrivate(addScriptContent, options.Content, options.Type).ConfigureAwait(false));
            }

            throw new ArgumentException("Provide options with a `Url`, `Path` or `Content` property");
        }
示例#7
0
 internal Task <ElementHandle> AddScriptTag(AddTagOptions options)
 {
     throw new NotImplementedException();
 }
示例#8
0
 /// <summary>
 /// Adds a <c><![CDATA[<script>]]></c> tag into the page with the desired url or content
 /// </summary>
 /// <param name="options">add script tag options</param>
 /// <returns>Task which resolves to the added tag when the script's onload fires or when the script content was injected into frame</returns>
 /// <seealso cref="Page.AddScriptTagAsync(AddTagOptions)"/>
 /// <seealso cref="Page.AddScriptTagAsync(string)"/>
 public Task <ElementHandle> AddScriptTag(AddTagOptions options) => _mainWorld.AddScriptTag(options);
示例#9
0
 public async Task <ElementHandle> AddStyleTagAsync(AddTagOptions options) => await MainFrame.AddStyleTag(options);
示例#10
0
 /// <summary>
 /// Adds a <c><![CDATA[<link rel="stylesheet">]]></c> tag into the page with the desired url or a <c><![CDATA[<link rel="stylesheet">]]></c> tag with the content
 /// </summary>
 /// <param name="options">add style tag options</param>
 /// <returns>Task which resolves to the added tag when the stylesheet's onload fires or when the CSS content was injected into frame</returns>
 /// <seealso cref="Page.AddStyleTagAsync(AddTagOptions)"/>
 /// <seealso cref="Page.AddStyleTagAsync(string)"/>
 public Task <ElementHandle> AddStyleTag(AddTagOptions options) => MainWorld.AddStyleTag(options);