react-native-webview.git

Injection.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, {Component} from 'react';
  2. import {Text, View, ScrollView} from 'react-native';
  3. import WebView from 'react-native-webview';
  4. const HTML = `
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1">
  10. <title>iframe test</title>
  11. </head>
  12. <body>
  13. <iframe src="https://birchlabs.co.uk/linguabrowse/infopages/obsol/iframe.html?v=1" name="iframe_0" style="width: 100%; height: 25px;"></iframe>
  14. <iframe src="https://birchlabs.co.uk/linguabrowse/infopages/obsol/iframe2.html?v=1" name="iframe_1" style="width: 100%; height: 25px;"></iframe>
  15. <iframe src="https://www.ebay.co.uk" name="iframe_2" style="width: 100%; height: 25px;"></iframe>
  16. </body>
  17. </html>
  18. `;
  19. type Props = {};
  20. type State = {
  21. backgroundColor: string,
  22. };
  23. export default class Injection extends Component<Props, State> {
  24. state = {
  25. backgroundColor: '#FF00FF00'
  26. };
  27. render() {
  28. return (
  29. <ScrollView>
  30. <View style={{ }}>
  31. <View style={{ height: 120 }}>
  32. <WebView
  33. /**
  34. * This HTML is a copy of a multi-frame JS injection test that I had lying around.
  35. * @see https://birchlabs.co.uk/linguabrowse/infopages/obsol/iframeTest.html
  36. */
  37. source={{ html: HTML }}
  38. automaticallyAdjustContentInsets={false}
  39. style={{backgroundColor:'#00000000'}}
  40. /* Must be populated in order for `messagingEnabled` to be `true` to activate the
  41. * JS injection user scripts, consistent with current behaviour. This is undesirable,
  42. * so needs addressing in a follow-up PR. */
  43. onMessage={() => {}}
  44. /* We set this property in each frame */
  45. injectedJavaScriptBeforeContentLoaded={`window.self.colourToUse = "orange";`}
  46. injectedJavaScriptForMainFrameOnly={false}
  47. /* We read the colourToUse property in each frame to recolour each frame */
  48. injectedJavaScript={`window.self.document.body.style.backgroundColor = window.self.colourToUse;`}
  49. />
  50. </View>
  51. </View>
  52. <Text>This test presents three iframes: iframe_0 (yellow); iframe_1 (pink); and iframe_2 (transparent, because its 'X-Frame-Options' is set to 'SAMEORIGIN').</Text>
  53. <Text>Before injection, the main frame's background is the browser's default value (transparent or white) and each frame has its natural colour.</Text>
  54. <Text>1) At injection time "beforeContentLoaded", a variable will be set in each frame to set 'orange' as the "colour to be used".</Text>
  55. <Text>2) At injection time "afterContentLoaded", that variable will be read, and thus the colour orange will be injected into all frames.</Text>
  56. <Text>✅ If the main frame, iframe_0, and iframe_1 all become orange, then multi-frame injection is supported and both injection times are supported.</Text>
  57. <Text>❌ If no frames become orange, then only one (or neither) of the injection times are supported, or even injection into the main frame is failing.</Text>
  58. <Text>❌ If only the main frame becomes orange, then multi-frame injection is not supported.</Text>
  59. </ScrollView>
  60. );
  61. }
  62. }