|
@@ -1,17 +1,18 @@
|
|
|
import { getChromeStorage, setChromeStorage } from '@/uilts/chromeExtension.js'
|
|
|
import { throttle } from '@/uilts/help'
|
|
|
-import { srcPublishSuccess } from '@/http/publishApi'
|
|
|
+import { reportSrcPublishEvent } from '@/http/publishApi'
|
|
|
import Report from "@/log-center/log"
|
|
|
|
|
|
|
|
|
let dom = {};
|
|
|
|
|
|
-function twitterPinLogin(port) {
|
|
|
+function twitterPinLogin() {
|
|
|
if (window.location.href == 'https://api.twitter.com/oauth/authorize') {
|
|
|
let code = document.querySelector('code')
|
|
|
|
|
|
if (code) {
|
|
|
- port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
|
|
|
+ chrome.runtime.sendMessage({ actionType: "CONTENT_SEND_CODE", code: code.innerText }, () => { })
|
|
|
+ // port.postMessage({ state: 'CONTENT_SEND_CODE', code: code.innerText })
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -22,9 +23,9 @@ function twitterPinLogin(port) {
|
|
|
* 渲染要插入的dom,初始化逻辑
|
|
|
* @param port
|
|
|
*/
|
|
|
-function renderDom(port) {
|
|
|
+function renderDom() {
|
|
|
if (window.location.href.indexOf('https://twitter.com') > -1) {
|
|
|
- _createBtnDom(port);
|
|
|
+ _createBtnDom();
|
|
|
onWindowResize();
|
|
|
checkHasDeBtn();
|
|
|
setTimeout(() => {
|
|
@@ -263,7 +264,7 @@ function _addIframe() {
|
|
|
* 点击deNet按钮处理
|
|
|
* @private
|
|
|
*/
|
|
|
-function _deNetBtnClick(port) {
|
|
|
+function _deNetBtnClick() {
|
|
|
getUserInfo((res) => {
|
|
|
if (res) {
|
|
|
if (window.location.pathname != '/home') {
|
|
@@ -286,7 +287,8 @@ function _deNetBtnClick(port) {
|
|
|
dom.loadingImg.style.transform = 'rotate(0deg)'
|
|
|
dom.deBtn.innerHTML = '<span>DeNet<span>';
|
|
|
}, 3000)
|
|
|
- port.postMessage({ state: 'CONTENT_TWITTER_LOGIN' })
|
|
|
+
|
|
|
+ chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_LOGIN", data: '1' }, (res) => { console.log(res) })
|
|
|
}
|
|
|
})
|
|
|
}
|
|
@@ -314,7 +316,7 @@ function _setPublishContent(content) {
|
|
|
* @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
|
|
|
* @private
|
|
|
*/
|
|
|
-function _createBtnDom(port) {
|
|
|
+function _createBtnDom() {
|
|
|
let loadingImg = document.createElement('img');
|
|
|
loadingImg.id = 'de-btn-loading'
|
|
|
loadingImg.src = require("@/assets/img/icon-btn-loading.png");
|
|
@@ -359,13 +361,13 @@ function _createBtnDom(port) {
|
|
|
// }, res => {
|
|
|
// console.log(res);
|
|
|
// })
|
|
|
-
|
|
|
+
|
|
|
Report.reportLog({
|
|
|
pageSource: Report.pageSource.mainPage,
|
|
|
businessType: Report.businessType.buttonClick,
|
|
|
objectType: Report.objectType.buttonMain
|
|
|
});
|
|
|
- _deNetBtnClick(port);
|
|
|
+ _deNetBtnClick();
|
|
|
})
|
|
|
deBtn1.addEventListener('click', () => {
|
|
|
Report.reportLog({
|
|
@@ -373,10 +375,10 @@ function _createBtnDom(port) {
|
|
|
businessType: Report.businessType.buttonClick,
|
|
|
objectType: Report.objectType.buttonSecond
|
|
|
});
|
|
|
- _deNetBtnClick(port);
|
|
|
+ _deNetBtnClick();
|
|
|
})
|
|
|
deBtn2.addEventListener('click', () => {
|
|
|
- _deNetBtnClick(port);
|
|
|
+ _deNetBtnClick();
|
|
|
})
|
|
|
deBtn3.addEventListener('click', () => {
|
|
|
Report.reportLog({
|
|
@@ -384,7 +386,7 @@ function _createBtnDom(port) {
|
|
|
businessType: Report.businessType.buttonClick,
|
|
|
objectType: Report.objectType.buttonMain
|
|
|
});
|
|
|
- _deNetBtnClick(port);
|
|
|
+ _deNetBtnClick();
|
|
|
})
|
|
|
dom.deBtn = deBtn;
|
|
|
dom.deBtn1 = deBtn1;
|
|
@@ -457,271 +459,143 @@ function checkHasDeBtn() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据postID绑定推文id
|
|
|
+ * 点击发推,后端绑定推特id
|
|
|
*/
|
|
|
-function bindTwitterArtMethod({ postId, twitterId }) {
|
|
|
- let regex = new RegExp(bindTwitterArt.postId);
|
|
|
- if (regex.test(postId)) {
|
|
|
- if (twitterId && bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
|
|
|
- bindTwitterArt.isBindIng = true;
|
|
|
- srcPublishSuccess({
|
|
|
- params: {
|
|
|
- postId: postId,
|
|
|
- srcContentId: twitterId
|
|
|
- }
|
|
|
- }).then((res) => {
|
|
|
- if (res.code == 0) {
|
|
|
- Report.reportLog({
|
|
|
- objectType: Report.objectType.tweetPostBinded
|
|
|
- });
|
|
|
- bindTwitterArt.needBind = false;
|
|
|
- bindTwitterArt.postId = '';
|
|
|
- bindTwitterArt.isBindIng = false;
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
+function bindTwitterArtMethod() {
|
|
|
+ if (!bindTwitterArt.postId) {
|
|
|
+ return
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-function parseDOMRedPacket() {
|
|
|
- let _dom = null
|
|
|
- let arr = document.querySelectorAll('a') || []
|
|
|
- let _type = ''
|
|
|
- for (let i in arr) {
|
|
|
- if (arr[i].innerText == '#DeNet') {
|
|
|
- let _tweetId = ''
|
|
|
- let _article = arr[i].closest('article')
|
|
|
- let _txt_area = _article.querySelector('[lang][dir=auto]')
|
|
|
- let _postId = _txt_area.innerText
|
|
|
- _postId = _postId.match(/###([\s\S]*?)###/)[1]
|
|
|
- _dom = _article.querySelector('div[aria-labelledby]')
|
|
|
- if (_dom) {
|
|
|
- _type = 'card'
|
|
|
- } else {
|
|
|
- _type = 'parnet'
|
|
|
- _dom = _txt_area
|
|
|
+ if (bindTwitterArt.needBind && !bindTwitterArt.isBindIng) {
|
|
|
+ bindTwitterArt.isBindIng = true;
|
|
|
+ reportSrcPublishEvent({
|
|
|
+ params: {
|
|
|
+ postId: bindTwitterArt.postId,
|
|
|
}
|
|
|
- let _a_area = _article.querySelector('a[aria-label]')
|
|
|
- if (_a_area && _a_area.getAttribute('href')) {
|
|
|
- _tweetId = _a_area.getAttribute('href').split('/status/')[1] || ''
|
|
|
- if (bindTwitterArt.needBind) {
|
|
|
- bindTwitterArtMethod({ postId: _postId, twitterId: _tweetId });
|
|
|
- }
|
|
|
+ }).then((res) => {
|
|
|
+ if (res.code == 0) {
|
|
|
+ Report.reportLog({
|
|
|
+ objectType: Report.objectType.tweetPostBinded
|
|
|
+ });
|
|
|
+ bindTwitterArt.needBind = false;
|
|
|
+ bindTwitterArt.postId = '';
|
|
|
+ bindTwitterArt.isBindIng = false;
|
|
|
}
|
|
|
- replaceDOMRedPacket(_type, _dom, _postId, _tweetId)
|
|
|
- }
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
-function getTwitterIdByDOM(dom) {
|
|
|
- let _twitter_id = ''
|
|
|
- for (let i in dom) {
|
|
|
- if (dom[i].href.indexOf('/status/') > 0) {
|
|
|
- _twitter_id = dom[i].href.split('/status/')[1] || ''
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- return _twitter_id
|
|
|
-}
|
|
|
|
|
|
-let parse_dom = {}
|
|
|
|
|
|
-async function parseDOMRedPacketByShortUrl(port) {
|
|
|
- let _new_time = new Date().getTime()
|
|
|
- if ((_new_time - change_time) > 1000) {
|
|
|
- change_time = _new_time
|
|
|
- } else {
|
|
|
- return
|
|
|
- }
|
|
|
- // 为了减少声明变量次数
|
|
|
- parse_dom.dom = null
|
|
|
- parse_dom.txt_area = null
|
|
|
- parse_dom.short_url = ''
|
|
|
- parse_dom.postId = ''
|
|
|
- parse_dom.a_arr = null
|
|
|
- parse_dom.type = ''
|
|
|
- parse_dom.tweetId = ''
|
|
|
- parse_dom.article = null
|
|
|
- parse_dom.a_tweetId = null
|
|
|
- parse_dom.a_arr = document.querySelectorAll('a') || []
|
|
|
- for (let i in parse_dom.a_arr) {
|
|
|
- if (parse_dom.a_arr[i].innerText == '#DeNet') {
|
|
|
- parse_dom.article = parse_dom.a_arr[i].closest('article')
|
|
|
- parse_dom.dom = parse_dom.article.querySelector('div[aria-labelledby]')
|
|
|
- if (parse_dom.dom && !parse_dom.dom.querySelector('iframe') && parse_dom.dom.closest('article').querySelector('iframe')) {
|
|
|
- parse_dom.dom.style.display = 'none'
|
|
|
- continue
|
|
|
- }
|
|
|
- if (parse_dom.dom && parse_dom.dom.parentElement.querySelector('iframe')) {
|
|
|
- continue
|
|
|
- }
|
|
|
- parse_dom.short_url = getTwitterShortUrl(parse_dom.article)
|
|
|
- parse_dom.tweetId = getTwitterIdByDOM(parse_dom.article.querySelectorAll('a'))
|
|
|
- parse_dom.postId = await handleShortUrl(port, parse_dom.short_url)
|
|
|
-
|
|
|
- // 获取到postId了
|
|
|
- if (parse_dom.postId && parse_dom.tweetId) {
|
|
|
- console.log('bindTwitterArt.postId', bindTwitterArt.postId)
|
|
|
- console.log('parse_dom.postId', parse_dom.postId)
|
|
|
- // if (bindTwitterArt.needBind) {
|
|
|
- // bindTwitterArtMethod({ postId: parse_dom.postId, twitterId: parse_dom.tweetId });
|
|
|
- // }
|
|
|
- if (parse_dom.dom) {
|
|
|
- parse_dom.type = 'card'
|
|
|
- } else {
|
|
|
- parse_dom.type = 'parnet'
|
|
|
- parse_dom.txt_area = parse_dom.article.querySelector('[lang][dir=auto]')
|
|
|
- parse_dom.dom = parse_dom.txt_area
|
|
|
- }
|
|
|
- replaceDOMRedPacket(parse_dom.type, parse_dom.dom, parse_dom.postId, parse_dom.tweetId)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+import parseCard from './ParseCard'
|
|
|
+
|
|
|
+// 检测dom改变
|
|
|
+// 获取短链接
|
|
|
+// 查看本地是否有postid
|
|
|
+// 如果有 修改dom 返回
|
|
|
+// 如果没有 网络请求
|
|
|
+// 获取postid
|
|
|
+// 获取twitterid
|
|
|
+// 检测当前所有dom 如果没有
|
|
|
+
|
|
|
|
|
|
-// 校验推特短数组大小
|
|
|
-function checkShortUrlArraySize(_array) {
|
|
|
- if (new Blob(_array).size >= 1024 * 1024) {
|
|
|
- _array.splice(0, parseInt(_array.length / 2))
|
|
|
+
|
|
|
+let queue_num = 1
|
|
|
+
|
|
|
+export const changeQueueNum = (num = 0) => {
|
|
|
+ queue_num = queue_num + num
|
|
|
+ if (queue_num > 5) {
|
|
|
+ queue_num = 5
|
|
|
}
|
|
|
- return _array
|
|
|
}
|
|
|
|
|
|
-// 获取推特短链接
|
|
|
-function getTwitterShortUrl(_article) {
|
|
|
- let dom_arr = _article.querySelectorAll('a[href][role]')
|
|
|
- let url = ''
|
|
|
- for (let i in dom_arr) {
|
|
|
- if (dom_arr[i].href.includes('https://t.co')) {
|
|
|
- url = dom_arr[i].href
|
|
|
- break
|
|
|
+let main_observer = null
|
|
|
+function onChangePageMain(targetNode) {
|
|
|
+ try {
|
|
|
+ const config = { attributes: false, childList: true, subtree: true };
|
|
|
+ const callback = (mutationsList, observer) => {
|
|
|
+ console.log('watch',mutationsList)
|
|
|
+ changeQueueNum(1)
|
|
|
}
|
|
|
+ main_observer = new MutationObserver(callback);
|
|
|
+ main_observer.observe(targetNode, config);
|
|
|
+ } catch (error) {
|
|
|
+ main_observer = null
|
|
|
}
|
|
|
- return url
|
|
|
}
|
|
|
-// 处理短链接
|
|
|
-async function handleShortUrl(port, url) {
|
|
|
- let post_id = ''
|
|
|
- // 校验本地是否存在
|
|
|
- let sort_link_data = await getChromeStorage('sortLink') || ''
|
|
|
- if (sort_link_data) {
|
|
|
- let _item = sort_link_data.filter((_item) => { return _item.url == url })
|
|
|
- // 本地有值
|
|
|
- if (_item.length > 0) {
|
|
|
- if (_item[0].post_id) {
|
|
|
- post_id = _item[0].post_id
|
|
|
- } else {
|
|
|
- // 防止多次请求,校验timeout时间
|
|
|
- let _new_time = new Date().getTime()
|
|
|
- if (_new_time - _item[0].time > 1000) {
|
|
|
- for (let i in sort_link_data) {
|
|
|
- if (sort_link_data[i].url == url) {
|
|
|
- sort_link_data[i].time = _new_time
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- setChromeStorage({ sortLink: JSON.stringify(sort_link_data) })
|
|
|
- port.postMessage({ state: 'CONTENT_TWITTER_SHORT_LINK', url })
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 本地没有值
|
|
|
- sort_link_data.push({ url, post_id: '', time: new Date().getTime() })
|
|
|
- setChromeStorage({ sortLink: JSON.stringify(sort_link_data) })
|
|
|
- port.postMessage({ state: 'CONTENT_TWITTER_SHORT_LINK', url })
|
|
|
+
|
|
|
+// 1.监听main改变
|
|
|
+// 2.监听卡片是否可见
|
|
|
+// 3.如果可见了 去找
|
|
|
+
|
|
|
+function setIframeRedPacket() {
|
|
|
+ // 获取所有卡片参数
|
|
|
+ let card_json_data = parseCard.parseAllDeNetCardParmas()
|
|
|
+ // 过滤出可以请求的短链接
|
|
|
+ parseCard.getCardParmas(card_json_data).then((res) => {
|
|
|
+ for (let i in res.has_post_Id_card_data) {
|
|
|
+ parseCard.replaceDOMRedPacket(res.has_post_Id_card_data[i])
|
|
|
}
|
|
|
- // 校验存储大小
|
|
|
- let new_item = checkShortUrlArraySize(sort_link_data)
|
|
|
- if (sort_link_data.length != new_item.length) {
|
|
|
- setChromeStorage({ sortLink: JSON.stringify(new_item) })
|
|
|
+ if (res.need_net_short_url.length > 0) {
|
|
|
+ // 请求短链接
|
|
|
+ chrome.runtime.sendMessage({ actionType: "CONTENT_TWITTER_SHORT_LINK", data: "", arr_url: res.need_net_short_url }, () => { })
|
|
|
}
|
|
|
- } else {
|
|
|
- setChromeStorage({ sortLink: JSON.stringify([{ url, post_id: '', time: new Date().getTime() }]) })
|
|
|
- port.postMessage({ state: 'CONTENT_TWITTER_SHORT_LINK', url })
|
|
|
- }
|
|
|
- return post_id
|
|
|
-}
|
|
|
-
|
|
|
-function createIframe(postId, tweetId) {
|
|
|
- let _iframe = document.createElement('iframe')
|
|
|
- _iframe.id = postId
|
|
|
- _iframe.src = chrome.runtime.getURL('/iframe/red-packet.html') + `?postId=${postId}&tweetId=${tweetId}`;
|
|
|
- _iframe.style.cssText = 'border: medium none; width:375px;min-height:500px;'
|
|
|
- return _iframe
|
|
|
+ })
|
|
|
}
|
|
|
-function replaceDOMRedPacket(_type, _dom, postId, tweetId) {
|
|
|
- if (!_dom || !_dom.parentElement) {
|
|
|
- return
|
|
|
- }
|
|
|
- if (_dom.parentElement.querySelector('iframe')) {
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
- if (_type == 'card') {
|
|
|
- let _len
|
|
|
- _len = _dom.childNodes.length
|
|
|
- for (let i = 0; i < _len; i++) {
|
|
|
- _dom.children[i].style.display = 'none'
|
|
|
+// 监听点击发推 按钮绑定事件
|
|
|
+document.addEventListener('click', (e) => {
|
|
|
+ try {
|
|
|
+ if (e.target.dataset && e.target.dataset.testid && e.target.dataset.testid == 'tweetButton') {
|
|
|
+ bindTwitterArtMethod()
|
|
|
+ } else if (e.target.closest('div[data-testid=tweetButton]')) {
|
|
|
+ bindTwitterArtMethod()
|
|
|
}
|
|
|
- _dom.style = 'min-height:500px'
|
|
|
- _dom.appendChild(createIframe(postId, tweetId))
|
|
|
- } else {
|
|
|
- let _parent = _dom.parentNode
|
|
|
- _parent.appendChild(createIframe(postId, tweetId))
|
|
|
+ } catch (error) {
|
|
|
+ console.error('error', error)
|
|
|
}
|
|
|
-}
|
|
|
+})
|
|
|
|
|
|
-let change_time = new Date().getTime()
|
|
|
-function onChangePageMain(port, targetNode) {
|
|
|
- const config = { attributes: false, childList: true, subtree: true };
|
|
|
- const callback = (mutationsList, observer) => {
|
|
|
- parseDOMRedPacketByShortUrl(port)
|
|
|
- setTimeout(() => {
|
|
|
- parseDOMRedPacketByShortUrl(port)
|
|
|
- }, 1100)
|
|
|
+
|
|
|
+export function initExecuteScript(changes) {
|
|
|
+ if (changes.executeScript) {
|
|
|
+ let item = JSON.parse(changes.executeScript.newValue)
|
|
|
+ if (item.executeScript) {
|
|
|
+ init()
|
|
|
+ }
|
|
|
}
|
|
|
- const observer = new MutationObserver(callback);
|
|
|
- observer.observe(targetNode, config);
|
|
|
}
|
|
|
-
|
|
|
-function setIframeRedPacket(port) {
|
|
|
- // let elment = document.documentElement
|
|
|
-
|
|
|
- if (window.location.href.includes('twitter.com)')) {
|
|
|
+// 初始化
|
|
|
+export function init() {
|
|
|
+ if (window.location.href.indexOf('twitter.com') < 0) {
|
|
|
return
|
|
|
}
|
|
|
- // setInterval(() => {
|
|
|
- // parseDOMRedPacketByShortUrl(port)
|
|
|
- // }, 1000)
|
|
|
-
|
|
|
-
|
|
|
+ twitterPinLogin();
|
|
|
+ // 渲染dom
|
|
|
let targetNode = null
|
|
|
let timer = setInterval(() => {
|
|
|
targetNode = document.querySelector('main')
|
|
|
if (targetNode) {
|
|
|
clearInterval(timer)
|
|
|
- onChangePageMain(port, targetNode)
|
|
|
+ setInterval(() => {
|
|
|
+ if (window.location.href.includes('twitter.com)')) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (!main_observer) {
|
|
|
+ onChangePageMain(targetNode)
|
|
|
+ changeQueueNum(1)
|
|
|
+ }
|
|
|
+ console.log('main_observer', main_observer)
|
|
|
+ console.log('queue_num', queue_num)
|
|
|
+ if (queue_num <= 0) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setIframeRedPacket()
|
|
|
+ changeQueueNum(-1)
|
|
|
+ }, 1000)
|
|
|
}
|
|
|
}, 1000);
|
|
|
-}
|
|
|
-
|
|
|
-export function initExecuteScript(port,changes) {
|
|
|
- if (changes.executeScript) {
|
|
|
- let item = JSON.parse(changes.executeScript.newValue)
|
|
|
- if (item.executeScript) {
|
|
|
- init(port)
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+ renderDom();
|
|
|
|
|
|
|
|
|
-export function init(port) {
|
|
|
- if (window.location.href.indexOf('twitter.com') < 0) {
|
|
|
- return
|
|
|
- }
|
|
|
- twitterPinLogin(port);
|
|
|
- renderDom(port);
|
|
|
- setIframeRedPacket(port);
|
|
|
-
|
|
|
getChromeStorage("popupShowPublishDialog", (res) => {
|
|
|
console.log("popupShowPublishDialog", res);
|
|
|
if (res && res.show) {
|