@@ -41,6 +41,7 @@ const { Credential } = require('./virtual_authenticator')
4141const webElement = require ( './webelement' )
4242const { isObject } = require ( './util' )
4343const BIDI = require ( '../bidi' )
44+ const { PinnedScript } = require ( './pinnedScript' )
4445
4546// Capability names that are defined in the W3C spec.
4647const W3C_CAPABILITY_NAMES = new Set ( [
@@ -686,6 +687,8 @@ class WebDriver {
686687
687688 /** @private {./virtual_authenticator}*/
688689 this . authenticatorId_ = null
690+
691+ this . pinnedScripts_ = { }
689692 }
690693
691694 /**
@@ -795,6 +798,15 @@ class WebDriver {
795798 if ( typeof script === 'function' ) {
796799 script = 'return (' + script + ').apply(null, arguments);'
797800 }
801+
802+ if ( script && script instanceof PinnedScript ) {
803+ return this . execute (
804+ new command . Command ( command . Name . EXECUTE_SCRIPT )
805+ . setParameter ( 'script' , script . executionScript ( ) )
806+ . setParameter ( 'args' , args )
807+ )
808+ }
809+
798810 return this . execute (
799811 new command . Command ( command . Name . EXECUTE_SCRIPT )
800812 . setParameter ( 'script' , script )
@@ -807,6 +819,15 @@ class WebDriver {
807819 if ( typeof script === 'function' ) {
808820 script = 'return (' + script + ').apply(null, arguments);'
809821 }
822+
823+ if ( script && script instanceof PinnedScript ) {
824+ return this . execute (
825+ new command . Command ( command . Name . EXECUTE_ASYNC_SCRIPT )
826+ . setParameter ( 'script' , script . executionScript ( ) )
827+ . setParameter ( 'args' , args )
828+ )
829+ }
830+
810831 return this . execute (
811832 new command . Command ( command . Name . EXECUTE_ASYNC_SCRIPT )
812833 . setParameter ( 'script' , script )
@@ -1226,7 +1247,9 @@ class WebDriver {
12261247 this . _wsUrl = await this . getWsUrl ( debuggerUrl , target , caps )
12271248 return new Promise ( ( resolve , reject ) => {
12281249 try {
1229- this . _wsConnection = new WebSocket ( this . _wsUrl . replace ( 'localhost' , '127.0.0.1' ) )
1250+ this . _wsConnection = new WebSocket (
1251+ this . _wsUrl . replace ( 'localhost' , '127.0.0.1' )
1252+ )
12301253 this . _cdpConnection = new cdp . CdpConnection ( this . _wsConnection )
12311254 } catch ( err ) {
12321255 reject ( err )
@@ -1532,6 +1555,74 @@ class WebDriver {
15321555 } )
15331556 }
15341557
1558+ async pinScript ( script ) {
1559+ let pinnedScript = new PinnedScript ( script )
1560+ let connection
1561+ if ( Object . is ( this . _cdpConnection , undefined ) ) {
1562+ connection = await this . createCDPConnection ( 'page' )
1563+ } else {
1564+ connection = this . _cdpConnection
1565+ }
1566+
1567+ await connection . execute ( 'Page.enable' , { } , null )
1568+
1569+ await connection . execute (
1570+ 'Runtime.evaluate' ,
1571+ {
1572+ expression : pinnedScript . creationScript ( ) ,
1573+ } ,
1574+ null
1575+ )
1576+
1577+ let result = await connection . send (
1578+ 'Page.addScriptToEvaluateOnNewDocument' ,
1579+ {
1580+ source : pinnedScript . creationScript ( ) ,
1581+ }
1582+ )
1583+
1584+ pinnedScript . scriptId = result [ 'result' ] [ 'identifier' ]
1585+
1586+ this . pinnedScripts_ [ pinnedScript . handle ] = pinnedScript
1587+
1588+ return pinnedScript
1589+ }
1590+
1591+ async unpinScript ( script ) {
1592+ if ( script && ! ( script instanceof PinnedScript ) ) {
1593+ throw Error ( `Pass valid PinnedScript object. Received: ${ script } ` )
1594+ }
1595+
1596+ if ( script . handle in this . pinnedScripts_ ) {
1597+ let connection
1598+ if ( Object . is ( this . _cdpConnection , undefined ) ) {
1599+ connection = this . createCDPConnection ( 'page' )
1600+ } else {
1601+ connection = this . _cdpConnection
1602+ }
1603+
1604+ await connection . execute ( 'Page.enable' , { } , null )
1605+
1606+ await connection . execute (
1607+ 'Runtime.evaluate' ,
1608+ {
1609+ expression : script . removalScript ( ) ,
1610+ } ,
1611+ null
1612+ )
1613+
1614+ await connection . execute (
1615+ 'Page.removeScriptToEvaluateOnLoad' ,
1616+ {
1617+ identifier : script . scriptId ,
1618+ } ,
1619+ null
1620+ )
1621+
1622+ delete this . pinnedScripts_ [ script . handle ]
1623+ }
1624+ }
1625+
15351626 /**
15361627 *
15371628 * @returns The value of authenticator ID added
0 commit comments