-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add WebView "blockedURLs" property #11842
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,15 +245,26 @@ - (void)setData_:(id)value | |
|
||
- (void)setBlacklistedURLs_:(id)blacklistedURLs | ||
{ | ||
ENSURE_TYPE(blacklistedURLs, NSArray); | ||
DEPRECATED_REPLACED(@"UI.WebView.blacklistedURLs", @"9.2.0", @"UI.WebView.blockedURLs"); | ||
|
||
for (id blacklistedURL in blacklistedURLs) { | ||
ENSURE_TYPE(blacklistedURL, NSString); | ||
ENSURE_TYPE(blacklistedURLs, NSArray); | ||
for (id nextURL in blacklistedURLs) { | ||
ENSURE_TYPE(nextURL, NSString); | ||
} | ||
|
||
_blacklistedURLs = blacklistedURLs; | ||
} | ||
|
||
- (void)setBlockedURLs_:(id)blockedURLs | ||
{ | ||
ENSURE_TYPE(blockedURLs, NSArray); | ||
for (id nextURL in blockedURLs) { | ||
ENSURE_TYPE(nextURL, NSString); | ||
} | ||
|
||
_blockedURLs = blockedURLs; | ||
} | ||
|
||
- (void)setHtml_:(id)args | ||
{ | ||
ignoreNextRequest = YES; | ||
|
@@ -1010,20 +1021,37 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK | |
} | ||
NSArray<NSString *> *allowedURLSchemes = [[self proxy] valueForKey:@"allowedURLSchemes"]; | ||
|
||
// Handle blacklisted URL's | ||
// Do not load the URL if it's black-listed. | ||
// DEPRECATED: Should use "blockedURLs" property with a "blockedurl" event listener instead. | ||
NSString *urlCandidate = navigationAction.request.URL.absoluteString; | ||
if (_blacklistedURLs != nil && _blacklistedURLs.count > 0) { | ||
NSString *urlCandidate = navigationAction.request.URL.absoluteString; | ||
|
||
for (NSString *blackListedURL in _blacklistedURLs) { | ||
if ([urlCandidate rangeOfString:blackListedURL options:NSCaseInsensitiveSearch].location != NSNotFound) { | ||
for (NSString *blockedURL in _blacklistedURLs) { | ||
if ([urlCandidate rangeOfString:blockedURL options:NSCaseInsensitiveSearch].location != NSNotFound) { | ||
if ([[self proxy] _hasListeners:@"blacklisturl"]) { | ||
[[self proxy] fireEvent:@"blacklisturl" | ||
withObject:@{ | ||
@"url" : urlCandidate, | ||
@"message" : @"Webview did not load blacklisted url." | ||
}]; | ||
} | ||
decisionHandler(WKNavigationActionPolicyCancel); | ||
[self _cleanupLoadingIndicator]; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// Do not load the URL if it's on the block-list. | ||
if (_blockedURLs != nil && _blockedURLs.count > 0) { | ||
for (NSString *blockedURL in _blockedURLs) { | ||
if ([urlCandidate rangeOfString:blockedURL options:NSCaseInsensitiveSearch].location != NSNotFound) { | ||
if ([[self proxy] _hasListeners:@"blockedurl"]) { | ||
[[self proxy] fireEvent:@"blockedurl" | ||
withObject:@{ | ||
@"url" : urlCandidate, | ||
@"message" : @"Webview did not load blocked url." | ||
}]; | ||
} | ||
decisionHandler(WKNavigationActionPolicyCancel); | ||
[self _cleanupLoadingIndicator]; | ||
return; | ||
Comment on lines
-1013
to
1057
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be better to merge 'backlisteurl' event related code snippet inside 'blockedurl' . So above code snippet can be replaced by following-
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I separated the 2 on purpose so that we can easily delete the "blacklisted" code in the future. So, the way it works now is a "blacklisturl" event will only be fired for matches in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explaining. Approved! |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -487,6 +487,7 @@ describe('Titanium.UI.WebView', function () { | |
win.open(); | ||
}); | ||
|
||
// DEPRECATED: Since Titanium 9.2.0 | ||
it.ios('blacklisturl', function (finish) { | ||
win = Ti.UI.createWindow(); | ||
const webView = Ti.UI.createWebView({ | ||
|
@@ -501,6 +502,47 @@ describe('Titanium.UI.WebView', function () { | |
win.open(); | ||
}); | ||
|
||
it.ios('blockedurl', function (finish) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be ran on Android too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately no. On Android, we can only "natively" block the links the end-user tapped on. We can't block the URLs that are loaded programmatically. This is something we document. We can natively detect the URL being navigated to after the HTTP request has been sent by the |
||
win = Ti.UI.createWindow(); | ||
const webView = Ti.UI.createWebView({ | ||
url: 'https://google.com', | ||
blockedURLs: [ 'https://google.com' ] | ||
}); | ||
|
||
webView.addEventListener('blockedurl', function () { | ||
finish(); | ||
}); | ||
win.add(webView); | ||
win.open(); | ||
}); | ||
|
||
// DEPRECATED: Since Titanium 9.2.0 | ||
it('blacklistedURLs', (finish) => { | ||
win = Ti.UI.createWindow(); | ||
const webView = Ti.UI.createWebView({ | ||
url: 'https://www.axway.com', | ||
blacklistedURLs: [ 'www.apple.com', 'www.google.com' ] | ||
}); | ||
webView.addEventListener('load', () => { | ||
finish(); | ||
}); | ||
win.add(webView); | ||
win.open(); | ||
}); | ||
|
||
it('blockedURLs', (finish) => { | ||
win = Ti.UI.createWindow(); | ||
const webView = Ti.UI.createWebView({ | ||
url: 'https://www.axway.com', | ||
blockedURLs: [ 'www.apple.com', 'www.google.com' ] | ||
}); | ||
webView.addEventListener('load', () => { | ||
finish(); | ||
}); | ||
win.add(webView); | ||
win.open(); | ||
}); | ||
|
||
it.ios('basicAuthentication', function (finish) { | ||
const url = 'https://httpbin.org/basic-auth/user/password'; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call [self setBlockedURLs_: blacklistedURLs] from here.