misc changes; removed force()

This commit is contained in:
Alex Adam 2020-02-28 14:15:48 +02:00
parent b49b78ad88
commit 9a75e6e92a
3 changed files with 57 additions and 143 deletions

View file

@ -33,20 +33,14 @@ var supportedCss = [
'border', 'border',
'color', 'color',
'font', 'font',
// 'letter-spacing',
'line-height', 'line-height',
'list-style', 'list-style',
'padding', 'padding',
// 'text-decoration',
// 'text-transform',
'text-align', 'text-align',
// 'word-spacing',
]; ];
////// //////
function getImageSrc(srcTxt) { function getImageSrc(srcTxt) {
if (!srcTxt) { if (!srcTxt) {
return ''; return '';
} }
@ -73,8 +67,6 @@ function getImageSrc(srcTxt) {
data: getBase64ImgData(srcTxt) data: getBase64ImgData(srcTxt)
}); });
} else { } else {
console.log("img url", fileExtension, getImgDownloadUrl(srcTxt));
allImages.push({ allImages.push({
originalUrl: getImgDownloadUrl(srcTxt), originalUrl: getImgDownloadUrl(srcTxt),
filename: newImgFileName, // TODO name filename: newImgFileName, // TODO name
@ -131,84 +123,6 @@ function preProcess($htmlObject) {
extractSvgToImg($htmlObject); extractSvgToImg($htmlObject);
} }
// TODO remove / change
// function force($content, withError) {
// try {
// var tagOpen = '@@@' + generateRandomTag();
// var tagClose = '###' + generateRandomTag();
// // var tagOpen = '<';
// // var tagClose = '>';
// var startEl = '<object>';
// var endEl = '</object>';
// // if (withError) {
// // $content = $($content);
// // preProcess($content);
// // }
// $content.find('img').each(function (index, elem) {
// var $elem = $(elem);
// var imgSrc = getImageSrc($elem.attr('src'));
// if (imgSrc === '') {
// $elem.replaceWith('');
// } else {
// var className = $elem.attr('data-class');
// $elem.replaceWith(startEl + tagOpen + 'img src="' + imgSrc + '" class="' + className + '"' + tagClose + tagOpen + '/img' + tagClose + endEl);
// }
// });
// $content.find('a').each(function (index, elem) {
// var $elem = $(elem);
// var aHref = getHref($elem.attr('href'));
// if (aHref === '') {
// $elem.replaceWith('');
// } else {
// var className = $elem.attr('data-class');
// $elem.replaceWith(startEl + tagOpen + 'a href="' + aHref + '" class="' + className + '"' + tagClose + $(elem).html() + tagOpen + '/a' + tagClose + endEl);
// }
// });
// // var regex = /(<([^>]+)>)/ig
// // var contentString = $content.html().replace(regex, '')
// function all($startElement) {
// var tagName = $startElement.get(0).tagName.toLowerCase();
// if (allowedTags.indexOf(tagName) >= 0) {
// var children = $startElement.children();
// var childrenLen = children.length;
// while (childrenLen--) {
// all($(children[childrenLen]));
// }
// var className = $startElement.attr('data-class');
// // $startElement.replaceWith(startEl + tagOpen + tagName + ' class="' + className + '"' + tagClose + $startElement.html() + tagOpen + '/' + tagName + tagClose + endEl);
// $startElement.replaceWith(tagOpen + tagName + ' class="' + className + '"' + tagClose +
// $startElement.html() + tagOpen + '/' + tagName + tagClose);
// }
// }
// all($content);
// var contentString = $content.text();
// var tagOpenRegex = new RegExp(tagOpen, 'gi');
// var tagCloseRegex = new RegExp(tagClose, 'gi');
// contentString = contentString.replace(tagOpenRegex, '<');
// contentString = contentString.replace(tagCloseRegex, '>');
// contentString = contentString.replace(/&/gi, '&amp;');
// contentString = contentString.replace(/&amp;nbsp;/gi, '&#160;');
// // getHref() replace does not work (&amp; is overwritten)
// // contentString = escapeXMLChars(contentString);
// return contentString;
// } catch (e) {
// console.log('Error:', e);
// return e;
// }
// }
function parseHTML(rawContentString) { function parseHTML(rawContentString) {
allImages = []; allImages = [];
extractedImages = []; extractedImages = [];
@ -225,9 +139,9 @@ function parseHTML(rawContentString) {
} }
if (tag === 'img') { if (tag === 'img') {
var tmpAttrsTxt = ''; let tmpAttrsTxt = '';
let tmpSrc = '' let tmpSrc = ''
for (var i = 0; i < attrs.length; i++) { for (let i = 0; i < attrs.length; i++) {
if (attrs[i].name === 'src') { if (attrs[i].name === 'src') {
tmpSrc = getImageSrc(attrs[i].value) tmpSrc = getImageSrc(attrs[i].value)
tmpAttrsTxt += ' src="' + tmpSrc + '"'; tmpAttrsTxt += ' src="' + tmpSrc + '"';
@ -248,8 +162,8 @@ function parseHTML(rawContentString) {
lastFragment = tmpAttrsTxt.length === 0 ? '<img></img>' : '<img' + tmpAttrsTxt + ' alt=""></img>'; lastFragment = tmpAttrsTxt.length === 0 ? '<img></img>' : '<img' + tmpAttrsTxt + ' alt=""></img>';
} }
} else if (tag === 'a') { } else if (tag === 'a') {
var tmpAttrsTxt = ''; let tmpAttrsTxt = '';
for (var i = 0; i < attrs.length; i++) { for (let i = 0; i < attrs.length; i++) {
if (attrs[i].name === 'href') { if (attrs[i].name === 'href') {
tmpAttrsTxt += ' href="' + getHref(attrs[i].value) + '"'; tmpAttrsTxt += ' href="' + getHref(attrs[i].value) + '"';
} else if (attrs[i].name === 'data-class') { } else if (attrs[i].name === 'data-class') {
@ -258,25 +172,25 @@ function parseHTML(rawContentString) {
} }
lastFragment = tmpAttrsTxt.length === 0 ? '<a>' : '<a' + tmpAttrsTxt + '>'; lastFragment = tmpAttrsTxt.length === 0 ? '<a>' : '<a' + tmpAttrsTxt + '>';
} else if (tag === 'br' || tag === 'hr') { } else if (tag === 'br' || tag === 'hr') {
var tmpAttrsTxt = ''; let tmpAttrsTxt = '';
for (var i = 0; i < attrs.length; i++) { for (let i = 0; i < attrs.length; i++) {
if (attrs[i].name === 'data-class') { if (attrs[i].name === 'data-class') {
tmpAttrsTxt += ' class="' + attrs[i].value + '"'; tmpAttrsTxt += ' class="' + attrs[i].value + '"';
} }
} }
lastFragment = '<' + tag + tmpAttrsTxt + '></' + tag + '>'; lastFragment = '<' + tag + tmpAttrsTxt + '></' + tag + '>';
} else if (tag === 'math') { } else if (tag === 'math') {
var tmpAttrsTxt = ''; let tmpAttrsTxt = '';
tmpAttrsTxt += ' xmlns="http://www.w3.org/1998/Math/MathML"'; tmpAttrsTxt += ' xmlns="http://www.w3.org/1998/Math/MathML"';
for (var i = 0; i < attrs.length; i++) { for (let i = 0; i < attrs.length; i++) {
if (attrs[i].name === 'alttext') { if (attrs[i].name === 'alttext') {
tmpAttrsTxt += ' alttext="' + attrs[i].value + '"'; tmpAttrsTxt += ' alttext="' + attrs[i].value + '"';
} }
} }
lastFragment = '<' + tag + tmpAttrsTxt + '>'; lastFragment = '<' + tag + tmpAttrsTxt + '>';
} else { } else {
var tmpAttrsTxt = ''; let tmpAttrsTxt = '';
for (var i = 0; i < attrs.length; i++) { for (let i = 0; i < attrs.length; i++) {
if (attrs[i].name === 'data-class') { if (attrs[i].name === 'data-class') {
tmpAttrsTxt += ' class="' + attrs[i].value + '"'; tmpAttrsTxt += ' class="' + attrs[i].value + '"';
} }
@ -321,9 +235,9 @@ function getContent(htmlContent) {
try { try {
// TODO - move; called multiple times on selection // TODO - move; called multiple times on selection
preProcess($('body')) preProcess($('body'))
var tmp = document.createElement('div'); let tmp = document.createElement('div');
tmp.appendChild(htmlContent.cloneNode(true)); tmp.appendChild(htmlContent.cloneNode(true));
var tmpHtml = '<div>' + tmp.innerHTML + '</div>'; let tmpHtml = '<div>' + tmp.innerHTML + '</div>';
return parseHTML(tmpHtml); return parseHTML(tmpHtml);
} catch (e) { } catch (e) {
console.log('Error:', e); console.log('Error:', e);
@ -338,9 +252,9 @@ function getSelectedNodes() {
// return document.selection.createRange().parentElement(); // return document.selection.createRange().parentElement();
// return document.selection.createRange(); // return document.selection.createRange();
// } // }
var selection = window.getSelection(); let selection = window.getSelection();
var docfrag = []; let docfrag = [];
for (var i = 0; i < selection.rangeCount; i++) { for (let i = 0; i < selection.rangeCount; i++) {
docfrag.push(selection.getRangeAt(i).cloneContents()); docfrag.push(selection.getRangeAt(i).cloneContents());
} }
return docfrag; return docfrag;
@ -387,8 +301,8 @@ function extractCss(includeStyle, appliedStyles) {
// Reuse CSS - if the same css code was generated for another element, reuse it's class name // Reuse CSS - if the same css code was generated for another element, reuse it's class name
var tcss = JSON.stringify(tmpNewCss) let tcss = JSON.stringify(tmpNewCss)
var found = false let found = false
if (Object.keys(tmpIdsToNewCssSTRING).length === 0) { if (Object.keys(tmpIdsToNewCssSTRING).length === 0) {
tmpIdsToNewCssSTRING[tmpName] = tcss; tmpIdsToNewCssSTRING[tmpName] = tcss;
@ -426,7 +340,7 @@ function extractCss(includeStyle, appliedStyles) {
///// /////
function deferredAddZip(url, filename) { function deferredAddZip(url, filename) {
var deferred = $.Deferred(); let deferred = $.Deferred();
JSZipUtils.getBinaryContent(url, function(err, data) { JSZipUtils.getBinaryContent(url, function(err, data) {
if (err) { if (err) {
// deferred.reject(err); TODO // deferred.reject(err); TODO

View file

@ -38,27 +38,27 @@ chrome.runtime.sendMessage({
function createStyleList(styles) { function createStyleList(styles) {
allStyles = styles; allStyles = styles;
chrome.tabs.query({'active': true}, function (tabs) { chrome.tabs.query({'active': true}, function (tabs) {
var currentUrl = tabs[0].url; let currentUrl = tabs[0].url;
if (!styles || styles.length === 0) { if (!styles || styles.length === 0) {
return; return;
} }
var foundMatchingUrl = false; let foundMatchingUrl = false;
// if multiple URL regexes match, select the longest one // if multiple URL regexes match, select the longest one
var allMatchingStyles = []; let allMatchingStyles = [];
for (var i = 0; i < styles.length; i++) { for (let i = 0; i < styles.length; i++) {
var listItem = document.createElement('option'); let listItem = document.createElement('option');
listItem.id = 'option_' + i; listItem.id = 'option_' + i;
listItem.className = 'cssEditor-chapter-item'; listItem.className = 'cssEditor-chapter-item';
listItem.value = 'option_' + i; listItem.value = 'option_' + i;
listItem.innerText = styles[i].title; listItem.innerText = styles[i].title;
currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase(); currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
var styleUrl = styles[i].url; let styleUrl = styles[i].url;
var styleUrlRegex = null; let styleUrlRegex = null;
try { try {
styleUrlRegex = new RegExp(styleUrl, 'i'); styleUrlRegex = new RegExp(styleUrl, 'i');
@ -77,7 +77,7 @@ function createStyleList(styles) {
allMatchingStyles.sort(function (a, b) { allMatchingStyles.sort(function (a, b) {
return b.length - a.length; return b.length - a.length;
}); });
var selStyle = allMatchingStyles[0]; let selStyle = allMatchingStyles[0];
currentStyle = styles[selStyle.index]; currentStyle = styles[selStyle.index];
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
@ -90,7 +90,7 @@ function createStyleList(styles) {
} }
function createIncludeStyle(data) { function createIncludeStyle(data) {
var includeStyleCheck = document.getElementById('includeStyleCheck'); let includeStyleCheck = document.getElementById('includeStyleCheck');
includeStyleCheck.checked = data; includeStyleCheck.checked = data;
} }
@ -101,7 +101,7 @@ chrome.runtime.sendMessage({
}); });
document.getElementById('includeStyleCheck').onclick = function () { document.getElementById('includeStyleCheck').onclick = function () {
var includeStyleCheck = document.getElementById('includeStyleCheck'); let includeStyleCheck = document.getElementById('includeStyleCheck');
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
type: "set include style", type: "set include style",
includeStyle: includeStyleCheck.checked includeStyle: includeStyleCheck.checked

View file

@ -100,7 +100,7 @@ function setIsBusy(isBusy) {
///// /////
function getCurrentUrl() { function getCurrentUrl() {
var url = window.location.href; let url = window.location.href;
if (url.indexOf('?') > 0) { if (url.indexOf('?') > 0) {
url = window.location.href.split('?')[0]; url = window.location.href.split('?')[0];
} }
@ -109,7 +109,7 @@ function getCurrentUrl() {
} }
function getOriginUrl() { function getOriginUrl() {
var originUrl = window.location.origin; let originUrl = window.location.origin;
if (!originUrl) { if (!originUrl) {
originUrl = window.location.protocol + "//" + window.location.host; originUrl = window.location.protocol + "//" + window.location.host;
} }
@ -118,7 +118,7 @@ function getOriginUrl() {
function getFileExtension(fileName) { function getFileExtension(fileName) {
try { try {
var tmpFileName = ''; let tmpFileName = '';
if (isBase64Img(fileName)) { if (isBase64Img(fileName)) {
tmpFileName = getBase64ImgType(fileName); tmpFileName = getBase64ImgType(fileName);
@ -147,7 +147,7 @@ function getFileExtension(fileName) {
} }
function getImageType(fileName) { function getImageType(fileName) {
var imageType = getFileExtension(fileName); let imageType = getFileExtension(fileName);
if (imageType === 'svg') { if (imageType === 'svg') {
imageType = 'svg+xml'; imageType = 'svg+xml';
} }
@ -205,18 +205,18 @@ function removeEndingSlash(inputStr) {
// https://gist.github.com/jonleighton/958841 // https://gist.github.com/jonleighton/958841
function base64ArrayBuffer(arrayBuffer) { function base64ArrayBuffer(arrayBuffer) {
var base64 = ''; let base64 = '';
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; let encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var bytes = new Uint8Array(arrayBuffer); let bytes = new Uint8Array(arrayBuffer);
var byteLength = bytes.byteLength; let byteLength = bytes.byteLength;
var byteRemainder = byteLength % 3; let byteRemainder = byteLength % 3;
var mainLength = byteLength - byteRemainder; let mainLength = byteLength - byteRemainder;
var a, b, c, d; let a, b, c, d;
var chunk; 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]; chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
a = (chunk & 16515072) >> 18; a = (chunk & 16515072) >> 18;
b = (chunk & 258048) >> 12; b = (chunk & 258048) >> 12;
@ -273,11 +273,11 @@ function getBase64ImgData(srcTxt) {
function getXPath(elm) { function getXPath(elm) {
if (!elm) return '' if (!elm) return ''
var allNodes = document.getElementsByTagName('*'); let allNodes = document.getElementsByTagName('*');
for (var segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) { for (let segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) {
if (elm.hasAttribute('id')) { if (elm.hasAttribute('id')) {
var uniqueIdCount = 0; let uniqueIdCount = 0;
for (var n = 0; n < allNodes.length; n++) { for (let n = 0; n < allNodes.length; n++) {
if (allNodes[n].hasAttribute('id') && allNodes[n].id === elm.id) { if (allNodes[n].hasAttribute('id') && allNodes[n].id === elm.id) {
uniqueIdCount++; uniqueIdCount++;
} }
@ -306,16 +306,16 @@ function getXPath(elm) {
} }
function lookupElementByXPath(path) { function lookupElementByXPath(path) {
var evaluator = new XPathEvaluator(); let evaluator = new XPathEvaluator();
var result = evaluator.evaluate(path, document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); let result = evaluator.evaluate(path, document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue; return result.singleNodeValue;
} }
function generateRandomTag(tagLen) { function generateRandomTag(tagLen) {
tagLen = tagLen || 5; tagLen = tagLen || 5;
var text = ''; let text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
for(var i = 0; i < tagLen; i++) { for(let i = 0; i < tagLen; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length)); text += possible.charAt(Math.floor(Math.random() * possible.length));
} }
return text; return text;
@ -361,13 +361,13 @@ function getPageTitle(title) {
} }
function jsonToCss(jsonObj) { function jsonToCss(jsonObj) {
var keys = Object.keys(jsonObj); let keys = Object.keys(jsonObj);
var result = ''; let result = '';
for (var i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
var tmpJsonObj = jsonObj[keys[i]]; let tmpJsonObj = jsonObj[keys[i]];
var tmpKeys = Object.keys(tmpJsonObj); let tmpKeys = Object.keys(tmpJsonObj);
result += '.' + keys[i] + ' {'; result += '.' + keys[i] + ' {';
for (var j = 0; j < tmpKeys.length; j++) { for (let j = 0; j < tmpKeys.length; j++) {
result += tmpKeys[j] + ':' + tmpJsonObj[tmpKeys[j]] + ';'; result += tmpKeys[j] + ':' + tmpJsonObj[tmpKeys[j]] + ';';
} }
result += '} '; result += '} ';