Skip to content

Commit

Permalink
New custom payment & licensing, bug fixes, and more tests!
Browse files Browse the repository at this point in the history
Because the Chrome Webstore native payment is too restrictive (you had to have a google account to install the extension...! and in some countries the extension could not be installed...!).
Now it's handled by Stripe :)
  • Loading branch information
maximelebreton committed Jan 16, 2019
1 parent 429d88f commit add7747
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 59 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 1,11 @@
{
"name": "bar-translate",
"version": "1.1.0",
"version": "1.2.0",
"description": "",
"scripts": {
"watch": "npm run pre-watch && parcel watch src/background.js --out-dir dist --no-hmr",
"build": "npm run pre-build && parcel build src/background.js --out-dir dist --no-source-maps && npm run post-build",
"test": "jest --watchAll",
"watch": "npm run pre-watch && parcel watch src/background.js src/register.html --out-dir dist --no-hmr",
"build": "npm run pre-build && parcel build src/background.js src/register.html --out-dir dist --no-source-maps && npm run post-build",
"test": "jest --verbose --silent --watchAll",
"pre-watch": "npm run clean-dist && npm run copy-dev-manifest && npm run copy-hot-reload",
"pre-build": "npm run clean-dist && npm run copy-prod-manifest",
"post-build": "npm run zip-build",
Expand All @@ -30,6 30,9 @@
"jest": {
"roots": [
"src"
],
"setupFiles": [
"jest-webextension-mock"
]
},
"author": "Maxime Le Breton",
Expand All @@ -41,8 44,10 @@
"copyfiles": "^2.1.0",
"cross-env": "^5.2.0",
"cross-zip-cli": "^1.0.0",
"cssnano": "^4.1.8",
"fetch-mock": "^7.2.5",
"jest": "^23.6.0",
"jest-webextension-mock": "^3.5.0",
"node-fetch": "^2.3.0",
"parcel-plugin-copy": "^1.0.2",
"rimraf": "^2.6.3"
Expand Down
6 changes: 6 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,9 @@
"default": {
"message": "default"
},
"default__smallcase": {
"message": "ᴅᴇғᴀᴜʟᴛ"
},
"try": {
"message": "try"
},
Expand All @@ -35,6 38,9 @@
"syncLicense": {
"message": "Link existing license"
},
"registerLicense": {
"message": "Register your license"
},
"dontShowAnymore": {
"message": "Don't show anymore"
},
Expand Down
12 changes: 12 additions & 0 deletions src/js/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 10,24 @@ const actions = {
})
},

openRegisterPage: () => {
chrome.tabs.create({
url: chrome.runtime.getURL("register.html")
})
},

openHelpPage: () => {
chrome.tabs.create({
url: chrome.runtime.getURL("help.html")
})
},

openBuyPage: () => {
chrome.tabs.create({
url: 'https://license.maximelebreton.com/bar-translate/buy'
})
},

openChromeWebstorePage: () => {
chrome.tabs.create({
url: `https://chrome.google.com/webstore/detail/bar-translate/inigdjcpofmlcigjhhiigigihmookhcp`
Expand Down
8 changes: 5 additions & 3 deletions src/js/contextMenus/onClicked.js
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
import contextMenus from './index'
import actions from '../actions'
import licensing from '../licensing'
//import licensing from '../licensing'
import langUtils from '../utils/lang'
import notifications from '../notifications'
import translateService from '../translateService'
Expand Down Expand Up @@ -32,10 32,12 @@ export default chrome.contextMenus.onClicked.addListener(function (info, tab) {
actions.openHelpPage()
}
if (info.menuItemId === contextMenus.buyLicense) {
actions.openChromeWebstorePage()
//actions.openChromeWebstorePage()
actions.openBuyPage()
}
if (info.menuItemId === contextMenus.registerLicense) {
licensing.registerLicense()
//licensing.registerLicense()
actions.openRegisterPage()
}

});
24 changes: 14 additions & 10 deletions src/js/licensing.js
Original file line number Diff line number Diff line change
@@ -1,12 1,12 @@
import notifications from './notifications'
//import notifications from './notifications'
import storage from './storage'

var CWS_API_URL = 'https://www.googleapis.com/chromewebstore/v1.1/';
//var CWS_API_URL = 'https://www.googleapis.com/chromewebstore/v1.1/';


const licensing = {

getAuthToken: () => {
/*getAuthToken: () => {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({interactive: false}, function (token) {
Expand Down Expand Up @@ -43,16 43,16 @@ const licensing = {
return data
})
},
},*/


getLicenseMessage: (license) => {
let licenseSuccessMessage = `Your Bar Translate license has been registered!`
let licenseRejectMessage = `Sorry, but you don't have a Bar Translate license associated to this account`
if (license && license.result === true && license.accessLevel === 'FULL') {
return licenseSuccessMessage
} else {
let licenseSuccessMessage = `Your Bar Translate license is registered!`
let licenseRejectMessage = `Sorry, but there is no Bar Translate license associated with this account`
if (!license || (license && license.accessLevel === 'FREE_TRIAL')) {
return licenseRejectMessage
} else {
return licenseSuccessMessage
}
},

Expand All @@ -61,7 61,11 @@ const licensing = {
return storage.sync.getValue('barTranslate.license')
},

storeLicense: (licenseData) => {
return storage.sync.set('barTranslate.license', licenseData)
}

/*
registerLicense: () => {
return licensing.getAuthToken()
.then(token => {
Expand All @@ -81,7 85,7 @@ const licensing = {
console.info(license)
return license
})
}
}*/
}


Expand Down
4 changes: 2 additions & 2 deletions src/js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 31,7 @@ const messages = {
title: getMessage('buyNow')
},
sync: {
title: getMessage('syncLicense')
title: getMessage('registerLicense')
}
}
},
Expand All @@ -56,7 56,7 @@ const messages = {
title: getMessage('buyLicense')
},
license: {
title: getMessage('syncLicense')
title: getMessage('registerLicense')
}

},
Expand Down
13 changes: 8 additions & 5 deletions src/js/notifications/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 3,23 @@ import langUtils from '../utils/lang.js'



describe('Test supported lanuages translations', () => {

it("Returns language traduction for each supported language", function () {
describe('Supported languages translations', () => {

it("Returns hello traduction for each supported language", function () {
let missingLanguages = []
chromeLanguages.forEach((lang) => {

let hello = langUtils.getHelloFromLangOrAlias(lang)
expect(hello).toBeDefined()
if (hello) {
expect(hello).toBeDefined()

} else {
console.warn(`${hello} for ${lang}`)
missingLanguages.push(lang)

}

})
console.warn(`missing hello for ${missingLanguages}`)

})

Expand Down
7 changes: 4 additions & 3 deletions src/js/notifications/onButtonClicked.js
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
import notifications from './index'
import actions from '../actions'
import licensing from '../licensing'
//import licensing from '../licensing'
import storage from '../storage'
import textUtils from '../utils/text'

Expand All @@ -10,10 10,11 @@ export default chrome.notifications.onButtonClicked.addListener((notificationId,

if (buttonIndex === 0) {
chrome.notifications.clear(notifications.tryLoveBuy.id)
actions.openChromeWebstorePage()
actions.openBuyPage()
}
if (buttonIndex === 1) {
licensing.registerLicense()
//licensing.registerLicense()
actions.openRegisterPage()
chrome.notifications.clear(notifications.tryLoveBuy.id)
}

Expand Down
5 changes: 3 additions & 2 deletions src/js/omnibox/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,7 @@ const messages = {

let toPrefix = sourceLanguage && !isDetectedSourceLanguage ? `${chrome.i18n.getMessage('into')} ` : `${chrome.i18n.getMessage('in')} `
let toSuffix = `<dim>${isDefault ? ` (${chrome.i18n.getMessage('default')})` : ''}</dim>`

let to = `<match>${toPrefix}<url>${targetLanguageName}</url></match>${toSuffix}`

return `${translate}${match}${from}${to}`
Expand All @@ -33,7 34,7 @@ const messages = {
},*/

getSuggestDescription(languageName, languageSlice, langName, langSlice, aliasName, aliasSlice) {
return `<match><dim>?</dim></match>${space.repeat(2)}Translate in <url><match>${languageName.slice(0, languageSlice.length)}</match>${languageName.slice(languageSlice.length, languageName.length)}</url> <dim>(<match>${!aliasSlice ? langName.slice(0, langSlice.length) : ''}</match>${!aliasSlice ? langName.slice(langSlice.length, langName.length) : ''}<match>${aliasSlice ? aliasName.slice(0, aliasSlice.length) : ''}</match>${aliasSlice ? aliasName.slice(aliasSlice.length, aliasName.length) : ''})</dim>`
return `<match><dim>?</dim></match>${space.repeat(2)}Translate in <url><match>${languageName.slice(0, languageSlice.length)}</match>${languageName.slice(languageSlice.length, languageName.length)}</url> <dim>(<match>${!aliasSlice ? langName.slice(0, langSlice.length) : ''}</match>${!aliasSlice ? langName.slice(langSlice.length, langName.length) : ''}<match>${aliasSlice ? aliasName.slice(0, aliasSlice.length) : ''}</match>${aliasSlice ? aliasName.slice(aliasSlice.length, aliasName.length) : ''})</dim>`
},

/*getFromLanguageSuggestDescription(languageName, languageSlice, langName, langSlice) {
Expand Down Expand Up @@ -65,7 66,7 @@ const messages = {
//let arrow = `—›`
//let prefix = `<dim>${sourceLanguage ? `${sourceLanguage}` : ''}</dim><match></match><dim> › ${targetLanguage}</dim>`
let space = ` `
return `${space}<match><dim>=</dim></match>${space}<url><match>${translatedText}</match></url> <dim>${space}ᴛʀᴀɴsʟᴀᴛᴇᴅ ʙʏ ${translateService.smallcase}</dim>`
return `${space}<match><dim>=</dim></match>${space}<url><match>${translatedText}</match></url><dim>${space.repeat(2)}${chrome.i18n.getMessage('translatedBy__smallcase')} ${translateService.smallcase}</dim>`
},

getErrorDescription(sourceLanguage, targetLanguage, query, translateService) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/omnibox/onInputChanged.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,7 @@ let abortOnChangeDebounce = false

const getTranslationsSuggests = (sourceLanguage, targetLanguage, query, translations, translateService) => {
return translations.map(function (translation) {
// because suggest doesn't display if content attr already exist in suggests, and somestimes the input query match the translated result
let content = translation.translatedText === query ? `${translation.translatedText}${textUtils.zeroWidthSpace}` : translation.translatedText

return {
Expand Down Expand Up @@ -193,7 194,6 @@ let debounceOnChange = debounce(onChange, 300)


const onInputChangedListener = (text, suggest) => {

let {sourceLanguage, targetLanguage, query, isDefault} = langUtils.extractLanguageFromQuery(text)
messages.defaultDescription = messages.getDefaultDescription(sourceLanguage, targetLanguage, query, isDefault)

Expand Down
2 changes: 1 addition & 1 deletion src/js/omnibox/onInputStarted.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,7 @@ export default chrome.omnibox.onInputStarted.addListener(function() {
.then(license => {
console.info(license, 'license')
console.info(licensing.getLicenseMessage(license))
if (!license || (license && license.accessLevel !== 'FULL')) {
if (!license || (license && license.accessLevel === 'FREE_TRIAL')) {

//notifications.show('tryLoveBuy')
storage.local.getValue(`${notifications.tryLoveBuy.id}.${storage.states.alreadyNotified}`)
Expand Down
5 changes: 3 additions & 2 deletions src/js/translateService/googleScriptService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 9,16 @@ const googleScriptService = () => {
getUrl: (sourceLanguage, targetLanguage, query) => {
const path = 'https://script.google.com/macros/s/AKfycbyCMrxkscWQ_fqmz_G0jBp44qzSitIu37RcsUVznA4M-zSP2tI/exec'
const params = {
'sl': sourceLanguage,
'sl': sourceLanguage ? sourceLanguage : '',
'tl': targetLanguage,
'q': encodeURI(query)
}
return googleScriptService().getUrlWithParams(path, params)
},

getSiteUrl: (sourceLanguage, targetLanguage, query) => {
return `https://translate.google.com/#view=home&op=translate&sl=${sourceLanguage}&tl=${targetLanguage}&text=${encodeURI(query)}`
console.log(sourceLanguage)
return `https://translate.google.com/#view=home&op=translate&sl=${sourceLanguage ? sourceLanguage : ''}&tl=${targetLanguage}&text=${encodeURI(query)}`
},

getNormalizedData: (data) => {
Expand Down
15 changes: 13 additions & 2 deletions src/js/translateService/googleTranslateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 19,29 @@ const googleTranslateService = () => {
},

getSiteUrl: (sourceLanguage, targetLanguage, query) => {
console.log(sourceLanguage)
return `https://translate.google.com/#view=home&op=translate&sl=${sourceLanguage ? sourceLanguage : 'auto'}&tl=${targetLanguage}&text=${encodeURI(query)}`
},

getNormalizedData: (data) => {
const translations = data[0]
const detectedSourceLanguage = data[2]
const normalizedData = translations.map(translation => {

// because google translate api split every sentence after a dot (.), so we need to re-concate them
let concatedTranslatedText = ''
translations.forEach(translation => {
concatedTranslatedText = translation[0]
})
const normalizedData = [{
translatedText: concatedTranslatedText,
detectedSourceLanguage: detectedSourceLanguage
}]
/*const normalizedData = translations.map(translation => {
return {
translatedText: translation[0],
detectedSourceLanguage: detectedSourceLanguage
}
})
})*/
return normalizedData
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/js/translateService/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 47,18 @@ describe('Check Services', () => {
Object.keys(translateService.list).forEach(function (id) {
let currentTranslateService = translateService.list[id]

let urlWithoutSource = currentTranslateService.getUrl(null, targetLanguage, query)
it(urlWithoutSource, () => {
if (id === 'googleTranslate') {
expect(urlWithoutSource).toMatch('auto')
} else {
expect(urlWithoutSource).toMatch('')
}
expect(urlWithoutSource).toMatch(targetLanguage)
expect(urlWithoutSource).toMatch(query)
})


let url = currentTranslateService.getUrl(sourceLanguage, targetLanguage, query)
it(url, () => {
expect(url).toMatch(sourceLanguage)
Expand Down
4 changes: 2 additions & 2 deletions src/js/translateService/yandexService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,7 @@ const yandexService = () => {
smallcase: 'ʏᴀɴᴅᴇx',

getUrl: (sourceLanguage, targetLanguage, query) => {
sourceLanguage = sourceLanguage.split('-')[0]
sourceLanguage = sourceLanguage ? sourceLanguage.split('-')[0] : null
targetLanguage = targetLanguage.split('-')[0]

const path = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
Expand All @@ -20,7 20,7 @@ const yandexService = () => {
},

getSiteUrl: (sourceLanguage, targetLanguage, query) => {
sourceLanguage = sourceLanguage.split('-')[0]
sourceLanguage = sourceLanguage ? sourceLanguage.split('-')[0] : null
targetLanguage = targetLanguage.split('-')[0]
return `https://translate.yandex.com/?lang=${sourceLanguage ? sourceLanguage '-' : ''}${targetLanguage}&text=${encodeURI(query)}`
},
Expand Down
Loading

0 comments on commit add7747

Please sign in to comment.