File size: 1,779 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package bridge

import (
	"context"

	"github.com/chromedp/cdproto/emulation"
	"github.com/chromedp/cdproto/page"
	"github.com/chromedp/chromedp"
)

// DisableAnimationsCSS is injected to force-disable all CSS animations and transitions.
const DisableAnimationsCSS = `
(function() {
  const style = document.createElement('style');
  style.setAttribute('data-pinchtab', 'no-animations');
  style.textContent = '*, *::before, *::after { animation: none !important; animation-duration: 0s !important; transition: none !important; transition-duration: 0s !important; scroll-behavior: auto !important; }';
  (document.head || document.documentElement).appendChild(style);
})();
`

// InjectNoAnimations adds a persistent script (via CDP) that disables CSS
// animations on every document load. Used when BRIDGE_NO_ANIMATIONS=true.
func (b *Bridge) InjectNoAnimations(ctx context.Context) error {
	return chromedp.Run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			_, err := page.AddScriptToEvaluateOnNewDocument(DisableAnimationsCSS).Do(ctx)
			return err
		}),
		chromedp.ActionFunc(func(ctx context.Context) error {
			return emulation.SetEmulatedMedia().
				WithFeatures([]*emulation.MediaFeature{
					{Name: "prefers-reduced-motion", Value: "reduce"},
				}).Do(ctx)
		}),
	)
}

// DisableAnimationsOnce runs the animation-disabling CSS on the current page
// (one-shot, for per-request ?noAnimations=true).
func DisableAnimationsOnce(ctx context.Context) error {
	return chromedp.Run(ctx,
		chromedp.Evaluate(DisableAnimationsCSS, nil),
		chromedp.ActionFunc(func(ctx context.Context) error {
			return emulation.SetEmulatedMedia().
				WithFeatures([]*emulation.MediaFeature{
					{Name: "prefers-reduced-motion", Value: "reduce"},
				}).Do(ctx)
		}),
	)
}