Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Commit

Permalink
Format javascript according to StandardJS spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGrandon committed Oct 26, 2015
1 parent 73820ba commit fba05c2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(function() {
window.LinkBubble.fetchHtml(document.documentElement.outerHTML, String(window.location));
})();
;(function () {
window.LinkBubble.fetchHtml(document.documentElement.outerHTML, String(window.location))
})()
130 changes: 65 additions & 65 deletions Application/LinkBubble/src/main/assets/pagescripts/SelectElements.js
Original file line number Diff line number Diff line change
@@ -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)
})()
12 changes: 6 additions & 6 deletions Application/LinkBubble/src/main/assets/pagescripts/ThemeColor.js
Original file line number Diff line number Diff line change
@@ -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'))
}
})()
40 changes: 20 additions & 20 deletions Application/LinkBubble/src/main/assets/pagescripts/TouchIcon.js
Original file line number Diff line number Diff line change
@@ -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())
}
})()
41 changes: 21 additions & 20 deletions Application/LinkBubble/src/main/assets/pagescripts/YouTube.js
Original file line number Diff line number Diff line change
@@ -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()
})()
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
83 changes: 41 additions & 42 deletions scripts/updateTranslations.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,74 @@
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 <username> <password>');
process.exit(0);
if (process.argv.length !== 4) {
console.error('usage: babel-node udpateTranslations.js <username> <password>')
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) => {
return https.get({
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)
})

0 comments on commit fba05c2

Please sign in to comment.