|  | @@ -0,0 +1,154 @@
 | 
	
		
			
				|  |  | +import { debounce } from "@/uilts/help";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const keywordData = [
 | 
	
		
			
				|  |  | +  {
 | 
	
		
			
				|  |  | +    type: 1,
 | 
	
		
			
				|  |  | +    keyword: 'giveaway',
 | 
	
		
			
				|  |  | +    tips: ''
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  {
 | 
	
		
			
				|  |  | +    type: 1,
 | 
	
		
			
				|  |  | +    keyword: 'follow',
 | 
	
		
			
				|  |  | +    tips: ''
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  {
 | 
	
		
			
				|  |  | +    type: 2,
 | 
	
		
			
				|  |  | +    keyword: 'ToolBox',
 | 
	
		
			
				|  |  | +    tips: ''
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  {
 | 
	
		
			
				|  |  | +    type: 1,
 | 
	
		
			
				|  |  | +    keyword: 'retweet',
 | 
	
		
			
				|  |  | +    tips: ''
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  {
 | 
	
		
			
				|  |  | +    type: 2,
 | 
	
		
			
				|  |  | +    keyword: 'PostEditor',
 | 
	
		
			
				|  |  | +    tips: ''
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +let tagData = []
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const getInputBox = () => {
 | 
	
		
			
				|  |  | +  return document.querySelector('div[data-testid="tweetTextarea_0"]');
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +export const keywordReminderInit = () => {
 | 
	
		
			
				|  |  | +  listenerKeywordInput();
 | 
	
		
			
				|  |  | +  addHighlightEleWrapper();
 | 
	
		
			
				|  |  | +  console.log('keywordReminderInit')
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +export const listenerKeywordInput = debounce(function() {
 | 
	
		
			
				|  |  | +  let inputBox = getInputBox();
 | 
	
		
			
				|  |  | +  if(inputBox) {
 | 
	
		
			
				|  |  | +    inputBox.oninput = function () {
 | 
	
		
			
				|  |  | +        highlightKeyword();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}, 800);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +export const highlightKeyword = () => {
 | 
	
		
			
				|  |  | +  let inputBox = getInputBox();
 | 
	
		
			
				|  |  | +  if(inputBox) {
 | 
	
		
			
				|  |  | +    let textEleArr = inputBox.querySelectorAll('span[data-text="true"]');
 | 
	
		
			
				|  |  | +    let lastRange;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if(textEleArr && textEleArr.length) {
 | 
	
		
			
				|  |  | +      for(let i = 0; i < keywordData.length; i++) {
 | 
	
		
			
				|  |  | +        let keyword = keywordData[i]['keyword'];
 | 
	
		
			
				|  |  | +        for(let j = 0; j < textEleArr.length; j++) {
 | 
	
		
			
				|  |  | +          let txt = textEleArr[j]['innerHTML'];
 | 
	
		
			
				|  |  | +          let startPos = txt.indexOf(keyword), endPos;
 | 
	
		
			
				|  |  | +          if(startPos > -1) {
 | 
	
		
			
				|  |  | +            endPos = startPos + keyword.length;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if(checkHasTag({keyword, startPos, endPos, rowIndex: j})) {
 | 
	
		
			
				|  |  | +              continue;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            lastRange = window.getSelection().getRangeAt(0)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            let range = new Range();
 | 
	
		
			
				|  |  | +            range.setStart(textEleArr[j].firstChild, startPos);
 | 
	
		
			
				|  |  | +            range.setEnd(textEleArr[j].firstChild, endPos);
 | 
	
		
			
				|  |  | +            window.getSelection().removeAllRanges();
 | 
	
		
			
				|  |  | +            window.getSelection().addRange(range);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            var range1 = window.getSelection().getRangeAt(0);
 | 
	
		
			
				|  |  | +            var rectDom = range1.getBoundingClientRect();
 | 
	
		
			
				|  |  | +            console.log(rectDom);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            window.getSelection().removeAllRanges();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if(lastRange) {
 | 
	
		
			
				|  |  | +              window.getSelection().addRange(lastRange)
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            tagData.push({
 | 
	
		
			
				|  |  | +              keyword,
 | 
	
		
			
				|  |  | +              rowIndex: j,
 | 
	
		
			
				|  |  | +              startPos,
 | 
	
		
			
				|  |  | +              endPos
 | 
	
		
			
				|  |  | +            })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            let inputBoxRect = inputBox.getBoundingClientRect();
 | 
	
		
			
				|  |  | +            let left = rectDom.left - inputBoxRect.left;
 | 
	
		
			
				|  |  | +            let top = rectDom.top - inputBoxRect.top + 2;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            let cssText = `left: ${left}px;top: ${top}px;width: ${rectDom.width}px;height: ${rectDom.height}px;`;
 | 
	
		
			
				|  |  | +            addHighlightDom({cssText, rowIndex: j, keyword})
 | 
	
		
			
				|  |  | +          } else {
 | 
	
		
			
				|  |  | +            removeTag({rowIndex: j, keyword})
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const checkHasTag = (params) => {
 | 
	
		
			
				|  |  | +  let hasTag = false;
 | 
	
		
			
				|  |  | +  for(let i = 0; i < tagData.length; i++) {
 | 
	
		
			
				|  |  | +    let item = tagData[i];
 | 
	
		
			
				|  |  | +    if(params.rowIndex == item.rowIndex && params.keyword == item.keyword && item.startPos == params.startPos && item.endPos == params.endPos) {
 | 
	
		
			
				|  |  | +      hasTag = true;
 | 
	
		
			
				|  |  | +      break;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  return hasTag;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const removeTag = (params) => {
 | 
	
		
			
				|  |  | +  let tagDom = document.getElementsByClassName(`${params.rowIndex}-${params.keyword}`);
 | 
	
		
			
				|  |  | +  tagData = tagData.filter(item => item.rowIndex == params.rowIndex && params.keyword == item.keyword)
 | 
	
		
			
				|  |  | +  if(tagDom && tagDom.length) {
 | 
	
		
			
				|  |  | +    for(let i = 0; i < tagDom.length; i++) {
 | 
	
		
			
				|  |  | +      tagDom[i].remove();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +export const addHighlightEleWrapper = () => {
 | 
	
		
			
				|  |  | +  let ele = document.createElement('div');
 | 
	
		
			
				|  |  | +  ele.style.cssText = 'pointer-events: none;mix-blend-mode: darken;';
 | 
	
		
			
				|  |  | +  ele.id = 'denet-keword-highlight-wrapper';
 | 
	
		
			
				|  |  | +  let inputBox = getInputBox();
 | 
	
		
			
				|  |  | +  if(inputBox) {
 | 
	
		
			
				|  |  | +    inputBox.parentNode.appendChild(ele);
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +export const addHighlightDom = (params) => {
 | 
	
		
			
				|  |  | +  let ele = document.createElement('div');
 | 
	
		
			
				|  |  | +  let defaultCssText = 'position: absolute;border: 1.5px dashed #FCAB40;box-sizing: border-box';
 | 
	
		
			
				|  |  | +  ele.style.cssText = params.cssText + defaultCssText;
 | 
	
		
			
				|  |  | +  ele.id = params.id || '';
 | 
	
		
			
				|  |  | +  ele.setAttribute("class", `${params.rowIndex}-${params.keyword}`);
 | 
	
		
			
				|  |  | +  let highlightEleWrapper = document.getElementById('denet-keword-highlight-wrapper');
 | 
	
		
			
				|  |  | +  if(highlightEleWrapper) {
 | 
	
		
			
				|  |  | +    highlightEleWrapper.appendChild(ele);
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}
 |