Skip to content

Commit

Permalink
Merge pull request #28 from andrewdotn/custom-api-url
Browse files Browse the repository at this point in the history
embedded: allow custom API URL
  • Loading branch information
andrewdotn authored Apr 7, 2021
2 parents 9a71931 + 923734b commit 55d5393
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/embedded/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const addSaveOptionHandler = () => {
return
}

// Set `window.clickInTextApiUrl` before `require()`ing `embedded.js` to point
// at a custom server
const apiUrl : string | undefined = (window as any).clickInTextApiUrl;

const asyncGetTranslation = (word, callback) => {
Core.callAPI(word, Core.parseAPIResponse, callback)
Core.callAPI(word, Core.parseAPIResponse, callback, {apiUrl})
}

const addTATAndCopyPasteListener = function (callback) {
Expand Down Expand Up @@ -44,4 +47,4 @@ const grayOutIcon = () => {
}


Core.start(addSaveOptionHandler, asyncGetTranslation, addTATAndCopyPasteListener, disable, grayOutIcon)
Core.start(addSaveOptionHandler, asyncGetTranslation, addTATAndCopyPasteListener, disable, grayOutIcon)
18 changes: 14 additions & 4 deletions src/lib/transover_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,20 @@ type ResponseParser = (data: { "results": SerializedSearchResult[] }, word: stri
const Core = {

// where translate api happens
callAPI(word: string, responseParser: ResponseParser, sendParsedResponse: SendParsedResponse): void {
callAPI(
word: string,
responseParser: ResponseParser,
sendParsedResponse: SendParsedResponse,
options?: {apiUrl: string}
): void {
const apiUrl = options?.apiUrl ?? 'https://itwewina.altlab.app/click-in-text/?q=';

if (!word) {
return;
}

const options = {
url: 'https://itwewina.altlab.app/click-in-text/?q=' + word,
const ajaxRequestSettings = {
url: apiUrl + encodeURIComponent(word),
dataType: 'json',
success: function on_success(data) {
const parsedResponse = responseParser(data, word)
Expand All @@ -107,7 +117,7 @@ const Core = {
console.error({e: e, xhr: xhr})
}
}
$.ajax(options)
$.ajax(ajaxRequestSettings)
},

parseAPIResponse(data: { "results": SerializedSearchResult[] }, word: string): ParsedResponse {
Expand Down

0 comments on commit 55d5393

Please sign in to comment.