Skip to content
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

JS API to open card browser and search with query #9247

Merged
merged 1 commit into from
Aug 5, 2021

Conversation

krmanik
Copy link
Member

@krmanik krmanik commented Jul 11, 2021

Pull Request template

Purpose / Description

This is continuation of JS API features. From reviewer using js function/button in one click it will go to CardBrowser and search for cards using query.

Fixes

Fixes #8109

Approach

  1. Added a JSInterface function and used intent to call CardBrowser
  2. Get string extra in CardBrowser and assign query to mSearchTerms then call searchCards()

How Has This Been Tested?

Testesd on devices and emulator

  1. Add a button in card template to call AnkiDroidJS.ankiSearchCard(query)
  2. Add query like, this query are same as entering in CardBrowser
'deck:"deck name" {{front}}'
'deck:"test" flag:2 tag:marked'
'deck:"test" flag:2 tag:marked is:suspended'
'deck:"test" flag:2 tag:marked search text here'
  1. Click button to run the query
<button onclick="AnkiDroidJS.ankiSearchCard('deck:\"test\" {{hanzi}}')">Search this in Card browser</button>

video

Note in this video: Add AnkiDroidJS. before ankiSearchCard because JSInterface used to call this function.

video.mp4

Another example, Added following to front side of card template

<button onclick="searchCard();">{{Simplified}}</button>     //  <-----  note this line

<script>
function searchCard() {
AnkiDroidJS.ankiSearchCard('deck:"xiehanzi HSK 3.0" {{Simplified}}');  //  <-----  note this line
}
</script>

<div class="text-color1">{{Pinyin}}</div>
<div class="text-color3">{{Meaning}}</div>

image

Learning (optional, can help others)

https://github.com/ankidroid/Anki-Android/wiki/AnkiDroid-Javascript-API

Checklist

Please, go through these checks before submitting the PR.

  • You have not changed whitespace unnecessarily (it makes diffs hard to read)
  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • Your code follows the style of the project (e.g. never omit braces in if statements)
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea, but as mentioned in comments, split into 2 commits where one alters the API and all callers to always use Nullable query string to open card browser as a small simple change, then second commit layers the new Javascript API on top of it using the new capability

@mikehardy mikehardy added the Needs Author Reply Waiting for a reply from the original author label Jul 17, 2021
@krmanik
Copy link
Member Author

krmanik commented Jul 17, 2021

I like this idea, but as mentioned in comments, split into 2 commits where one alters the API and all callers to always use Nullable query string to open card browser as a small simple change, then second commit layers the new Javascript API on top of it using the new capability

I will split into two PR. Thanks

@mikehardy
Copy link
Member

For this one could just be 2 commits I think - in this same PR, that would work for me, these ones will be small

@krmanik
Copy link
Member Author

krmanik commented Jul 17, 2021

For this one could just be 2 commits I think - in this same PR, that would work for me, these ones will be small

Okay, I will do it.

@mikehardy mikehardy added Needs Review and removed Needs Author Reply Waiting for a reply from the original author labels Aug 5, 2021
@mikehardy mikehardy self-requested a review August 5, 2021 16:27
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use @JavascriptInterface in this one as well? Like in #9245 ?

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@mikehardy
Copy link
Member

Has a conflict with your other PR though 😅

@krmanik
Copy link
Member Author

krmanik commented Aug 5, 2021

Yes, I think I need to rebase it.

I also thinking about implementing in signal. Because here are two types of function

  • Functions that get parameter(s) - ankiShowToast, ankiMarkCard etc.
  • Functions that do not get parameter(s) - All JSInterface functions

So, should we make a rule that function that needs parameter(s) should go through signal and the function which do no require parameter(s) should go though JSInterface?

As security concerns discussed in PR comments #9245

@mikehardy
Copy link
Member

  • I am not aware of any security concerns for JavascriptInterface anymore (android 5+). Arguments, no arguments, no matter.
  • I am under the impression everything should be JavascriptInterface, I expect this to be JavascriptInterface
  • The commit history here is not good. There is no reason to have more than one commit here, containing nothing but the commit that adds the JavascriptInterface. Rebase is what you need to get there

@krmanik
Copy link
Member Author

krmanik commented Aug 5, 2021

Okay, I am on it.

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@mikehardy mikehardy added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Review labels Aug 5, 2021
@mikehardy mikehardy added this to the 2.16 release milestone Aug 5, 2021
@mikehardy mikehardy merged commit 12f24b9 into ankidroid:master Aug 5, 2021
@mikehardy mikehardy removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Aug 5, 2021
@krmanik krmanik deleted the jsapi-search-card branch August 5, 2021 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

js api -> ability to open card browser (the card search) from js api with ankiSearch("deck:foo myField:bar")
2 participants