Skip to content

Commit

Permalink
More comments and minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed Oct 1, 2020
1 parent 4975656 commit 37d0330
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
80 changes: 54 additions & 26 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

(function() {
const $ = Zepto;
// Selector for the original Crowdin Copy Source button
const copySourceElemSelector = '#translation_container #action_copy_source';
// Mac users have different keyboard shortcut
const isMac = (/mac/i).test(navigator.platform);

// Catch the keyboard shortcut specified in manifest
// and click the plugin button
chrome.runtime.onMessage.addListener(function(message) {
switch (message.action) {
case 'translate-math':
Expand All @@ -17,12 +20,16 @@
}
});

// Catch and block the Crowdin default keyboard shortcut
// NOTE: We could actually just press our button here
// but using chrome.commmands in manigest allows
// users to customize the Keyboard shortcut
// NOTE2: This will obviously not work if user customized
// their Crowdin shortcut for "Copy Source"
/**
* Catch and block the Crowdin default keyboard shortcut
*
* We could actually just press our button here
* but using chrome.commmands in manifest allows
* users to customize the Keyboard shortcut
*
* This will obviously not work if user customized
* their Crowdin shortcut for "Copy Source"
*/
$(document).on('keydown', function(e) {
// Alt+C for Linux and Win
// Ctrl+C for Mac
Expand All @@ -33,24 +40,36 @@
}
});

// Users need to set the lang manually in plugin options!
// Here we send a message to background.js to open Options
/**
* Users need to set the lang manually in plugin options!
* Here we send a message to background.js to open Options page
*
* @returns {undefined}
*/
const openOptions = function() {
chrome.runtime.sendMessage({'action': 'openOptionsPage'});
};

/**
* Get locale from user options and execute callback,
* or open options if locale is not set.
*
* @param {function} cb callback to be called with locale as parameter
* @returns {undefined}
*/
const getLocale = function(cb) {

/**
* Auxiliary function, calling callback
* once we have the locale from options
* or open options if language is not set.
* once we have the locale from chrome.storage API
* or open options if locale is not set.
*
* @param {object} result from options page
* @return {undefined}
*/
function onGet(result) {
const lang = result.locale;
// 'dec-comma' is a legacy option that we do not
// support anymore
if (lang && lang != 'dec-comma') {
cb(lang);
} else {
Expand All @@ -72,30 +91,33 @@
});
};

const initializePlugin = function() {

// Create a new button
// TODO: Move this code to a separate function createButton(parenElem);
const createPluginButton = function() {
const $translateMathBtn =
$('<button id="translate_math" tabindex="-1" class="btn btn-icon">'
+ '<i class="static-icon-copy"></i></button>');

let shortcut;
if (isMac)
shortcut = ' (Ctrl+C)';
shortcut = '(Ctrl+C)';
else
shortcut = ' (Alt+C)';
shortcut = '(Alt+C)';
const title = `Copy Source & Translate Math ${shortcut}`;
$translateMathBtn.attr('title', title);

const commaURL = chrome.runtime.getURL('5commastyle.gif');
$translateMathBtn.css('background',
`url("${commaURL}") 3px 7px no-repeat`);

// The original button
const $copySourceBtn = $(copySourceElemSelector);
// Translation area
const $translation = $('#translation');
return $translateMathBtn;
};

const initializePlugin = function() {

// Create a new button
const $translateMathBtn = createPluginButton();

// Append our button
// Append our button next to the existing buttons
const $copySourceBtn = $(copySourceElemSelector);
const $menu = $copySourceBtn.parent();
$menu.append($translateMathBtn);

Expand All @@ -104,8 +126,6 @@
$copySourceBtn.attr('title', 'Copy Source');

const copyAndTranslateMath = function(lang) {
// Click the original button
$copySourceBtn.click();

/**
* This is where we actually translate math
Expand All @@ -123,10 +143,18 @@
return translateMath(math, template, lang);
}

// Click the original button
// TODO: Are we sure the click action happens before
// the rest of the code?
$copySourceBtn.click();

// Extract string from the translation textarea
const $translation = $('#translation');
const sourceString = $translation.val();

// Unescape string to pass to Translation Assistant
// (as happens in the Khan Translation Editor)
// Here we again use the actual code from KA codebase
// (the same happens in the Khan Translation Editor)
// Here we use the actual code from KA codebase
// defined in jipt_hack.js
let translatedString = maybeUnescape(sourceString);

Expand Down
2 changes: 1 addition & 1 deletion pack_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ nlines=$(grep -n 'module.exports' translation-assistant/lib/math-translator.js |
let nlines--
head -n $nlines translation-assistant/lib/math-translator.js > KhanAcademyLibs/math-translator.js

rm -rf .git/ *.md pack_plugin.sh translation-assistant/
rm -rf node_modules/ package.json package-lock.json .eslintrc .git/ *.md pack_plugin.sh translation-assistant/
if [[ $browser = 'chrome' ]];then
# We need to exclude Firefox-specific manifest entries
grep -v -e gecko -e browser_specific_settings manifest.json > tmp
Expand Down

0 comments on commit 37d0330

Please sign in to comment.