您的位置:

用Node.js和Puppeteer提高你网站的可访问性

一、什么是Puppeteer

Puppeteer是Google官方的无头浏览器的Node.js版,在Node.js中使用Puppeteer库调用和操纵无头浏览器。它提供给我们一个对Chrome浏览器的全面控制,可以模拟用户行为、生成网页截图、获取页面信息等。

Puppeteer已被广泛用于网页自动化测试、爬虫、SEO、可视化等领域。

二、Puppeteer的可访问性工具

除了基本的浏览器控制功能外,Puppeteer还提供了一些实用的可访问性工具,可以帮助我们检测并提高网站的无障碍可访问性。

1. 模拟网络环境

我们可以使用Puppeteer来模拟不同的网络环境,以检测网站在不同网络条件下的性能表现。

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com', {
    waitUntil: 'networkidle0',
    // Simulate slow network environment
    // Throughput in bytes/s, Latency in milliseconds
    // Downloads at 500Kb/s, 400ms roundtrip time
    // See https://chromedevtools.github.io/devtools-protocol/tot/Network#method-emulateNetworkConditions
    networkIdleTimeout: 5000,
    offline: false,
    // Additional headers that will be sent with every request
    extraHeaders: {},
  });
  await browser.close();
})();

2. 检测网页可访问性

Puppeteer内置了许多可访问性检测工具,如pa11yAccessibility Developer Tools等。

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com', {waitUntil: 'networkidle0'});
  const accessibilityReport = await page.accessibility.snapshot();
  console.log(accessibilityReport);
  await browser.close();
})();

3. 测试网页性能

使用Puppeteer,我们可以很容易地测试网页的性能,包括渲染时间、资源加载状态等。

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com', {waitUntil: 'networkidle0'});
  const performance = await page.evaluate(() => JSON.stringify(window.performance));
  console.log(JSON.parse(performance));
  await browser.close();
})();

三、如何提高网站的可访问性

除了使用Puppeteer进行可访问性检测外,我们还可以通过其他方式提高网站的可访问性。

1. 语义化标签

使用语义化标签可以让页面更加易读,并且对于辅助技术(如屏幕阅读器)更加友好。

<header>
  <h1>Page Title</h1>
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>

2. 提供交互反馈

在用户与网站交互时,提供及时的反馈可以让用户感知他们的操作是否成功。

<form>
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>
  <button type="submit">Submit</button>
  <div role="status" aria-live="polite">Please enter a username</div>
</form>

3. 避免使用无障碍条款

不要使用无障碍条款替代无障碍设计。使用无障碍设计可以让你的网站变得更加易于使用,并能够吸引更多用户。

4. 提供描述图片的文本

在使用图像时,请提供描述图像的文本。这对视障人士来说非常重要,因为他们无法直接观看图片内容。

<img src="example.png" alt="Description of example image">
总之,使用Puppeteer可以很方便地提高网站的可访问性,但这并不是唯一的解决方案。我们还需要根据不同的用户需求来设计和实现更加友好和易于访问的网站。
用Node.js和Puppeteer提高你网站的可访问性

2023-05-17
如何利用Node.js提升网站搜索可见性

2023-05-19
NodePuppeteer:让你的自动化测试和网页抓取更加轻

2023-05-20
用Node.js加速网站访问的方法

2023-05-19
使用Vue Dev Server提高你的网站可访问性

2023-05-16
提升网站访问量的方法——Node.js URL优化指南

2023-05-18
印象笔记记录java学习(Java成长笔记)

2022-11-12
ChromeHeadless——革新了前端测试以及网站监控!

2023-05-17
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)