Пример #1
0
        /// <inheritdoc cref="IFrame.AddStyleTagAsync(AddTagOptions)"/>
        public async Task <IElementHandle> AddStyleTagAsync(AddTagOptions options)
        {
            if (
                options == null ||
                (
                    string.IsNullOrEmpty(options.Url) &&
                    string.IsNullOrEmpty(options.Path) &&
                    string.IsNullOrEmpty(options.Content)))
            {
                throw new ArgumentException("Provide an object with a 'url', 'path' or 'content' property");
            }

            var context = await GetMainContextAsync().ConfigureAwait(false);

            return(await RaceWithCSPErrorAsync(async() =>
            {
                if (!string.IsNullOrEmpty(options.Url))
                {
                    return await context.EvaluateHandleAsync(
                        @"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;
                        }",
                        options.Url).ConfigureAwait(false) as ElementHandle;
                }

                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.Path))
                {
                    string contents = File.ReadAllText(options.Path);
                    contents += "/*# sourceURL=" + options.Path.Replace("\n", string.Empty) + "*/";
                    return await context.EvaluateHandleAsync(addStyleContent, contents).ConfigureAwait(false) as ElementHandle;
                }

                return await context.EvaluateHandleAsync(addStyleContent, options.Content).ConfigureAwait(false) as ElementHandle;
            }).ConfigureAwait(false));
        }
Пример #2
0
        /// <inheritdoc cref="IFrame.AddScriptTagAsync(AddTagOptions)"/>
        public async Task <IElementHandle> 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;
            }";

            if (options == null || (string.IsNullOrEmpty(options.Url) && string.IsNullOrEmpty(options.Path) && string.IsNullOrEmpty(options.Content)))
            {
                throw new PlaywrightSharpException("Provide an object with a `url`, `path` or `content` property");
            }

            var context = await GetMainContextAsync().ConfigureAwait(false);

            return(await RaceWithCSPErrorAsync(async() =>
            {
                if (!string.IsNullOrEmpty(options.Url))
                {
                    return await context.EvaluateHandleAsync(addScriptUrl, options.Url, options.Type)
                    .ConfigureAwait(false) as ElementHandle;
                }

                if (!string.IsNullOrEmpty(options.Path))
                {
                    string content = File.ReadAllText(options.Path);
                    content += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
                    return await context.EvaluateHandleAsync(addScriptContent, content, options.Type)
                    .ConfigureAwait(false) as ElementHandle;
                }

                return await context.EvaluateHandleAsync(
                    addScriptContent,
                    options.Content,
                    string.IsNullOrEmpty(options.Type) ? "text/javascript" : options.Type).ConfigureAwait(false) as ElementHandle;
            }).ConfigureAwait(false));
        }
Пример #3
0
 /// <inheritdoc />
 public Task <IElementHandle> AddScriptTagAsync(AddTagOptions options) => throw new NotImplementedException();
Пример #4
0
 /// <inheritdoc cref="IPage.AddStyleTagAsync(AddTagOptions)"/>
 public Task <IElementHandle> AddStyleTagAsync(AddTagOptions options)
 {
     throw new NotImplementedException();
 }