| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 function createWebview() { | |
| 6 var webview = document.createElement('webview'); | |
| 7 document.body.appendChild(webview); | |
| 8 return webview; | |
| 9 } | |
| 10 | |
| 11 function onGetBackgroundExecuted(results) { | |
| 12 chrome.send('testResult', [results.length == 1 && results[0] == 'red']); | |
| 13 }; | |
| 14 | |
| 15 function testExecuteScriptCode(url) { | |
| 16 var webview = createWebview(); | |
| 17 | |
| 18 var onSetBackgroundExecuted = function() { | |
| 19 webview.executeScript({ | |
| 20 code: 'document.body.style.backgroundColor;' | |
| 21 }, onGetBackgroundExecuted); | |
| 22 }; | |
| 23 | |
| 24 var onLoadStop = function() { | |
| 25 webview.executeScript({ | |
| 26 code: 'document.body.style.backgroundColor = \'red\';' | |
| 27 }, onSetBackgroundExecuted); | |
| 28 }; | |
| 29 | |
| 30 webview.addEventListener('loadstop', onLoadStop); | |
| 31 webview.src = url; | |
| 32 } | |
| 33 | |
| 34 function testExecuteScriptCodeFromFile(url) { | |
| 35 var webview = createWebview(); | |
| 36 | |
| 37 var onSetBackgroundExecuted = function() { | |
| 38 webview.executeScript({ | |
| 39 code: 'document.body.style.backgroundColor;' | |
| 40 }, onGetBackgroundExecuted); | |
| 41 }; | |
| 42 | |
| 43 var onLoadStop = function() { | |
| 44 webview.executeScript({ | |
| 45 file: 'test/webview_execute_script.js' | |
| 46 }, onSetBackgroundExecuted); | |
| 47 }; | |
| 48 | |
| 49 webview.addEventListener('loadstop', onLoadStop); | |
| 50 webview.src = url; | |
| 51 } | |
| 52 | |
| 53 function testAddContentScript(url) { | |
| 54 var webview = document.createElement('webview'); | |
| 55 | |
| 56 console.log("Step 1: call <webview>.addContentScripts."); | |
| 57 webview.addContentScripts( | |
| 58 [{"name": 'myrule', | |
| 59 "matches": ["http://*/empty*"], | |
| 60 "js": ["test/inject_comm_channel.js", "test/inject_comm_channel_2.js"], | |
| 61 "run_at": "document_start"}]); | |
| 62 | |
| 63 webview.addEventListener('loadstop', function() { | |
| 64 console.log("Step 2: postMessage to build connection."); | |
| 65 var msg = ['connect']; | |
| 66 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
| 67 }); | |
| 68 | |
| 69 window.addEventListener('message', function(e) { | |
| 70 var data = JSON.parse(e.data); | |
| 71 if (data == 'connected') { | |
| 72 console.log( | |
| 73 'Step 3: A communication channel has been established with webview.'); | |
| 74 chrome.send('testResult', [true]); | |
| 75 return; | |
| 76 } | |
| 77 console.log('Unexpected message: \'' + data[0] + '\''); | |
| 78 chrome.send('testResult', [false]); | |
| 79 }); | |
| 80 | |
| 81 webview.src = url; | |
| 82 document.body.appendChild(webview); | |
| 83 } | |
| 84 | |
| 85 function testAddMultiContentScripts(url) { | |
| 86 var webview = document.createElement('webview'); | |
| 87 | |
| 88 console.log("Step 1: call <webview>.addContentScripts(myrule1 & myrule2)"); | |
| 89 webview.addContentScripts( | |
| 90 [{"name": 'myrule1', | |
| 91 "matches": ["http://*/empty*"], | |
| 92 "js": ["test/inject_comm_channel.js"], | |
| 93 "run_at": "document_start"}, | |
| 94 {"name": 'myrule2', | |
| 95 "matches": ["http://*/empty*"], | |
| 96 "js": ["test/inject_comm_channel_2.js"], | |
| 97 "run_at": "document_start"}]); | |
| 98 | |
| 99 webview.addEventListener('loadstop', function() { | |
| 100 console.log("Step 2: postMessage to build connection."); | |
| 101 var msg1 = ['connect']; | |
| 102 webview.contentWindow.postMessage(JSON.stringify(msg1), '*'); | |
| 103 console.log("Step 3: postMessage to build connection to the other script."); | |
| 104 var msg2 = ['connect_request']; | |
| 105 webview.contentWindow.postMessage(JSON.stringify(msg2), '*'); | |
| 106 }); | |
| 107 | |
| 108 var response_1 = false; | |
| 109 var response_2 = false; | |
| 110 window.addEventListener('message', function(e) { | |
| 111 var data = JSON.parse(e.data); | |
| 112 if (data == 'connected') { | |
| 113 console.log( | |
| 114 'Step 4: A communication channel has been established with webview.'); | |
| 115 response_1 = true; | |
| 116 if (response_1 && response_2) | |
| 117 chrome.send('testResult', [true]); | |
| 118 return; | |
| 119 } else if (data == 'connected_response') { | |
| 120 console.log( | |
| 121 'Step 5: A communication channel has been established with webview.'); | |
| 122 response_2 = true; | |
| 123 if (response_1 && response_2) | |
| 124 chrome.send('testResult', [true]); | |
| 125 return; | |
| 126 } | |
| 127 console.log('!!!!!Unexpected message: \'' + data[0] + '\''); | |
| 128 chrome.send('testResult', [false]); | |
| 129 }); | |
| 130 | |
| 131 webview.src = url; | |
| 132 document.body.appendChild(webview); | |
| 133 } | |
| 134 | |
| 135 function testAddContentScriptWithSameNameShouldOverwriteTheExistingOne(url) { | |
| 136 var webview = document.createElement('webview'); | |
| 137 | |
| 138 console.log("Step 1: call <webview>.addContentScripts(myrule1)"); | |
| 139 webview.addContentScripts( | |
| 140 [{"name": 'myrule1', | |
| 141 "matches": ["http://*/empty*"], | |
| 142 "js": ["test/inject_comm_channel.js"], | |
| 143 "run_at": "document_start"}]); | |
| 144 var connect_script_1 = true; | |
| 145 var connect_script_2 = false; | |
| 146 | |
| 147 webview.addEventListener('loadstop', function() { | |
| 148 if (connect_script_1) { | |
| 149 var msg1 = ['connect']; | |
| 150 webview.contentWindow.postMessage(JSON.stringify(msg1), '*'); | |
| 151 connect_script_1 = false; | |
| 152 } | |
| 153 if (connect_script_2) { | |
| 154 var msg2 = ['connect_request']; | |
| 155 webview.contentWindow.postMessage(JSON.stringify(msg2), '*'); | |
| 156 connect_script_2 = false; | |
| 157 } | |
| 158 }); | |
| 159 | |
| 160 var should_get_response_from_script_1 = true; | |
| 161 window.addEventListener('message', function(e) { | |
| 162 var data = JSON.parse(e.data); | |
| 163 if (data == 'connected') { | |
| 164 if (should_get_response_from_script_1) { | |
| 165 console.log( | |
| 166 'Step 2: A communication channel has been established with webview.' | |
| 167 ); | |
| 168 webview.addContentScripts( | |
| 169 [{"name": 'myrule1', | |
| 170 "matches": ["http://*/empty*"], | |
| 171 "js": ["test/inject_comm_channel_2.js"], | |
| 172 "run_at": "document_start"}]); | |
| 173 connect_script_2 = true; | |
| 174 should_get_response_from_script_1 = false; | |
| 175 webview.src = url; | |
| 176 } else { | |
| 177 chrome.send('testResult', [false]); | |
| 178 } | |
| 179 return; | |
| 180 } else if (data == 'connected_response') { | |
| 181 console.log( | |
| 182 'Step 3: Another communication channel has been established ' + | |
| 183 'with webview.'); | |
| 184 setTimeout(function() { | |
| 185 chrome.send('testResult', [true]); | |
| 186 }, 1000); | |
| 187 return; | |
| 188 } | |
| 189 console.log('Unexpected message: \'' + data[0] + '\''); | |
| 190 chrome.send('testResult', [false]); | |
| 191 }); | |
| 192 | |
| 193 webview.src = url; | |
| 194 document.body.appendChild(webview); | |
| 195 } | |
| 196 | |
| 197 function testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView(url) { | |
| 198 var webview1 = document.createElement('webview'); | |
| 199 var webview2 = document.createElement('webview'); | |
| 200 | |
| 201 console.log("Step 1: call <webview1>.addContentScripts."); | |
| 202 webview1.addContentScripts( | |
| 203 [{"name": 'myrule', | |
| 204 "matches": ["http://*/empty*"], | |
| 205 "js": ["test/inject_comm_channel.js"], | |
| 206 "run_at": "document_start"}]); | |
| 207 | |
| 208 webview2.addEventListener('loadstop', function() { | |
| 209 console.log("Step 2: webview2 requests to build communication channel."); | |
| 210 var msg = ['connect']; | |
| 211 webview2.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
| 212 setTimeout(function() { | |
| 213 chrome.send('testResult', [true]); | |
| 214 }, 2000); | |
| 215 }); | |
| 216 | |
| 217 window.addEventListener('message', function(e) { | |
| 218 var data = JSON.parse(e.data); | |
| 219 if (data == 'connected') { | |
| 220 chrome.send('testResult', [false]); | |
| 221 return; | |
| 222 } | |
| 223 console.log('Unexpected message: \'' + data[0] + '\''); | |
| 224 chrome.send('testResult', [false]); | |
| 225 }); | |
| 226 | |
| 227 webview1.src = url; | |
| 228 webview2.src = url; | |
| 229 document.body.appendChild(webview1); | |
| 230 document.body.appendChild(webview2); | |
| 231 } | |
| 232 | |
| 233 function testAddAndRemoveContentScripts(url) { | |
| 234 var webview = document.createElement('webview'); | |
| 235 | |
| 236 console.log("Step 1: call <webview>.addContentScripts."); | |
| 237 webview.addContentScripts( | |
| 238 [{"name": 'myrule', | |
| 239 "matches": ["http://*/empty*"], | |
| 240 "js": ["test/inject_comm_channel.js"], | |
| 241 "run_at": "document_start"}]); | |
| 242 | |
| 243 var count = 0; | |
| 244 webview.addEventListener('loadstop', function() { | |
| 245 if (count == 0) { | |
| 246 console.log('Step 2: post message to build connect.'); | |
| 247 var msg = ['connect']; | |
| 248 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
| 249 count++; | |
| 250 } else if (count == 1) { | |
| 251 console.log( | |
| 252 'Step 4: call <webview>.removeContentScripts and navigate.'); | |
| 253 webview.removeContentScripts(); | |
| 254 webview.src = url; | |
| 255 count++; | |
| 256 } else if (count == 2) { | |
| 257 console.log('Step 5: post message to build connect again.'); | |
| 258 var msg = ['connect']; | |
| 259 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
| 260 setTimeout(function() { | |
| 261 chrome.send('testResult', [true]); | |
| 262 }, 2000); | |
| 263 } | |
| 264 }); | |
| 265 | |
| 266 var replyCount = 0; | |
| 267 window.addEventListener('message', function(e) { | |
| 268 var data = JSON.parse(e.data); | |
| 269 if (data[0] == 'connected') { | |
| 270 console.log( | |
| 271 'Step 3: A communication channel has been established with webview.'); | |
| 272 if (replyCount == 0) { | |
| 273 webview.setAttribute('src', 'about:blank'); | |
| 274 replyCount++; | |
| 275 return; | |
| 276 } else if (replyCount == 1) { | |
| 277 chrome.send('testResult', [false]); | |
| 278 return; | |
| 279 } | |
| 280 } | |
| 281 console.log('Unexpected message: \'' + data[0] + '\''); | |
| 282 chrome.send('testResult', [false]); | |
| 283 }); | |
| 284 | |
| 285 webview.src = url; | |
| 286 document.body.appendChild(webview); | |
| 287 } | |
| 288 | |
| 289 function testAddContentScriptsWithNewWindowAPI(url) { | |
| 290 var webview = document.createElement('webview'); | |
| 291 | |
| 292 var newwebview; | |
| 293 webview.addEventListener('newwindow', function(e) { | |
| 294 e.preventDefault(); | |
| 295 newwebview = document.createElement('webview'); | |
| 296 | |
| 297 console.log('Step 2: call newwebview.addContentScripts.'); | |
| 298 newwebview.addContentScripts( | |
| 299 [{"name": 'myrule', | |
| 300 "matches": ["http://*/guest_from_opener*"], | |
| 301 "js": ["test/inject_comm_channel.js"], | |
| 302 "run_at": "document_start"}]); | |
| 303 | |
| 304 newwebview.addEventListener('loadstop', function(evt) { | |
| 305 var msg = ['connect']; | |
| 306 console.log('Step 4: new webview postmessage to build communication ' + | |
| 307 'channel.'); | |
| 308 newwebview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
| 309 }); | |
| 310 | |
| 311 document.body.appendChild(newwebview); | |
| 312 // attach the new window to the new <webview>. | |
| 313 console.log("Step 3: attaches the new webview."); | |
| 314 e.window.attach(newwebview); | |
| 315 }); | |
| 316 | |
| 317 window.addEventListener('message', function(e) { | |
| 318 var data = JSON.parse(e.data); | |
| 319 if (data == 'connected' && e.source == newwebview.contentWindow) { | |
| 320 console.log('Step 5: a communication channel has been established ' + | |
| 321 'with the new webview.'); | |
| 322 chrome.send('testResult', [true]); | |
| 323 return; | |
| 324 } else { | |
| 325 chrome.send('testResult', [false]); | |
| 326 return; | |
| 327 } | |
| 328 console.log('unexpected message: \'' + data[0] + '\''); | |
| 329 chrome.send('testResult', [false]); | |
| 330 }); | |
| 331 | |
| 332 console.log('Step 1: navigates the webview to window open guest URL.'); | |
| 333 webview.setAttribute('src', url); | |
| 334 document.body.appendChild(webview); | |
| 335 } | |
| 336 | |
| 337 function testContentScriptIsInjectedAfterTerminateAndReloadWebView(url) { | |
| 338 var webview = document.createElement('webview'); | |
| 339 | |
| 340 console.log('Step 1: call <webview>.addContentScripts.'); | |
| 341 webview.addContentScripts( | |
| 342 [{"name": 'myrule', | |
| 343 "matches": ["http://*/empty*"], | |
| 344 "js": ["test/inject_comm_channel.js"], | |
| 345 "run_at": "document_start"}]); | |
| 346 | |
| 347 var count = 0; | |
| 348 webview.addEventListener('loadstop', function() { | |
| 349 if (count == 0) { | |
| 350 console.log('Step 2: call webview.terminate().'); | |
| 351 webview.terminate(); | |
| 352 ++count; | |
| 353 return; | |
| 354 } else if (count == 1) { | |
| 355 console.log('Step 4: postMessage to build communication.'); | |
| 356 var msg = ['connect']; | |
| 357 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
| 358 ++count; | |
| 359 } | |
| 360 }); | |
| 361 | |
| 362 webview.addEventListener('exit', function() { | |
| 363 console.log('Step 3: call webview.reload().'); | |
| 364 webview.reload(); | |
| 365 }); | |
| 366 | |
| 367 window.addEventListener('message', function(e) { | |
| 368 var data = JSON.parse(e.data); | |
| 369 if (data == 'connected') { | |
| 370 console.log( | |
| 371 'Step 5: A communication channel has been established with webview.'); | |
| 372 chrome.send('testResult', [true]); | |
| 373 return; | |
| 374 } | |
| 375 console.log('Unexpected message: \'' + data[0] + '\''); | |
| 376 chrome.send('testResult', [false]); | |
| 377 }); | |
| 378 | |
| 379 webview.src = url; | |
| 380 document.body.appendChild(webview); | |
| 381 } | |
| OLD | NEW |