如何在chromedp中禁用JavaScript?

huangapple go评论97阅读模式
英文:

How to disable javascript in chromedp?

问题

我想在https://github.com/chromedp/chromedp中禁用JavaScript。我可以在配置中设置任何分配器标志吗?谢谢!

英文:

I want to disable javascript in https://github.com/chromedp/chromedp

any allocator flag can I put in the config? thank you!

答案1

得分: 1

有一个命令行选项:--blink-settings=scriptEnabled=false

package main

import (
	"context"
	"log"
	"os"

	"github.com/chromedp/chromedp"
)

func main() {
	opts := append(chromedp.DefaultExecAllocatorOptions[:],
		chromedp.Flag("blink-settings", "scriptEnabled=false"),
	)
	ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	defer cancel()

	ctx, cancel = chromedp.NewContext(ctx)
	defer cancel()

	var res []byte
	err := chromedp.Run(ctx,
		chromedp.Navigate(`https://twitter.com/`),
		chromedp.FullScreenshot(&res, 100),
	)
	if err != nil {
		log.Fatal(err)
	}

	os.WriteFile("screenshot.png", res, 0644)
}

FYI,可用的blink-settings列表可以在这里找到:https://source.chromium.org/chromium/chromium/src/+/09372ea8ec2ab6363ccad6a4847a1c9da560b29d:out/Debug/gen/third_party/blink/renderer/core/settings_macros.h;l=1535

英文:

There is a command line option: --blink-settings=scriptEnabled=false.

package main

import (
	"context"
	"log"
	"os"

	"github.com/chromedp/chromedp"
)

func main() {
	opts := append(chromedp.DefaultExecAllocatorOptions[:],
		chromedp.Flag("blink-settings", "scriptEnabled=false"),
	)
	ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	defer cancel()

	ctx, cancel = chromedp.NewContext(ctx)
	defer cancel()

	var res []byte
	err := chromedp.Run(ctx,
		chromedp.Navigate(`https://twitter.com/`),
		chromedp.FullScreenshot(&res, 100),
	)
	if err != nil {
		log.Fatal(err)
	}

	os.WriteFile("screenshot.png", res, 0644)
}

FYI, the list of available blink-settings can be found here: https://source.chromium.org/chromium/chromium/src/+/09372ea8ec2ab6363ccad6a4847a1c9da560b29d:out/Debug/gen/third_party/blink/renderer/core/settings_macros.h;l=1535

huangapple
  • 本文由 发表于 2022年9月22日 12:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/73809274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定