-
Notifications
You must be signed in to change notification settings - Fork 445
Fix #627 Add brave://search URL scheme #3582
Changes from 3 commits
2cf8ab0
a284191
219c74b
58026b2
d569892
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 |
---|---|---|
|
@@ -14,6 +14,10 @@ class ShareToBraveViewController: SLComposeServiceViewController { | |
return URL(string: "brave://open-url?url=\(url)") | ||
} | ||
|
||
private func searchScheme(for text: String) -> URL? { | ||
return URL(string: "brave://search?q=\(text)") | ||
} | ||
|
||
override func configurationItems() -> [Any]! { | ||
guard let inputItems = extensionContext?.inputItems as? [NSExtensionItem] else { | ||
return [] | ||
|
@@ -35,22 +39,31 @@ class ShareToBraveViewController: SLComposeServiceViewController { | |
|
||
provider.loadItem(of: provider.isUrl ? kUTTypeURL : kUTTypeText) { item, error in | ||
var urlItem: URL? | ||
var nonUrlText: String? | ||
|
||
// We can get urls from other apps as a kUTTypeText type, for example from Apple's mail.app. | ||
if let text = item as? String { | ||
urlItem = text.firstURL | ||
if let url = text.firstURL { | ||
urlItem = url | ||
} else { | ||
nonUrlText = text | ||
} | ||
} else if let url = item as? URL { | ||
urlItem = url.absoluteString.firstURL | ||
} else { | ||
self.cancel() | ||
return | ||
} | ||
|
||
// Just open the app if we don't find a url. In the future we could | ||
// use this entry point to search instead of open a given URL | ||
let urlString = urlItem?.absoluteString ?? "" | ||
if let braveUrl = urlString.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.urlScheme) { | ||
self.handleUrl(braveUrl) | ||
// Open url if it was found, in other case search for text with default search engine in browser | ||
if let urlString = urlItem?.absoluteString { | ||
if let braveUrl = urlString.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.urlScheme) { | ||
self.handleUrl(braveUrl) | ||
} | ||
} else if let text = nonUrlText { | ||
if let braveUrl = text.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.searchScheme) { | ||
self.handleUrl(braveUrl) | ||
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. This part is too duplicated, what do you think about adding new This way you won't need to call 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. Good idea, I'll solve it this way |
||
} | ||
} | ||
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 didn't remove one of the .addingPercentEncoding parts since later there is a .flatMap part which is different in both cases, but I managed to make it little simpler |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -386,7 +386,7 @@ class TopToolbarView: UIView, ToolbarProtocol { | |
if search { | ||
locationTextField?.text = text | ||
// Not notifying when empty agrees with AutocompleteTextField.textDidChange. | ||
delegate?.topToolbar(self, didEnterText: text) | ||
delegate?.topToolbar(self, didSubmitText: text) | ||
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. What is this change needed for? 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 noticed that without this change, view is stuck with empty url bar and doesn't submit searching. Thanks for your comment, I haven't realized it breaks other part of the app, will try to solve that in different way. |
||
} else { | ||
locationTextField?.setTextWithoutSearching(text) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,14 @@ class NavigationRouterTests: XCTestCase { | |
XCTAssertEqual(badNav, NavigationPath.url(webURL: URL(string: "blah"), isPrivate: false)) | ||
} | ||
|
||
func testSearchScheme() { | ||
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. Thank you for adding unit test for it |
||
let query = "Foo Bar".addingPercentEncoding(withAllowedCharacters: .alphanumerics)! | ||
let appURL = "\(appScheme)://search?q="+query | ||
let navItem = NavigationPath(url: URL(string: appURL)!)! | ||
|
||
XCTAssertEqual(navItem, NavigationPath.text("Foo Bar")) | ||
} | ||
|
||
func testDefaultNavigationPath() { | ||
let url = URL(string: "https://duckduckgo.com")! | ||
let appURL = URL(string: "\(self.appScheme)://open-url?url=\(url.absoluteString.escape()!)")! | ||
|
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.
This work, but I think we can make the code cleaner now, if you look at logic below that, there is a lot of nested ifs and the code is not super readable,
For us differences between text and url are small, we can remove code duplication a little and improve architecture
What do you think if we create a new structure to
ShareToBraveViewController
class holding either URL or text and handling the scheme properly,Sample code
Then calling it from
provider.loadItem
is much cleaner, you can simply do:what do you think about it?