From fba05c2faebbc25327f1967352da76c45bd71b58 Mon Sep 17 00:00:00 2001 From: Kevin Grandon Date: Mon, 26 Oct 2015 16:27:18 -0700 Subject: [PATCH] Format javascript according to StandardJS spec. --- .../main/assets/pagescripts/FetchContent.js | 6 +- .../main/assets/pagescripts/SelectElements.js | 130 +++++++++--------- .../src/main/assets/pagescripts/ThemeColor.js | 12 +- .../src/main/assets/pagescripts/TouchIcon.js | 40 +++--- .../src/main/assets/pagescripts/YouTube.js | 41 +++--- package.json | 6 +- scripts/updateTranslations.js | 83 ++++++----- 7 files changed, 160 insertions(+), 158 deletions(-) diff --git a/Application/LinkBubble/src/main/assets/pagescripts/FetchContent.js b/Application/LinkBubble/src/main/assets/pagescripts/FetchContent.js index f72958b32..a10f6c8af 100644 --- a/Application/LinkBubble/src/main/assets/pagescripts/FetchContent.js +++ b/Application/LinkBubble/src/main/assets/pagescripts/FetchContent.js @@ -1,3 +1,3 @@ -(function() { - window.LinkBubble.fetchHtml(document.documentElement.outerHTML, String(window.location)); -})(); +;(function () { + window.LinkBubble.fetchHtml(document.documentElement.outerHTML, String(window.location)) +})() diff --git a/Application/LinkBubble/src/main/assets/pagescripts/SelectElements.js b/Application/LinkBubble/src/main/assets/pagescripts/SelectElements.js index 6c1bfa0a4..b95fcef06 100644 --- a/Application/LinkBubble/src/main/assets/pagescripts/SelectElements.js +++ b/Application/LinkBubble/src/main/assets/pagescripts/SelectElements.js @@ -1,68 +1,68 @@ -(function() { - if (window.LinkBubble.selectOption) { return; } - LinkBubble.lastSelectFocused = null; - LinkBubble.selectOption = function(index) { - var select = LinkBubble.lastSelectFocused; - select.selectedIndex = index; - select.previousElementSibling.textContent = select[index].text; - }; - var positioningProps = ['float','position','width','height','left','top','margin-left','margin-top','padding-left','padding-top', 'border', 'background']; - var els = document.getElementsByTagName('select'); - function maskSelects() { - /* Remove all previous select masks if the next element is not a select any longer. */ - Array.prototype.forEach.call(document.querySelectorAll('.__link_bubble__select_mask__'), function(mask) { - if (mask.nextElementSibling && mask.nextElementSibling.nodeName.toLowerCase() === 'select') { return; }; - mask.parentNode.removeChild(mask); - }); +;(function () { + if (window.LinkBubble.selectOption) { return } + window.LinkBubble.lastSelectFocused = null + window.LinkBubble.selectOption = function (index) { + var select = window.LinkBubble.lastSelectFocused + select.selectedIndex = index + select.previousElementSibling.textContent = select[index].text + } + var positioningProps = ['float', 'position', 'width', 'height', 'left', 'top', 'margin-left', 'margin-top', 'padding-left', 'padding-top', 'border', 'background'] + var els = document.getElementsByTagName('select') + function maskSelects () { + /* Remove all previous select masks if the next element is not a select any longer. */ + Array.prototype.forEach.call(document.querySelectorAll('.__link_bubble__select_mask__'), function (mask) { + if (mask.nextElementSibling && mask.nextElementSibling.nodeName.toLowerCase() === 'select') { return } + mask.parentNode.removeChild(mask) + }) - Array.prototype.forEach.call(els, function(select) { - var mask = select.previousElementSibling; - /* Insert and style for new selects */ - if (!mask || mask.className !== '__link_bubble__select_mask__') { - mask = document.createElement('div'); - mask.className = '__link_bubble__select_mask__'; - mask.style.webkitAppearance = 'menulist'; - var computedStyle = getComputedStyle(select); + Array.prototype.forEach.call(els, function (select) { + var mask = select.previousElementSibling + /* Insert and style for new selects */ + if (!mask || mask.className !== '__link_bubble__select_mask__') { + mask = document.createElement('div') + mask.className = '__link_bubble__select_mask__' + mask.style.webkitAppearance = 'menulist' + var computedStyle = window.getComputedStyle(select) - for(var i in positioningProps){ - var prop = positioningProps[i]; - mask.style[prop] = computedStyle.getPropertyValue(prop); - } - select.parentNode.insertBefore(mask, select); - select.style.display = 'none'; + for (var i in positioningProps) { + var prop = positioningProps[i] + mask.style[prop] = computedStyle.getPropertyValue(prop) + } + select.parentNode.insertBefore(mask, select) + select.style.display = 'none' - mask.addEventListener('click', function(e) { - e.preventDefault(); - LinkBubble.lastSelectFocused = select; - var keyAndValues = [select.selectedIndex]; - for (var i = 0; i < select.length; i++) { - keyAndValues.push(select[i].text); - keyAndValues.push(select[i].value); - } - LinkBubble.onSelectElementInteract(JSON.stringify(keyAndValues)); - }); - } - mask.textContent = select[select.selectedIndex].text; - }); - } - /* Mask all selects when the script is injected. */ - maskSelects(); - /* Use a mutation observer for dynamic selects added after page load. */ - MutationObserver = window.MutationObserver || window.WebKitMutationObserver; - var observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - var changed = false; - var allChangedNodes = [].slice.call(mutation.addedNodes).concat([].slice.call(mutation.removedNodes)); - allChangedNodes.forEach(function(changedNode) { - if ((changedNode.querySelector && changedNode.querySelector('select')) || changedNode.nodeName.toLowerCase() === 'select') { - changed = true; - } - }); - if (changed) { - maskSelects(); - } - }); - }); - var config = {attributes: false, childList: true, characterData: false, subtree: true}; - observer.observe(document, config); -})(); + mask.addEventListener('click', function (e) { + e.preventDefault() + window.LinkBubble.lastSelectFocused = select + var keyAndValues = [select.selectedIndex] + for (var i = 0; i < select.length; i++) { + keyAndValues.push(select[i].text) + keyAndValues.push(select[i].value) + } + window.LinkBubble.onSelectElementInteract(JSON.stringify(keyAndValues)) + }) + } + mask.textContent = select[select.selectedIndex].text + }) + } + /* Mask all selects when the script is injected. */ + maskSelects() + /* Use a mutation observer for dynamic selects added after page load. */ + var MutationObserver = window.MutationObserver || window.WebKitMutationObserver + var observer = new MutationObserver(function (mutations) { + mutations.forEach(function (mutation) { + var changed = false + var allChangedNodes = [].slice.call(mutation.addedNodes).concat([].slice.call(mutation.removedNodes)) + allChangedNodes.forEach(function (changedNode) { + if ((changedNode.querySelector && changedNode.querySelector('select')) || changedNode.nodeName.toLowerCase() === 'select') { + changed = true + } + }) + if (changed) { + maskSelects() + } + }) + }) + var config = {attributes: false, childList: true, characterData: false, subtree: true} + observer.observe(document, config) +})() diff --git a/Application/LinkBubble/src/main/assets/pagescripts/ThemeColor.js b/Application/LinkBubble/src/main/assets/pagescripts/ThemeColor.js index e28770802..ea9ff13a7 100644 --- a/Application/LinkBubble/src/main/assets/pagescripts/ThemeColor.js +++ b/Application/LinkBubble/src/main/assets/pagescripts/ThemeColor.js @@ -1,6 +1,6 @@ -(function() { - var themeColorTag = document.getElementsByTagName('meta')['theme-color']; - if (themeColorTag) { - LinkBubble.onThemeColor(themeColorTag.getAttribute('content')); - } -})(); +;(function () { + var themeColorTag = document.getElementsByTagName('meta')['theme-color'] + if (themeColorTag) { + window.LinkBubble.onThemeColor(themeColorTag.getAttribute('content')) + } +})() diff --git a/Application/LinkBubble/src/main/assets/pagescripts/TouchIcon.js b/Application/LinkBubble/src/main/assets/pagescripts/TouchIcon.js index 1801e3303..134c401c4 100644 --- a/Application/LinkBubble/src/main/assets/pagescripts/TouchIcon.js +++ b/Application/LinkBubble/src/main/assets/pagescripts/TouchIcon.js @@ -1,21 +1,21 @@ -(function() { - var links = document.head.getElementsByTagName('link'); - var linksArray = null; - var linksCount = 0; - for(var link in links){ - if(links.hasOwnProperty(link)){ - var l = links[link]; - if (l.rel != null && l.rel.indexOf('apple-touch-icon') != -1) { - if (linksArray == null) { - linksArray = new Array(); - } - var s = "@@@" + l.rel + "," + l.href + "," + l.sizes + "###"; - linksArray[linksCount] = s; - linksCount++; - } +;(function () { + var links = document.head.getElementsByTagName('link') + var linksArray = null + var linksCount = 0 + for (var link in links) { + if (links.hasOwnProperty(link)) { + var l = links[link] + if (l.rel != null && l.rel.indexOf('apple-touch-icon') !== -1) { + if (linksArray == null) { + linksArray = [ ] } - } - if (linksCount > 0) { - LinkBubble.onTouchIconLinks(linksArray.toString()); - } -})(); + var s = '@@@' + l.rel + ',' + l.href + ',' + l.sizes + '###' + linksArray[linksCount] = s + linksCount++ + } + } + } + if (linksCount > 0) { + window.LinkBubble.onTouchIconLinks(linksArray.toString()) + } +})() diff --git a/Application/LinkBubble/src/main/assets/pagescripts/YouTube.js b/Application/LinkBubble/src/main/assets/pagescripts/YouTube.js index 1e1ad4f5b..b39747b80 100644 --- a/Application/LinkBubble/src/main/assets/pagescripts/YouTube.js +++ b/Application/LinkBubble/src/main/assets/pagescripts/YouTube.js @@ -1,23 +1,24 @@ -(function() { - function detectYoutubeEmbeds() { - var elems = document.getElementsByTagName('*'), i; - var YOUTUBE_EMBED_PREFIX = "//www.youtube.com/embed/"; - var resultArray = null; - var resultCount = 0; - for (i in elems) { - var elem = elems[i]; - if (elem.src != null && elem.src.indexOf(YOUTUBE_EMBED_PREFIX) != -1) { - if (resultArray == null) { - resultArray = new Array(); - } - resultArray[resultCount] = elem.src; - resultCount++; - } - } - if (resultCount > 0) { - LinkBubble.onYouTubeEmbeds(resultArray.toString()); +;(function () { + function detectYoutubeEmbeds () { + var elems = document.getElementsByTagName('*') + var i + var YOUTUBE_EMBED_PREFIX = '//www.youtube.com/embed/' + var resultArray = null + var resultCount = 0 + for (i in elems) { + var elem = elems[i] + if (elem.src != null && elem.src.indexOf(YOUTUBE_EMBED_PREFIX) !== -1) { + if (resultArray == null) { + resultArray = [ ] } + resultArray[resultCount] = elem.src + resultCount++ + } + } + if (resultCount > 0) { + window.LinkBubble.onYouTubeEmbeds(resultArray.toString()) } + } - detectYoutubeEmbeds(); -})(); + detectYoutubeEmbeds() +})() diff --git a/package.json b/package.json index 0381b1a8f..2ef4f1eca 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "version": "0.0.0", "description": "Link Bubble Browser", "scripts": { - "translate": "babel-node scripts/updateTranslations.js" + "translate": "babel-node scripts/updateTranslations.js", + "lint": "standard scripts/* Application/LinkBubble/src/main/assets/pagescripts/*" }, "repository": { "type": "git", @@ -21,6 +22,7 @@ "url": "https://github.com/brave/link-bubble/issues" }, "devDependencies": { - "babel": "^5.8.21" + "babel": "^5.8.21", + "standard": "^5.3.1" } } diff --git a/scripts/updateTranslations.js b/scripts/updateTranslations.js index ed38b5c3a..283bea6ed 100644 --- a/scripts/updateTranslations.js +++ b/scripts/updateTranslations.js @@ -1,15 +1,14 @@ -let fs = require('fs'); -let path = require('path'); -let child_process = require('child_process'); -var https = require('https'); +let fs = require('fs') +let path = require('path') +var https = require('https') -if (process.argv.length != 4) { - console.error('usage: babel-node udpateTranslations.js '); - process.exit(0); +if (process.argv.length !== 4) { + console.error('usage: babel-node udpateTranslations.js ') + process.exit(0) } -const username = process.argv[2]; -const password = process.argv[3]; +const username = process.argv[2] +const password = process.argv[3] const request = (path) => { return new Promise((resolve, reject) => { @@ -17,59 +16,59 @@ const request = (path) => { host: 'api.getlocalization.com', path, method: 'GET', - auth: `${username}:${password}`, + auth: `${username}:${password}` }, function (res) { - res.setEncoding('utf8'); - let body = ''; + res.setEncoding('utf8') + let body = '' res.on('data', function (chunk) { - body += chunk; - }); + body += chunk + }) res.on('end', function () { if (res.statusCode !== 200) { - reject(res.statusCode); + reject(res.statusCode) } else { - resolve(body); + resolve(body) } - }); - }); - }); -}; -const requestTranslations = request.bind(null, '/LinkBubble/api/translations/list/json/'); -const requestTranslationFile = (masterFile, languageTag) => request(`/LinkBubble/api/translations/file/${masterFile}/${languageTag}/`); + }) + }) + }) +} +const requestTranslations = request.bind(null, '/LinkBubble/api/translations/list/json/') +const requestTranslationFile = (masterFile, languageTag) => request(`/LinkBubble/api/translations/file/${masterFile}/${languageTag}/`) // List of locales that are downloaded that we don't want -const ignoreList = ['es-419', 'grk']; +const ignoreList = ['es-419', 'grk'] requestTranslations().then(translations => { - translations = JSON.parse(translations).filter(translation => !ignoreList.includes(translation.iana_code)); + translations = JSON.parse(translations).filter(translation => !ignoreList.includes(translation.iana_code)) Promise.all(translations.reduce((allPromises, translation) => { - allPromises.push(requestTranslationFile(translation.master_file, translation.iana_code)); - return allPromises; + allPromises.push(requestTranslationFile(translation.master_file, translation.iana_code)) + return allPromises }, [translations])).then(values => { - values = values.slice(1); + values = values.slice(1) if (translations.length !== values.length) { - console.error('Not all translations could be downloaded, aborting'); - process.exit(1); + console.error('Not all translations could be downloaded, aborting') + process.exit(1) } for (let i = 0; i < translations.length; i++) { - let translation = translations[i]; - let fileData = values[i]; + let translation = translations[i] + let fileData = values[i] // Android calls: en-US -> en-rUS - let filename = translation.iana_code.split('-').join('-r'); - let toPath = `./Application/LinkBubble/src/main/res/values-${filename}`; - if (!fs.existsSync(toPath)) { - fs.mkdirSync(toPath); + let filename = translation.iana_code.split('-').join('-r') + let toPath = `./Application/LinkBubble/src/main/res/values-${filename}` + if (!fs.existsSync(toPath)) { + fs.mkdirSync(toPath) } - toPath = path.join(toPath, translation.master_file); - fs.writeFileSync(toPath, fileData); + toPath = path.join(toPath, translation.master_file) + fs.writeFileSync(toPath, fileData) } - console.log('Success!'); - }); -}); + console.log('Success!') + }) +}) process.on('uncaughtException', function (err) { - console.log('Caught exception: ' + err); -}); + console.log('Caught exception: ' + err) +})