web-view-load-with-request.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Linking, Platform } from "react-native";
  2. import { EXCEPTION_URLS, UNA_URL } from "../constants/settings";
  3. export function shouldStartLoadWithRequest(event: {
  4. url: string;
  5. navigationType?: string;
  6. }): boolean {
  7. const url = event.url;
  8. // block blank pages
  9. if (url === "about:blank") return false;
  10. // check for exception URLs
  11. const isException = EXCEPTION_URLS.some(e => {
  12. url.indexOf(e) !== -1
  13. });
  14. // allow internal URLs
  15. const isInternal = url.indexOf(UNA_URL) !== -1;
  16. // special Android/iOS conditions
  17. const isAndroid = Platform.OS === "android";
  18. const isIosClick = Platform.OS === "ios" && event.navigationType === "click";
  19. // block & open external URLs
  20. if ((url.indexOf("http") !== 0 || (!isException && !isInternal && (isAndroid || isIosClick)))
  21. || url.indexOf("calendar_sync") !== -1) {
  22. Linking.canOpenURL(url)
  23. .then(supported => {
  24. if (supported) {
  25. Linking.openURL(url);
  26. } else {
  27. console.warn("Don't know how to open URI:", url);
  28. }
  29. })
  30. .catch(err => console.warn("Error opening URL:", url, err));
  31. return false; // prevent WebView from loading
  32. }
  33. return true; // allow WebView to load
  34. }