page.exposeFunction(name, puppeteerFunction)

  • name <string> Name of the function on the window object
  • puppeteerFunction <function> Callback function which will be called in Puppeteer's context.
  • returns: <Promise>

The method adds a function called name on the page's window object. When called, the function executes puppeteerFunction in node.js and returns a Promise which resolves to the return value of puppeteerFunction.

If the puppeteerFunction returns a Promise, it will be awaited.

NOTE Functions installed via page.exposeFunction survive navigations.

An example of adding an md5 function into the page:

const puppeteer = require('puppeteer');
const crypto = require('crypto'); puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('console', msg => console.log(msg.text()));
await page.exposeFunction('md5', text =>
crypto.createHash('md5').update(text).digest('hex')
);

await page.evaluate(async () => {
// use window.md5 to compute hashes
const myString = 'PUPPETEER';
const myHash = await window.md5(myString);
console.log(`md5 of ${myString} is ${myHash}`);
});
await browser.close();
});

An example of adding a window.readfile function into the page:

const puppeteer = require('puppeteer');
const fs = require('fs'); puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('console', msg => console.log(msg.text()));
await page.exposeFunction('readfile', async filePath => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, text) => {
if (err)
reject(err);
else
resolve(text);
});
});
});
await page.evaluate(async () => {
// use window.readfile to read contents of a file
const content = await window.readfile('/etc/hosts');
console.log(content);
});
await browser.close();
});
 

扩展新函数给window的更多相关文章

  1. jQuery源码分析-03扩展工具函数jQuery.extend

    // 扩展工具函数 jQuery.extend({ // http://www.w3school.com.cn/jquery/core_noconflict.asp // 释放$的 jQuery 控制 ...

  2. SQL Server 2019 新函数Approx_Count_Distinct

    2019年11月4日微软发布了2019正式版,该版本有着比以往更多强大的新功能和性能上的优势,可参阅SQL Server 2019 新版本. SQL Server 2019具有一组丰富的增强功能和新功 ...

  3. javascript 打开新窗口(window.open)

    打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL: ...

  4. Spring Cloud Eureka集群 动态扩展新节点

    场景描述: Eureka的集群节点有两个,互相注册形成集群,已经支持动态刷新(不知道怎么让Eureka支持动态刷新的可以参考http://www.cnblogs.com/flying607/p/845 ...

  5. JQuery语法 JQuery对象与原生对象互转 文档就绪函数与window.onload的区别

    [JQuery语法] 1.jQuery("选择器").action();通过选择器调用事件函数,但是jquery中,jquery可以用$(“选择器”).action();   ① ...

  6. Yii 1.1.17 一、安装、目录结构、视图、控制器、扩展自定义函数

    这几天了解了一下Yii框架,以简单的博客项目实战入门.大致的实现流程做个记录. 一.Yii 安装与环境检测 从 www.yiiframework.com 获取一份Yii的拷贝,解压到 /wwwroot ...

  7. 【C#】无损转换Image为Icon 【C#】组件发布:MessageTip,轻快型消息提示窗 【C#】给无窗口的进程发送消息 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方 【C#】DataRowState演变备忘

    [C#]无损转换Image为Icon 如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle ...

  8. CREATE FUNCTION - 定义一个新函数

    SYNOPSIS CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE lang ...

  9. js如何安全的扩展系统函数

    如果直接使用原型扩展系统函数,可能会和其他人的代码相互冲突 为了防止出现冲突,可以使用如下方法进行扩展: function MyArray(){ this.Name="MyArray&quo ...

随机推荐

  1. mark mark mark

    编译并使用静态lib---->>>>转自:Walkthrough: Creating and Using a Static Library (C++) 跟踪内存分配:http: ...

  2. 一、json与jsonp的使用

    1.json与jsonp的引入    在ajax中 JSON用来解决数据交换问题,而JSONP来实现跨域.    备注:跨域也可以通过服务器端代理来解决;    理解:JSON是一种数据交换格式,而J ...

  3. 项目中常用的全局宏定义#define

    一 关于屏幕大小 #pragma mark - 屏幕宽高 #define SCREEN_BOUNDS ([UIScreen mainScreen].bounds) #define SCREEN_WID ...

  4. 执行脚本,且以脚本名保存log

    !/bin/bash path="/sys/devices/platform/soc/fd880000.i2c-pld/i2c-0/i2c-4/i2c-15/15-0060" f_ ...

  5. Python学习第十二课——json&pickle&XML模块&OS模块

    json模块 import json dic={'name':'hanhan'} i=8 s='hello' l=[11,22] data=json.dumps(dic) #json.dumps() ...

  6. 建设基于TensorFlow的深度学习环境

    一.使用yum安装git 1.查看系统是否已经安装git git --version 2.yum 安装git yum install git 3.安装成功 git --version 4.进入指定目录 ...

  7. 苹果系统 MacOS 安装根证书

    12306 网上购票以及一些其他内部使用的系统,需要安装.cer扩展名的根证书的情况,windows安装较为简单大家也比较熟悉,使用mac安装根证书在此做下详细介绍. 当前以10.13.5版本为例,其 ...

  8. 学习不一样的vue实战(2): 项目分析

    学习不一样的vue实战(2): 项目分析 首先 首发博客: 我的博客 项目源码: 源码(喜欢请star) 项目预览: 预览 上一章: <学习不一样的vue(1)实战:环境搭建> 我的Q群: ...

  9. 微擎系统BUG漏洞解决方法汇总

    微擎微赞系统BUG漏洞解决方法汇总 弄了微擎系统来玩玩,发觉这个系统BUG还不少,阿里云的提醒都一大堆,主要是没有针对SQL注入做预防,处理的办法基本都是用转义函数. 汇总: 1. 漏洞名称: 微擎任 ...

  10. package.json中一些配置项的含义

    {   "name": "webpack-demo",   "version": "1.0.0",   "de ...