-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
convert synchronous js api to asynchronous and remove js interface #14564
Merged
+602
−729
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
06e1a88
convert synchronous js api to asynchronous and remove js interface
krmanik daa9a00
add docs to method
krmanik 9195c17
check contract for each api request and remove redudant init method
krmanik 79956b4
update junit test for js api
krmanik d834552
move api to one class and update tests
krmanik 3038eea
do not check again for api contract
krmanik b792bda
refactor js api, remove redudant check
krmanik 78fe106
refactor js api, return result in json format (success, value)
krmanik a8baf8e
update js unit test
krmanik 89167ce
parse api contract to api data class
krmanik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* AnkiDroid JavaScript API | ||
* Version: 0.0.2 | ||
*/ | ||
|
||
/** | ||
* jsApiList | ||
* | ||
* name: method name | ||
* value: endpoint | ||
*/ | ||
const jsApiList = { | ||
ankiGetNewCardCount: "newCardCount", | ||
ankiGetLrnCardCount: "lrnCardCount", | ||
ankiGetRevCardCount: "revCardCount", | ||
ankiGetETA: "eta", | ||
ankiGetCardMark: "cardMark", | ||
ankiGetCardFlag: "cardFlag", | ||
ankiGetNextTime1: "nextTime1", | ||
ankiGetNextTime2: "nextTime2", | ||
ankiGetNextTime3: "nextTime3", | ||
ankiGetNextTime4: "nextTime4", | ||
ankiGetCardReps: "cardReps", | ||
ankiGetCardInterval: "cardInterval", | ||
ankiGetCardFactor: "cardFactor", | ||
ankiGetCardMod: "cardMod", | ||
ankiGetCardId: "cardId", | ||
ankiGetCardNid: "cardNid", | ||
ankiGetCardType: "cardType", | ||
ankiGetCardDid: "cardDid", | ||
ankiGetCardLeft: "cardLeft", | ||
ankiGetCardODid: "cardODid", | ||
ankiGetCardODue: "cardODue", | ||
ankiGetCardQueue: "cardQueue", | ||
ankiGetCardLapses: "cardLapses", | ||
ankiGetCardDue: "cardDue", | ||
ankiIsInFullscreen: "isInFullscreen", | ||
ankiIsTopbarShown: "isTopbarShown", | ||
ankiIsInNightMode: "isInNightMode", | ||
ankiIsDisplayingAnswer: "isDisplayingAnswer", | ||
ankiGetDeckName: "deckName", | ||
ankiIsActiveNetworkMetered: "isActiveNetworkMetered", | ||
ankiTtsFieldModifierIsAvailable: "ttsFieldModifierIsAvailable", | ||
ankiTtsIsSpeaking: "ttsIsSpeaking", | ||
ankiTtsStop: "ttsStop", | ||
ankiBuryCard: "buryCard", | ||
ankiBuryNote: "buryNote", | ||
ankiSuspendCard: "suspendCard", | ||
ankiSuspendNote: "suspendNote", | ||
ankiAddTagToCard: "addTagToCard", | ||
ankiResetProgress: "resetProgress", | ||
ankiMarkCard: "markCard", | ||
ankiToggleFlag: "toggleFlag", | ||
ankiSearchCard: "searchCard", | ||
ankiSearchCardWithCallback: "searchCardWithCallback", | ||
ankiTtsSpeak: "ttsSpeak", | ||
ankiTtsSetLanguage: "ttsSetLanguage", | ||
ankiTtsSetPitch: "ttsSetPitch", | ||
ankiTtsSetSpeechRate: "ttsSetSpeechRate", | ||
ankiEnableHorizontalScrollbar: "enableHorizontalScrollbar", | ||
ankiEnableVerticalScrollbar: "enableVerticalScrollbar", | ||
ankiSetCardDue: "setCardDue", | ||
}; | ||
|
||
class AnkiDroidJS { | ||
constructor({ developer, version }) { | ||
this.developer = developer; | ||
this.version = version; | ||
this.handleRequest(`init`); | ||
} | ||
|
||
static init({ developer, version }) { | ||
return new AnkiDroidJS({ developer, version }); | ||
} | ||
|
||
handleRequest = async (endpoint, data) => { | ||
const url = `/jsapi/${endpoint}`; | ||
try { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
developer: this.developer, | ||
version: this.version, | ||
data, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to make the request"); | ||
} | ||
|
||
const responseData = await response.text(); | ||
if (endpoint.includes("nextTime") || endpoint.includes("deckName")) { | ||
return responseData; | ||
} | ||
return JSON.parse(responseData); | ||
} catch (error) { | ||
console.error("Request error:", error); | ||
throw error; | ||
} | ||
}; | ||
} | ||
|
||
Object.keys(jsApiList).forEach(method => { | ||
if (method === "ankiTtsSpeak") { | ||
AnkiDroidJS.prototype[method] = async function (text, queueMode = 0) { | ||
const endpoint = jsApiList[method]; | ||
const data = JSON.stringify({ text, queueMode }); | ||
return await this.handleRequest(endpoint, data); | ||
}; | ||
return; | ||
} | ||
AnkiDroidJS.prototype[method] = async function (data) { | ||
const endpoint = jsApiList[method]; | ||
return await this.handleRequest(endpoint, data); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should rethink the names here:
searchCard
opens the browsersearchCardWithCallback
performs a search and returns resultsThere 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.
During implementation of api, only searchCard are implemented but later user wanted result in reviewer also, so used call back, poor naming choice, it can be solved in separate PR, with better naming, because we are upgrading api, so it can also be upgraded.