question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to disable images to download?

See original GitHub issue

I tried to use the code, but it didn’t work.

                   // disable images to download
                    await page.SetRequestInterceptionAsync(true);
                    page.Request += (sender, e) =>
                    {
                        if (e.Request.ResourceType == ResourceType.Image)
                            e.Request.AbortAsync();
                        else
                            e.Request.ContinueAsync();
                    };

error like this:

Protocol error(Page.navigate): Target closed. (NetworkManager failed to process Fetch.requestPaused. Value cannot be null.
Parameter name: key.    at System.Collections.Concurrent.ConcurrentDictionary`2.ThrowKeyNullException()
   at System.Collections.Concurrent.ConcurrentDictionary`2.set_Item(TKey key, TValue value)
   at PuppeteerSharp.NetworkManager.OnRequestPausedAsync(FetchRequestPausedResponse e)
   at PuppeteerSharp.NetworkManager.Client_MessageReceived(Object sender, MessageEventArgs e))

environment:

dotnet core 2.2
vs 2019

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
TTonlyV5commented, May 27, 2019

code like this. use local chrome.

public static async Task<string> LogInForGetCookieSessionTokenAsync(string LoginUrl = "https://www.baidu.com/")
        {
            try
            {
                string[] ResultCookies = new string[] { };
                //获取用户名
                string UserName = Environment.UserName;

                var currentDirectory = Path.Combine(@"C:\Users\", UserName, @"AppData\Local\Google\Chrome\Application\", "Chrome.exe");//string currentDirectory = Path.GetDirectoryName(@"C:\Users\TT\AppData\Local\Google\Chrome\Application"); //指定Chrome.exe在这目录才行

                if (!File.Exists(currentDirectory))
                {
                    currentDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
                    var downloadPath = Path.Combine(currentDirectory, "LocalChromium");
                    Console.WriteLine($"Attemping to set up puppeteer to use Chromium found under directory {downloadPath} ");
                    if (!Directory.Exists(downloadPath))
                    {
                        Console.WriteLine("Custom directory not found. Creating directory");
                        Directory.CreateDirectory(downloadPath);

                        Console.WriteLine("Downloading Chromium");

                        var browserFetcherOptions = new BrowserFetcherOptions { Path = downloadPath };
                        var browserFetcher = new BrowserFetcher(browserFetcherOptions);
                        await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);

                        var executablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision);

                        if (string.IsNullOrEmpty(executablePath))
                        {
                            Console.WriteLine("Custom Chromium location is empty. Unable to start Chromium. Exiting.\n Press any key to continue");
                            Console.ReadLine();
                            return "Custom Chromium location is empty. Unable to start Chromium. Exiting.\n Press any key to continue";
                        }
                        Console.WriteLine($"Attemping to start Chromium using executable path: {executablePath}");
                        //Set Path
                        currentDirectory = Path.Combine(executablePath, "Chromium.exe");
                    }
                    else
                    {
                        //Set Path  这里没做下载失败的判断
                        currentDirectory = Path.Combine(downloadPath, "Chromium.exe");
                    }
                }

                var options = new LaunchOptions
                {
                    Headless = true,//无头
                    ExecutablePath = currentDirectory,//本地路径
                    Args = new string[]
                    {
                        "--disable-infobars",//隐藏 自动化标题
                    },//添加Argument 和webdriver一样吧
                    DefaultViewport = new ViewPortOptions
                    {
                        Width = 500,
                        Height = 500,
                        IsMobile = true,
                        //DeviceScaleFactor = 2
                    },
                    //SlowMo=250, // slow down by 250ms
                };

                using (var browser = await Puppeteer.LaunchAsync(options))
                using (var page = await browser.NewPageAsync())
                {              
                    // disable images to download
                    await page.SetRequestInterceptionAsync(true);
                    page.Request += (sender, e) =>
                    {
                        if (e.Request.ResourceType == ResourceType.Image)
                            e.Request.AbortAsync();
                        else
                            e.Request.ContinueAsync();
                    };
                    //设置手机模式
                    await page.SetUserAgentAsync("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
                    //进入登录页
                    await page.GoToAsync(LoginUrl);
                    // 登录
                    Console.WriteLine("Start Login!");
                    await page.GetContentAsync();
                    
                }
                return ResultCookies[0];
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }
        }

0reactions
TTonlyV5commented, May 28, 2019

Thank you for your answer. Maybe I misunderstood the versions of chrome and chromium. 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Protect image download - html
Protect image download · 1. Disable the Right Click on all Images. let allImages = document.querySelectorAll("img"); allImages.forEach((value)=>{ ...
Read more >
How to prevent pictures from being downloaded by right- ...
Right click on the webpage in Chrome, press Inspect , then find the directory where the image is stored by browsing thumbnails, then...
Read more >
How To Protect Your Website From Image Theft
The easiest way to download images is by right-clicking on them and selecting “save image”. While it's still easy enough to download images...
Read more >
Preventing visitors from downloading your images, videos ...
Disable right-clicks ... We don't recommend this option because it creates a false sense of security and in no way prevents screenshots, drag-and- ......
Read more >
how to disable download option from google site
If you have an image on your site and people can see it then their computer or mobile device has already downloaded it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found