-
-
Please Wait...
+
+
-
-
-
-
-
-
-
+
diff --git a/web-extension/menu.js b/web-extension/menu.js
index 3d18b36..d38dcf2 100644
--- a/web-extension/menu.js
+++ b/web-extension/menu.js
@@ -1,6 +1,137 @@
+var allStyles = [];
+var currentStyle = null;
+var appliedStyles = [];
+
+// create menu labels
+document.getElementById('menuTitle').innerHTML = chrome.i18n.getMessage('extName');
+document.getElementById('includeStyle').innerHTML = chrome.i18n.getMessage('includeStyle');
+document.getElementById('editStyles').innerHTML = chrome.i18n.getMessage('editStyles');
+document.getElementById('savePageLabel').innerHTML = chrome.i18n.getMessage('savePage');
+document.getElementById('saveSelectionLabel').innerHTML = chrome.i18n.getMessage('saveSelection');
+document.getElementById('pageChapterLabel').innerHTML = chrome.i18n.getMessage('pageChapter');
+document.getElementById('selectionChapterLabel').innerHTML = chrome.i18n.getMessage('selectionChapter');
+document.getElementById('editChapters').innerHTML = chrome.i18n.getMessage('editChapters');
+document.getElementById('waitMessage').innerHTML = chrome.i18n.getMessage('waitMessage');
+
+function removeEbook() {
+ chrome.runtime.sendMessage({
+ type: "remove"
+ }, function(response) {});
+}
+
+chrome.runtime.sendMessage({
+ type: "is busy?"
+}, function(response) {
+ if (response.isBusy) {
+ document.getElementById('busy').style.display = 'block';
+ } else {
+ document.getElementById('busy').style.display = 'none';
+ }
+});
+
+chrome.runtime.sendMessage({
+ type: "get styles"
+}, function(response) {
+ createStyleList(response.styles);
+});
+
+function createStyleList(styles) {
+ allStyles = styles;
+ chrome.tabs.query({'active': true}, function (tabs) {
+ let currentUrl = tabs[0].url;
+
+ if (!styles || styles.length === 0) {
+ return;
+ }
+
+ let foundMatchingUrl = false;
+
+ // if multiple URL regexes match, select the longest one
+ let allMatchingStyles = [];
+
+ for (let i = 0; i < styles.length; i++) {
+ let listItem = document.createElement('option');
+ listItem.id = 'option_' + i;
+ listItem.className = 'cssEditor-chapter-item';
+ listItem.value = 'option_' + i;
+ listItem.innerText = styles[i].title;
+
+ currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
+ let styleUrl = styles[i].url;
+ let styleUrlRegex = null;
+
+ try {
+ styleUrlRegex = new RegExp(styleUrl, 'i');
+ } catch (e) {
+ }
+
+ if (styleUrlRegex && styleUrlRegex.test(currentUrl)) {
+ allMatchingStyles.push({
+ index: i,
+ length: styleUrl.length
+ });
+ }
+ }
+
+ if (allMatchingStyles.length >= 1) {
+ allMatchingStyles.sort(function (a, b) {
+ return b.length - a.length;
+ });
+ let selStyle = allMatchingStyles[0];
+ currentStyle = styles[selStyle.index];
+
+ chrome.runtime.sendMessage({
+ type: "set current style",
+ currentStyle: currentStyle
+ }, function(response) {
+ });
+ }
+ });
+}
+
+function createIncludeStyle(data) {
+ let includeStyleCheck = document.getElementById('includeStyleCheck');
+ includeStyleCheck.checked = data;
+}
+
+chrome.runtime.sendMessage({
+ type: "get include style"
+}, function(response) {
+ createIncludeStyle(response.includeStyle);
+});
+
+document.getElementById('includeStyleCheck').onclick = function () {
+ let includeStyleCheck = document.getElementById('includeStyleCheck');
+ chrome.runtime.sendMessage({
+ type: "set include style",
+ includeStyle: includeStyleCheck.checked
+ }, function(response) {
+ });
+}
+
+document.getElementById("editStyles").onclick = function() {
+
+ if (document.getElementById('cssEditor-Modal')) {
+ return;
+ }
+
+ chrome.tabs.query({
+ currentWindow: true,
+ active: true
+ }, function(tab) {
+
+ chrome.tabs.insertCSS(tab[0].id, {file: '/cssEditor.css'});
+
+ chrome.tabs.executeScript(tab[0].id, {
+ file: '/cssEditor.js'
+ });
+
+ window.close();
+ });
+};
document.getElementById("editChapters").onclick = function() {
-
+
if (document.getElementById('chapterEditor-Modal')) {
return;
}
@@ -10,13 +141,8 @@ document.getElementById("editChapters").onclick = function() {
active: true
}, function(tab) {
- chrome.tabs.executeScript(tab[0].id, {file: '/jquery.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/utils.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/filesaver.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/jszip.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/jszip-utils.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/saveEbook.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/jquery-sortable.js'});
+ chrome.tabs.executeScript(tab[0].id, {file: './libs/jquery.js'});
+ chrome.tabs.executeScript(tab[0].id, {file: './libs/jquery-sortable.js'});
chrome.tabs.insertCSS(tab[0].id, {file: '/chapterEditor.css'});
chrome.tabs.executeScript(tab[0].id, {
@@ -27,77 +153,46 @@ document.getElementById("editChapters").onclick = function() {
});
};
-function dispatch(action, justAddToBuffer) {
+function dispatch(commandType, justAddToBuffer) {
document.getElementById('busy').style.display = 'block';
if (!justAddToBuffer) {
removeEbook();
}
- chrome.tabs.query({
- currentWindow: true,
- active: true
- }, function(tab) {
- chrome.tabs.sendMessage(tab[0].id, {
- type: 'echo'
- }, function(response) {
- if (!response) {
- chrome.tabs.executeScript(tab[0].id, {file: '/jquery.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/utils.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/filesaver.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/jszip.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/jszip-utils.js'});
- chrome.tabs.executeScript(tab[0].id, {file: '/pure-parser.js'});
-
- chrome.tabs.executeScript(tab[0].id, {
- file: 'extractHtml.js'
- }, function() {
- sendMessage(tab[0].id, action, justAddToBuffer);
- });
- } else if (response.echo) {
- sendMessage(tab[0].id, action, justAddToBuffer);
- }
- });
- });
-}
-
-function sendMessage(tabId, action, justAddToBuffer) {
- chrome.tabs.sendMessage(tabId, {
- type: action
+ chrome.runtime.sendMessage({
+ type: commandType
}, function(response) {
- if (response.length === 0) {
- if (justAddToBuffer) {
- alert('Cannot add an empty selection as chapter!');
- } else {
- alert('Cannot generate the eBook from an empty selection!');
- }
- window.close();
- }
- if (!justAddToBuffer) {
- buildEbook([response]);
- } else {
- getEbookPages(function (allPages) {
- allPages.push(response);
- saveEbookPages(allPages);
- window.close();
- });
- }
- setTimeout(function () {
- document.getElementById('busy').style.display = 'none';
- }, 500);
+ //FIXME - hidden before done
+ document.getElementById('busy').style.display = 'none';
});
}
document.getElementById('savePage').onclick = function() {
- dispatch('extract-page', false);
+ dispatch('save-page', false);
};
document.getElementById('saveSelection').onclick = function() {
- dispatch('extract-selection', false);
+ dispatch('save-selection', false);
};
document.getElementById('pageChapter').onclick = function() {
- dispatch('extract-page', true);
+ dispatch('add-page', true);
};
document.getElementById('selectionChapter').onclick = function() {
- dispatch('extract-selection', true);
+ dispatch('add-selection', true);
};
+
+// get all shortcuts and display them in the menuTitle
+chrome.commands.getAll((commands) => {
+ for (let command of commands) {
+ if (command.name === 'save-page') {
+ document.getElementById('savePageShortcut').appendChild(document.createTextNode(command.shortcut));
+ } else if (command.name === 'save-selection') {
+ document.getElementById('saveSelectionShortcut').appendChild(document.createTextNode(command.shortcut));
+ } else if (command.name === 'add-page') {
+ document.getElementById('pageChapterShortcut').appendChild(document.createTextNode(command.shortcut));
+ } else if (command.name === 'add-selection') {
+ document.getElementById('selectionChapterShortcut').appendChild(document.createTextNode(command.shortcut));
+ }
+ }
+})
diff --git a/web-extension/saveEbook.js b/web-extension/saveEbook.js
index 46eddc2..5198322 100644
--- a/web-extension/saveEbook.js
+++ b/web-extension/saveEbook.js
@@ -1,6 +1,16 @@
var cssFileName = 'ebook.css';
var ebookTitle = null;
+chrome.runtime.onMessage.addListener((obj, sender, sendResponse) => {
+ if (obj.shortcut && obj.shortcut === 'build-ebook') {
+ buildEbook(obj.response);
+ } else if (obj.alert) {
+ console.log(obj.alert);
+ alert(obj.alert);
+ }
+ return true;
+})
+
function getImagesIndex(allImages) {
return allImages.reduce(function(prev, elem, index) {
return prev + '\n' + '
';
@@ -23,22 +33,28 @@ function buildEbookFromChapters() {
})
}
-function buildEbook(allPages) {
- _buildEbook(allPages);
+// FIXME remove - keep one function
+function buildEbook(allPages, fromMenu=false) {
+ _buildEbook(allPages, fromMenu);
}
// http://ebooks.stackexchange.com/questions/1183/what-is-the-minimum-required-content-for-a-valid-epub
-function _buildEbook(allPages) {
+function _buildEbook(allPages, fromMenu=false) {
allPages = allPages.filter(function(page) {
return page !== null;
});
console.log('Prepare Content...');
+ var ebookFileName = 'eBook.epub';
+
if (ebookTitle) {
- ebookName = ebookTitle + '.epub';
+ // ~TODO a pre-processing function to apply escapeXMLChars to all page.titles
+ ebookName = escapeXMLChars(ebookTitle);
+ ebookFileName = getEbookFileName(removeSpecialChars(ebookTitle)) + '.epub';
} else {
- ebookName = allPages[0].title + '.epub';
+ ebookName = escapeXMLChars(allPages[0].title);
+ ebookFileName = getEbookFileName(removeSpecialChars(allPages[0].title)) + '.epub';
}
var zip = new JSZip();
@@ -68,7 +84,8 @@ function _buildEbook(allPages) {
'
Table of Contents
' +
'
' +
allPages.reduce(function(prev, page) {
- return prev + '\n' + '- ' + page.title + '
';
+ var tmpPageTitle = escapeXMLChars(page.title);
+ return prev + '\n' + '- ' + tmpPageTitle + '
';
}, '') +
'
' +
'' +
@@ -88,9 +105,10 @@ function _buildEbook(allPages) {
'' +
'
' +
allPages.reduce(function(prev, page, index) {
+ var tmpPageTitle = escapeXMLChars(page.title);
return prev + '\n' +
'' +
- '' + page.title + '' +
+ '' + tmpPageTitle + '' +
'' +
'';
}, '') +
@@ -98,16 +116,21 @@ function _buildEbook(allPages) {
''
);
- oebps.file(cssFileName, '');
+ oebps.file(cssFileName, ''); //TODO
+ var styleFolder = oebps.folder('style');
+ allPages.forEach(function(page) {
+ styleFolder.file(page.styleFileName, page.styleFileContent);
+ });
var pagesFolder = oebps.folder('pages');
allPages.forEach(function(page) {
+ var tmpPageTitle = escapeXMLChars(page.title);
pagesFolder.file(page.url,
'' +
'' +
'' +
- '' + page.title + '' +
- '' +
+ '' + tmpPageTitle+ '' +
+ '' +
'' +
page.content +
''
@@ -120,7 +143,7 @@ function _buildEbook(allPages) {
'' +
''+ ebookName + '' +
'isbn' +
- '' + new Date().toISOString() + '' +
+ '' + new Date().toISOString().replace(/\.[0-9]+Z/i, 'Z') + '' +
'en' +
'' +
'' +
@@ -130,10 +153,12 @@ function _buildEbook(allPages) {
allPages.reduce(function(prev, page, index) {
return prev + '\n' + ' ';
}, '') +
+ allPages.reduce(function(prev, page, index) {
+ return prev + '\n' + ' ';
+ }, '') +
allPages.reduce(function(prev, page, index) {
return prev + '\n' + getImagesIndex(page.images);
}, '') +
- // getExternalLinksIndex() +
'' +
'' +
allPages.reduce(function(prev, page, index) {
@@ -144,37 +169,35 @@ function _buildEbook(allPages) {
);
///////////////
- var imgsFolder = oebps.folder("images");
- allPages.forEach(function(page) {
- for (var i = 0; i < page.images.length; i++) {
- var tmpImg = page.images[i];
- imgsFolder.file(tmpImg.filename, tmpImg.data, {
- base64: true
- });
- }
- });
-
- var done = false;
+ try {
+ let imgsFolder = oebps.folder("images");
+ allPages.forEach(function(page) {
+ for (let i = 0; i < page.images.length; i++) {
+ let tmpImg = page.images[i]
+ // TODO - Must be JSON serializable - see the same comment in extractHtml.js
+ // if (tmpImg.isBinary) {
+ // imgsFolder.file(tmpImg.filename, tmpImg.data, {binary: true})
+ // } else {
+ imgsFolder.file(tmpImg.filename, tmpImg.data, {base64: true})
+ // }
+ }
+ });
+ } catch (error) {
+ console.log(error);
+ }
+
zip.generateAsync({
- type: "blob"
+ type: "blob",
+ mimeType: "application/epub+zip"
})
.then(function(content) {
- done = true;
console.log("done !");
- saveAs(content, ebookName);
+ saveAs(content, ebookFileName);
+
+ chrome.runtime.sendMessage({
+ type: "done"
+ }, (response) => {});
});
- setTimeout(function() {
- if (done) {
- return;
- }
- zip.generateAsync({
- type: "blob"
- })
- .then(function(content) {
- saveAs(content, ebookName);
- });
- }, 60000);
-
}
diff --git a/web-extension/selector.js b/web-extension/selector.js
new file mode 100644
index 0000000..39b84e9
--- /dev/null
+++ b/web-extension/selector.js
@@ -0,0 +1,140 @@
+var lastTargetElem = null
+var lastElementData = {}
+var lastParentElem = null
+var lastParentData = {}
+
+let cleanXPaths = []
+
+
+let bodyElem = document.getElementsByTagName('body')[0]
+let bodyInner = bodyElem.innerHTML
+bodyElem.innerHTML = ''
+
+// let container = document.createElement('div')
+// container.style.display = "flex"
+// container.style.flexFlow = "row"
+// let menuContainer = document.createElement('div')
+// menuContainer.style.width = '300px'
+// menuContainer.style.minWidth = '300px'
+// menuContainer.style.height = '600px'
+// menuContainer.style.minHeight = '600px'
+// let page = document.createElement('div')
+// page.id= 'super-selector'
+// page.style.width = '90%'
+// page.style.height = '90%'
+// page.innerHTML = bodyInner
+
+// let menu = document.createElement('div')
+// menu.id = "slector-main-menu"
+// menu.style.width = '300px'
+// menu.style.minWidth = '300px'
+// menu.style.height = '600px'
+// menu.style.minHeight = '600px'
+// menu.style.backgroundColor = 'black'
+// menu.style.position = 'fixed'
+// menu.style.top = '0'
+
+
+// container.appendChild(menuContainer)
+// container.appendChild(page)
+// bodyElem.appendChild(container)
+// bodyElem.appendChild(menu)
+
+bodyElem.innerHTML = ``
+
+
+document.getElementById('super-selector').addEventListener('mousemove', (e) => {
+// document.getElementsByTagName('body')[0].addEventListener('mousemove', (e) => {
+ e.preventDefault()
+
+ if (lastTargetElem) {
+ lastTargetElem.style.backgroundColor = lastElementData.backgroundColor
+ }
+
+ let targetElem = document.elementFromPoint(e.clientX, e.clientY);
+
+ let targetParent = targetElem.parentNode
+ lastElementData.backgroundColor = targetElem.style.backgroundColor
+
+ targetElem.style.backgroundColor = 'rgba(255,0,0,0.25)'
+ targetElem.dataset.excluded = 'true'
+
+ targetElem.onclick = (e) => {
+ e.preventDefault() // FIXME except for my menu
+ e.stopPropagation();
+
+ let originalClickedElem = e.target
+ let clickedElem = e.target
+
+ if (!clickedElem) {
+ return
+ }
+
+ // console.log(createXPathFromElement(clickedElem));
+
+ ///
+ let tmpElem = document.createElement('div')
+ tmpElem.innerText = createXPathFromElement(clickedElem);
+ document.getElementById('slector-main-menu').appendChild(tmpElem)
+
+ let clickedElemParent = clickedElem.parentNode;
+ let foundClickedEleme = false
+ while (clickedElemParent) {
+ if (!clickedElemParent || !clickedElemParent.style) {
+ break
+ }
+ if (clickedElemParent.id === 'slector-main-menu') {
+ // ignore main menu
+ return
+ }
+ if (clickedElemParent.style.opacity === '0.1') {
+ foundClickedEleme = true
+ clickedElem = clickedElemParent
+ break
+ }
+ clickedElemParent = clickedElemParent.parentNode;
+ }
+
+ if (!clickedElem.style.opacity || clickedElem.style.opacity > 0.2) {
+ clickedElem.style.opacity = 0.1
+ clickedElem.style.border = 'solid 3px black'
+ }
+ else {
+ clickedElem.style.opacity = 1
+ clickedElem.style.border = 'none'
+ }
+ }
+
+ lastTargetElem = targetElem
+})
+
+// src https://stackoverflow.com/questions/2661818/javascript-get-xpath-of-a-node
+function createXPathFromElement(elm) {
+ var allNodes = document.getElementsByTagName('*');
+ for (var segs = []; elm && elm.nodeType == 1; elm = elm.parentNode)
+ {
+ if (elm.hasAttribute('id')) {
+ var uniqueIdCount = 0;
+ for (var n=0;n < allNodes.length;n++) {
+ if (allNodes[n].hasAttribute('id') && allNodes[n].id == elm.id) uniqueIdCount++;
+ if (uniqueIdCount > 1) break;
+ };
+ if ( uniqueIdCount == 1) {
+ segs.unshift('id("' + elm.getAttribute('id') + '")');
+ return segs.join('/');
+ } else {
+ segs.unshift(elm.localName.toLowerCase() + '[@id="' + elm.getAttribute('id') + '"]');
+ }
+ } else if (elm.hasAttribute('class')) {
+ segs.unshift(elm.localName.toLowerCase() + '[@class="' + elm.getAttribute('class') + '"]');
+ } else {
+ for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) {
+ if (sib.localName == elm.localName) i++; };
+ segs.unshift(elm.localName.toLowerCase() + '[' + i + ']');
+ };
+ };
+ return segs.length ? '/' + segs.join('/') : null;
+};
diff --git a/web-extension/utils.js b/web-extension/utils.js
index 09cc179..1c0fb69 100644
--- a/web-extension/utils.js
+++ b/web-extension/utils.js
@@ -1,3 +1,51 @@
+function setIncludeStyle(includeStyle) {
+ chrome.runtime.sendMessage({
+ type: "set include style",
+ includeStyle: includeStyle
+ }, function(response) {
+ });
+}
+
+function getIncludeStyle(callback) {
+ chrome.runtime.sendMessage({
+ type: "get include style"
+ }, function(response) {
+ callback(response.includeStyle);
+ });
+}
+
+function setCurrentStyle(currentStyle) {
+ chrome.runtime.sendMessage({
+ type: "set current style",
+ currentStyle: currentStyle
+ }, function(response) {
+ });
+}
+
+function getCurrentStyle(callback) {
+ chrome.runtime.sendMessage({
+ type: "get current style"
+ }, function(response) {
+ callback(response.currentStyle);
+ });
+}
+
+function getStyles(callback) {
+ chrome.runtime.sendMessage({
+ type: "get styles"
+ }, function(response) {
+ callback(response.styles);
+ });
+}
+
+function setStyles(styles) {
+ chrome.runtime.sendMessage({
+ type: "set styles",
+ styles: styles
+ }, function(response) {
+ });
+}
+
function getEbookTitle(callback) {
chrome.runtime.sendMessage({
type: "get title"
@@ -35,9 +83,24 @@ function removeEbook() {
}, function(response) {});
}
+function checkIfBusy(callback) {
+ chrome.runtime.sendMessage({
+ type: "is busy?"
+ }, function(response) {
+ callback(response);
+ });
+}
+
+function setIsBusy(isBusy) {
+ chrome.runtime.sendMessage({
+ type: "set is busy",
+ isBusy: isBusy
+ }, function(response) {});
+}
+
/////
function getCurrentUrl() {
- var url = window.location.href;
+ let url = window.location.href;
if (url.indexOf('?') > 0) {
url = window.location.href.split('?')[0];
}
@@ -46,7 +109,7 @@ function getCurrentUrl() {
}
function getOriginUrl() {
- var originUrl = window.location.origin;
+ let originUrl = window.location.origin;
if (!originUrl) {
originUrl = window.location.protocol + "//" + window.location.host;
}
@@ -55,7 +118,7 @@ function getOriginUrl() {
function getFileExtension(fileName) {
try {
- var tmpFileName = '';
+ let tmpFileName = '';
if (isBase64Img(fileName)) {
tmpFileName = getBase64ImgType(fileName);
@@ -71,8 +134,10 @@ function getFileExtension(fileName) {
tmpFileName = 'jpeg';
} else if (tmpFileName === 'svg+xml') {
tmpFileName = 'svg';
- } else if (tmpFileName.trim() === '') {
- tmpFileName = '';
+ }
+
+ if (['png', 'gif', 'jpeg', 'svg'].indexOf(tmpFileName.trim()) < 0) {
+ return ''
}
return tmpFileName;
} catch (e) {
@@ -82,7 +147,7 @@ function getFileExtension(fileName) {
}
function getImageType(fileName) {
- var imageType = getFileExtension(fileName);
+ let imageType = getFileExtension(fileName);
if (imageType === 'svg') {
imageType = 'svg+xml';
}
@@ -97,18 +162,21 @@ function getImgDownloadUrl(imgSrc) {
return getAbsoluteUrl(imgSrc);
}
-function getAbsoluteUrl(urlStr) {
+function getAbsoluteUrl(urlStr) {
if (!urlStr) {
return '';
}
+ if (urlStr.length === 0) {
+ return '';
+ }
try {
- if (urlStr.length === 0) {
- return '';
- }
- urlStr = decodeHtmlEntity(urlStr);
- var currentUrl = getCurrentUrl();
- var originUrl = getOriginUrl();
- var absoluteUrl = urlStr;
+ urlStr = decodeHtmlEntity(urlStr);
+ let currentUrl = getCurrentUrl();
+ let originUrl = getOriginUrl();
+ let absoluteUrl = urlStr;
+
+ originUrl = removeEndingSlash(originUrl)
+ currentUrl = removeEndingSlash(currentUrl)
if (urlStr.indexOf('//') === 0) {
absoluteUrl = window.location.protocol + urlStr;
@@ -119,6 +187,8 @@ function getAbsoluteUrl(urlStr) {
} else if (urlStr.indexOf('http') !== 0) {
absoluteUrl = currentUrl + '/' + urlStr;
}
+ // TODO is this needed?
+ // absoluteUrl = escapeXMLChars(absoluteUrl);
return absoluteUrl;
} catch (e) {
console.log('Error:', e);
@@ -126,20 +196,27 @@ function getAbsoluteUrl(urlStr) {
}
}
+function removeEndingSlash(inputStr) {
+ if (inputStr.endsWith('/')) {
+ return inputStr.substring(0, inputStr.length - 1);
+ }
+ return inputStr;
+}
+
// https://gist.github.com/jonleighton/958841
function base64ArrayBuffer(arrayBuffer) {
- var base64 = '';
- var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+ let base64 = '';
+ let encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
- var bytes = new Uint8Array(arrayBuffer);
- var byteLength = bytes.byteLength;
- var byteRemainder = byteLength % 3;
- var mainLength = byteLength - byteRemainder;
+ let bytes = new Uint8Array(arrayBuffer);
+ let byteLength = bytes.byteLength;
+ let byteRemainder = byteLength % 3;
+ let mainLength = byteLength - byteRemainder;
- var a, b, c, d;
- var chunk;
+ let a, b, c, d;
+ let chunk;
- for (var i = 0; i < mainLength; i = i + 3) {
+ for (let i = 0; i < mainLength; i = i + 3) {
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
a = (chunk & 16515072) >> 18;
b = (chunk & 258048) >> 12;
@@ -194,11 +271,13 @@ function getBase64ImgData(srcTxt) {
}
function getXPath(elm) {
- var allNodes = document.getElementsByTagName('*');
- for (var segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) {
+ if (!elm) return ''
+
+ let allNodes = document.getElementsByTagName('*');
+ for (let segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) {
if (elm.hasAttribute('id')) {
- var uniqueIdCount = 0;
- for (var n = 0; n < allNodes.length; n++) {
+ let uniqueIdCount = 0;
+ for (let n = 0; n < allNodes.length; n++) {
if (allNodes[n].hasAttribute('id') && allNodes[n].id === elm.id) {
uniqueIdCount++;
}
@@ -227,17 +306,71 @@ function getXPath(elm) {
}
function lookupElementByXPath(path) {
- var evaluator = new XPathEvaluator();
- var result = evaluator.evaluate(path, document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
+ let evaluator = new XPathEvaluator();
+ let result = evaluator.evaluate(path, document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue;
}
function generateRandomTag(tagLen) {
tagLen = tagLen || 5;
- var text = '';
- var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
- for(var i = 0; i < tagLen; i++) {
+ let text = '';
+ let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
+ for(let i = 0; i < tagLen; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
+
+function generateRandomNumber(bigNr) {
+ if (bigNr) {
+ return Math.floor(Math.random()*1000000*Math.random()*1000000)
+ }
+ return Math.floor(Math.random()*1000000)
+}
+
+function removeSpecialChars(text) {
+ // FIXME remove white spaces ?
+ return text.replace(/\//g, '-')
+}
+
+function escapeXMLChars(text) {
+ return text.replace(/&/g, '&')
+ .replace(/>/g, '>')
+ .replace(/