|
@@ -65,10 +65,14 @@ class ThirdPartyVerifyModel {
|
|
replaceKeysAndInsertMark(content) {
|
|
replaceKeysAndInsertMark(content) {
|
|
// 字符串替换
|
|
// 字符串替换
|
|
content = this.replaceKeys(content)
|
|
content = this.replaceKeys(content)
|
|
- // 将字符串转成dom
|
|
|
|
- this.contentDOM = this.createDomInMemory(content)
|
|
|
|
- // 插入button
|
|
|
|
- this.checkHighlightInsert(this.contentDOM)
|
|
|
|
|
|
+ try {
|
|
|
|
+ // 将字符串转成dom
|
|
|
|
+ this.contentDOM = this.createDomInMemory(content)
|
|
|
|
+ // 插入button
|
|
|
|
+ this.checkHighlightInsert(this.contentDOM)
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error(error)
|
|
|
|
+ }
|
|
// 插入完成后在将dom转成字符串
|
|
// 插入完成后在将dom转成字符串
|
|
return this.contentDOM.innerHTML
|
|
return this.contentDOM.innerHTML
|
|
}
|
|
}
|
|
@@ -92,7 +96,8 @@ class ThirdPartyVerifyModel {
|
|
}
|
|
}
|
|
|
|
|
|
getInsertButton() {
|
|
getInsertButton() {
|
|
- const htmlString = '<span class="third-party-popover third-party-verify-button"><span class="icon icon-third-party-verify-logo"></span><span class="button-text">剑鱼认证服务</span></span>'
|
|
|
|
|
|
+ // third-party-popover标记点击弹窗
|
|
|
|
+ const htmlString = '<span class="third-party-popover third-party-verify-button"><span class="icon icon-third-party-verify-logo third-party-popover"></span><span class="third-party-popover third-party-button-text">剑鱼认证服务</span></span>'
|
|
const div = document.createElement('div')
|
|
const div = document.createElement('div')
|
|
div.innerHTML = htmlString
|
|
div.innerHTML = htmlString
|
|
const dom = div.firstChild
|
|
const dom = div.firstChild
|
|
@@ -106,6 +111,26 @@ class ThirdPartyVerifyModel {
|
|
return div
|
|
return div
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 获取当前元素下一个指定元素,同jquery的.next()
|
|
|
|
+ getNextElement(element, tagName) {
|
|
|
|
+ let nextSibling = element.nextElementSibling;
|
|
|
|
+
|
|
|
|
+ if (!tagName) {
|
|
|
|
+ return nextSibling
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ while (nextSibling) {
|
|
|
|
+ if (nextSibling.tagName === tagName.toUpperCase()) {
|
|
|
|
+ return nextSibling;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ nextSibling = nextSibling.nextElementSibling;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
// 检查并插入button
|
|
// 检查并插入button
|
|
checkHighlightInsert(dom) {
|
|
checkHighlightInsert(dom) {
|
|
var container = dom;
|
|
var container = dom;
|
|
@@ -113,41 +138,38 @@ class ThirdPartyVerifyModel {
|
|
if (verifyIconButton.length) return;
|
|
if (verifyIconButton.length) return;
|
|
var waitingInsertList = [];
|
|
var waitingInsertList = [];
|
|
|
|
|
|
- // 获取要插入元素
|
|
|
|
- var button = this.getInsertButton();
|
|
|
|
-
|
|
|
|
// 1. 先找highlights同级next的.paragraph-last
|
|
// 1. 先找highlights同级next的.paragraph-last
|
|
waitingInsertList = Array.from(container.querySelectorAll('.paragraph-last'));
|
|
waitingInsertList = Array.from(container.querySelectorAll('.paragraph-last'));
|
|
waitingInsertList = this.unique(waitingInsertList);
|
|
waitingInsertList = this.unique(waitingInsertList);
|
|
if (waitingInsertList.length) {
|
|
if (waitingInsertList.length) {
|
|
// 找到了.paragraph-last在其后插入button
|
|
// 找到了.paragraph-last在其后插入button
|
|
waitingInsertList.forEach(function (dom) {
|
|
waitingInsertList.forEach(function (dom) {
|
|
- dom.insertAdjacentElement('afterend', button);
|
|
|
|
- });
|
|
|
|
|
|
+ dom.insertAdjacentElement('afterend', this.getInsertButton());
|
|
|
|
+ }.bind(this));
|
|
} else {
|
|
} else {
|
|
// 2. 找不到.paragraph-last。找同级next的br
|
|
// 2. 找不到.paragraph-last。找同级next的br
|
|
- waitingInsertList = this.findNextMark(dom, function (_this) {
|
|
|
|
- return _this.nextElementSibling && _this.nextElementSibling.tagName === 'BR';
|
|
|
|
- });
|
|
|
|
|
|
+ waitingInsertList = this.findNextMark(dom, function (element) {
|
|
|
|
+ const nextBr = this.getNextElement(element, 'br')
|
|
|
|
+ return nextBr
|
|
|
|
+ }.bind(this));
|
|
if (waitingInsertList.length) {
|
|
if (waitingInsertList.length) {
|
|
// 找到了br,则在其前面插入
|
|
// 找到了br,则在其前面插入
|
|
waitingInsertList.forEach(function (dom) {
|
|
waitingInsertList.forEach(function (dom) {
|
|
- dom.insertAdjacentElement('beforebegin', button);
|
|
|
|
- });
|
|
|
|
|
|
+ dom.insertAdjacentElement('beforebegin', this.getInsertButton());
|
|
|
|
+ }.bind(this));
|
|
} else {
|
|
} else {
|
|
// 3. 找不到br,找到其父级标签,并在其末尾插入
|
|
// 3. 找不到br,找到其父级标签,并在其末尾插入
|
|
- waitingInsertList = this.findNextMark(dom, function (_this) {
|
|
|
|
- return _this.parentNode;
|
|
|
|
|
|
+ waitingInsertList = this.findNextMark(dom, function (element) {
|
|
|
|
+ return element.parentNode;
|
|
});
|
|
});
|
|
if (waitingInsertList.length) {
|
|
if (waitingInsertList.length) {
|
|
// 找到了br,则在其前面插入
|
|
// 找到了br,则在其前面插入
|
|
waitingInsertList.forEach(function (dom) {
|
|
waitingInsertList.forEach(function (dom) {
|
|
- dom.appendChild(button);
|
|
|
|
- });
|
|
|
|
|
|
+ dom.appendChild(this.getInsertButton());
|
|
|
|
+ }.bind(this));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
return dom
|
|
return dom
|
|
}
|
|
}
|
|
|
|
|
|
@@ -155,9 +177,8 @@ class ThirdPartyVerifyModel {
|
|
var waitingInsertList = [] // 待插入节点dom对象
|
|
var waitingInsertList = [] // 待插入节点dom对象
|
|
var container = dom
|
|
var container = dom
|
|
var highlights = container.querySelectorAll('.third-party-verify-highlight')
|
|
var highlights = container.querySelectorAll('.third-party-verify-highlight')
|
|
- highlights.forEach(function () {
|
|
|
|
- var $mark = callback(this)
|
|
|
|
- var mark = $mark[0]
|
|
|
|
|
|
+ highlights.forEach(function (item) {
|
|
|
|
+ var mark = callback(item)
|
|
if (mark) {
|
|
if (mark) {
|
|
waitingInsertList.push(mark)
|
|
waitingInsertList.push(mark)
|
|
}
|
|
}
|