|
@@ -1,7 +1,8 @@
|
|
|
import { getChromeStorage, setChromeStorage, LANDING_PAGE } from '@/uilts/chromeExtension.js'
|
|
|
import { throttle } from '@/uilts/help'
|
|
|
-import { getTtwitterRequestToken, twitterLogin } from '../server/twitter.js'
|
|
|
+import { getTtwitterRequestToken, twitterLogin, httpTwitterShortUrl } from '../server/twitter.js'
|
|
|
import { srcPublishSuccess } from '@/http/publishApi'
|
|
|
+import Report from "@/log-center/log"
|
|
|
|
|
|
let dom = {};
|
|
|
|
|
@@ -66,6 +67,44 @@ export function backTwitterPinLoginCode(code) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+export function backHttpTwitterShortUrl(url) {
|
|
|
+ return new Promise(function (resolve, reject) {
|
|
|
+ httpTwitterShortUrl(url).then(res => {
|
|
|
+ let _str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
|
|
|
+ let _post_id = _str_arr[1] || ''
|
|
|
+ console.log('_str_arr_post_id', _post_id)
|
|
|
+ if (!_post_id) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 解析
|
|
|
+ let _obj = {
|
|
|
+ url,
|
|
|
+ post_id: _post_id
|
|
|
+ // tweet_id
|
|
|
+ }
|
|
|
+ getChromeStorage('sortLink', item => {
|
|
|
+ if (item) {
|
|
|
+ for (let i in item) {
|
|
|
+ if (item[i].url == _obj.url) {
|
|
|
+ item[i] = _obj
|
|
|
+ }
|
|
|
+ // else{
|
|
|
+ // delete item[i].tweet_id
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ setChromeStorage({ sortLink: JSON.stringify(item) })
|
|
|
+ } else {
|
|
|
+ setChromeStorage({ sortLink: JSON.stringify([_obj]) })
|
|
|
+ }
|
|
|
+ resolve({
|
|
|
+ post_id: _post_id
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 渲染要插入的dom,初始化逻辑
|
|
|
* @param port
|
|
@@ -140,10 +179,6 @@ export function addPinedPop() {
|
|
|
let contentDom = document.createElement('div');
|
|
|
contentDom.innerHTML = "<div style='font-weight: 500;font-size: 18px;margin-top: 20px;margin-bottom: 20px'>📌 Pin an Extension is more convenient to open😄</div><div style='display: flex; align-items: center; justify-content: space-between;'><div style='display: flex; align-items: center; font-size: 14px; color: #899099; cursor: pointer;' id='de-remind'><input id='de-check' type='checkbox'/> Don't remind</div><div class='de-pin-skip' style='font-weight: 500; font-size: 16px; color: #1D9BF0;cursor: pointer;'>Skip</div></div>"
|
|
|
|
|
|
- let arrow = document.createElement('div');
|
|
|
- arrow.style.cssText = 'position: absolute;height: 15px;width: 15px;top: -8px;right: 80px;border-left: 0.5px solid rgb(145, 145, 145);border-top: 0.5px solid rgb(145, 145, 145);background: rgb(255, 255, 255);transform: rotate(45deg);';
|
|
|
-
|
|
|
- popWrapper.appendChild(arrow);
|
|
|
popWrapper.appendChild(img);
|
|
|
popWrapper.appendChild(contentDom);
|
|
|
|
|
@@ -185,8 +220,16 @@ let bindTwitterArt = {
|
|
|
*/
|
|
|
function _publishTweetEvent(contentStr, cb) {
|
|
|
setTimeout(() => {
|
|
|
- let publishTweetBtn = document.querySelector('div[role="dialog"]').querySelector('div[data-testid="tweetButton"]');
|
|
|
- publishTweetBtn.addEventListener('click', function () {
|
|
|
+ let publishTweetBtn;
|
|
|
+ let dialog = document.querySelector('div[role="dialog"]');
|
|
|
+ if (dialog) {
|
|
|
+ publishTweetBtn = dialog.querySelector('div[data-testid="tweetButton"]');
|
|
|
+ } else {
|
|
|
+ let domMain = document.querySelector('main[role="main"]');
|
|
|
+ publishTweetBtn = domMain && domMain.querySelector('div[data-testid="tweetButton"]');
|
|
|
+ }
|
|
|
+
|
|
|
+ publishTweetBtn && publishTweetBtn.addEventListener('click', function () {
|
|
|
bindTwitterArt.needBind = true;
|
|
|
bindTwitterArt.postId = contentStr;
|
|
|
cb && cb()
|
|
@@ -204,11 +247,23 @@ function _publishTweetEvent(contentStr, cb) {
|
|
|
function _addDeNetEditBtn(parent, dom, isClick = false) {
|
|
|
setTimeout(() => {
|
|
|
if (parent && parent.parentNode) {
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonView,
|
|
|
+ objectType: Report.objectType.buttonSecond
|
|
|
+ });
|
|
|
parent.parentNode.insertBefore(dom, parent.nextElementSibling);
|
|
|
} else {
|
|
|
setTimeout(() => {
|
|
|
parent = _getScheduleDom(isClick);
|
|
|
- parent && parent.parentNode && parent.parentNode.insertBefore(dom, parent.nextElementSibling);
|
|
|
+ if (parent && parent.parentNode) {
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonView,
|
|
|
+ objectType: Report.objectType.buttonSecond
|
|
|
+ });
|
|
|
+ parent.parentNode.insertBefore(dom, parent.nextElementSibling);
|
|
|
+ }
|
|
|
}, 1000)
|
|
|
}
|
|
|
})
|
|
@@ -389,15 +444,17 @@ function _createBtnDom(port) {
|
|
|
style.innerHTML = "#de-btn:hover{opacity: .9;};@-webkit-keyframes load{from{ transform: rotate(0deg);} to{transform: rotate(360deg);}}";
|
|
|
document.getElementsByTagName('head').item(0).appendChild(style);
|
|
|
|
|
|
+ // 左侧大屏按钮
|
|
|
let deBtn = document.createElement('span');
|
|
|
// const shadowDiv = document.createElement('div');
|
|
|
deBtn.innerHTML = '<span>DeNet</span>';
|
|
|
deBtn.id = 'de-btn';
|
|
|
deBtn.style.cssText = 'width:90%;height: 52px;text-align:center;line-height:52px;margin-bottom: 4px;margin-top: 4px;background: linear-gradient(274.8deg, #FF9900 -3.69%, #BD00FF 69.71%, #00F0FF 122.65%);color:#fff;font-size:17px;font-weight:700;border-radius:100px;cursor: pointer;display: flex;align-items: center;justify-content: center;';
|
|
|
|
|
|
+ // 编辑框内按钮
|
|
|
const deBtn1 = document.createElement('img');
|
|
|
let src = require("@/assets/img/icon-gift-pack.png");
|
|
|
- const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;marign-left:4px;margin-right:4px';
|
|
|
+ const smallDeBtnStyle = 'width:20px;height: 20px;cursor: pointer;padding: 0px 8px';
|
|
|
deBtn1.id = 'de-btn1';
|
|
|
deBtn1.style.cssText = smallDeBtnStyle;
|
|
|
deBtn1.src = src
|
|
@@ -407,21 +464,37 @@ function _createBtnDom(port) {
|
|
|
deBtn2.style.cssText = smallDeBtnStyle;
|
|
|
deBtn2.src = "@/assets/img/icon-gift-pack.png"
|
|
|
|
|
|
+ // 小屏按钮
|
|
|
const deBtn3 = document.createElement('img');
|
|
|
deBtn3.id = 'de-btn3'
|
|
|
deBtn3.src = require("@/assets/logo/128.png");
|
|
|
deBtn3.style.cssText = 'width:52px;height: 52px;margin-top:20px;cursor: pointer;';
|
|
|
|
|
|
deBtn.addEventListener('click', () => {
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonClick,
|
|
|
+ objectType: Report.objectType.buttonMain
|
|
|
+ });
|
|
|
_deNetBtnClick(port);
|
|
|
})
|
|
|
deBtn1.addEventListener('click', () => {
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonClick,
|
|
|
+ objectType: Report.objectType.buttonSecond
|
|
|
+ });
|
|
|
_deNetBtnClick(port);
|
|
|
})
|
|
|
deBtn2.addEventListener('click', () => {
|
|
|
_deNetBtnClick(port);
|
|
|
})
|
|
|
deBtn3.addEventListener('click', () => {
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonClick,
|
|
|
+ objectType: Report.objectType.buttonMain
|
|
|
+ });
|
|
|
_deNetBtnClick(port);
|
|
|
})
|
|
|
dom.deBtn = deBtn;
|
|
@@ -437,12 +510,22 @@ function addSliderNavDeBtn(isSmall = false) {
|
|
|
let deBtn = document.getElementById('de-btn');
|
|
|
if (bigDom && !deBtn) {
|
|
|
bigDom.appendChild(dom.deBtn);
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonView,
|
|
|
+ objectType: Report.objectType.buttonMain
|
|
|
+ });
|
|
|
}
|
|
|
} else {
|
|
|
let smallDom = document.querySelector('h1[role]').parentNode.parentNode;
|
|
|
let deBtn3 = document.getElementById('de-btn3');
|
|
|
if (smallDom && !deBtn3) {
|
|
|
smallDom.appendChild(dom.deBtn3);
|
|
|
+ Report.reportLog({
|
|
|
+ pageSource: Report.pageSource.mainPage,
|
|
|
+ businessType: Report.businessType.buttonView,
|
|
|
+ objectType: Report.objectType.buttonMain
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -499,6 +582,9 @@ function bindTwitterArtMethod({ postId, twitterId }) {
|
|
|
}
|
|
|
}).then((res) => {
|
|
|
if (res.code == 0) {
|
|
|
+ Report.reportLog({
|
|
|
+ objectType: Report.objectType.tweetPostBinded
|
|
|
+ });
|
|
|
bindTwitterArt.needBind = false;
|
|
|
bindTwitterArt.postId = '';
|
|
|
bindTwitterArt.isBindIng = false;
|
|
@@ -538,21 +624,139 @@ function parseDOMRedPacket() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+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.a_tweetId = parse_dom.article.querySelector('a[aria-label]')
|
|
|
+ if (parse_dom.a_tweetId && parse_dom.a_tweetId.getAttribute('href')) {
|
|
|
+ parse_dom.tweetId = parse_dom.a_tweetId.getAttribute('href').split('/status/')[1] || ''
|
|
|
+ }
|
|
|
+ parse_dom.postId = await handleShortUrl(port, parse_dom.short_url)
|
|
|
+ console.log('_postId', parse_dom.postId)
|
|
|
+ console.log('short_url', parse_dom.short_url)
|
|
|
+
|
|
|
+ // 获取到postId了
|
|
|
+ if (parse_dom.postId) {
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 校验推特短数组大小
|
|
|
+function checkShortUrlArraySize(_array) {
|
|
|
+ if (new Blob(_array).size >= 1024 * 1024) {
|
|
|
+ _array.splice(0, parseInt(_array.length / 2))
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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 > 5000) {
|
|
|
+ for (let i in sort_link_data) {
|
|
|
+ if (sort_link_data[i].url == url) {
|
|
|
+ sort_link_data[i].time = _new_time
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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 })
|
|
|
+ }
|
|
|
+ // 校验存储大小
|
|
|
+ let new_item = checkShortUrlArraySize(sort_link_data)
|
|
|
+ if (sort_link_data.length != new_item.length) {
|
|
|
+ setChromeStorage({ sortLink: JSON.stringify(new_item) })
|
|
|
+ }
|
|
|
+ } 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;height:500px;'
|
|
|
+ _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 (_type == 'card' && !_dom.querySelector('iframe') && _dom.closest('article').querySelector('iframe')) {
|
|
|
- _dom.style.display = 'none'
|
|
|
- return
|
|
|
- }
|
|
|
if (_dom.parentElement.querySelector('iframe')) {
|
|
|
return
|
|
|
}
|
|
@@ -563,7 +767,7 @@ function replaceDOMRedPacket(_type, _dom, postId, tweetId) {
|
|
|
for (let i = 0; i < _len; i++) {
|
|
|
_dom.children[i].style.display = 'none'
|
|
|
}
|
|
|
- _dom.style = 'height:500px'
|
|
|
+ _dom.style = 'min-height:500px'
|
|
|
_dom.appendChild(createIframe(postId, tweetId))
|
|
|
} else {
|
|
|
let _parent = _dom.parentNode
|
|
@@ -571,18 +775,35 @@ function replaceDOMRedPacket(_type, _dom, postId, tweetId) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// let change_time = new Date().getTime()
|
|
|
+// function onChangePageMain(port, targetNode) {
|
|
|
+// const config = { attributes: true, childList: true, subtree: true };
|
|
|
+// const callback = (mutationsList, observer) => {
|
|
|
+// setTimeout(() => {
|
|
|
+// parseDOMRedPacketByShortUrl(port)
|
|
|
+// }, 2000)
|
|
|
+// }
|
|
|
+// const observer = new MutationObserver(callback);
|
|
|
+// observer.observe(targetNode, config);
|
|
|
+// }
|
|
|
|
|
|
-export function setIframeRedPacket() {
|
|
|
+export function setIframeRedPacket(port) {
|
|
|
// let elment = document.documentElement
|
|
|
|
|
|
- if (window.location.href.includes('https://twitter.com)')) {
|
|
|
+ if (window.location.href.includes('twitter.com)')) {
|
|
|
return
|
|
|
}
|
|
|
- // const observer = new MutationObserver(callback);
|
|
|
-
|
|
|
- parseDOMRedPacket()
|
|
|
- // let _current_top = 0
|
|
|
setInterval(() => {
|
|
|
- parseDOMRedPacket()
|
|
|
+ parseDOMRedPacketByShortUrl(port)
|
|
|
}, 1000)
|
|
|
+
|
|
|
+
|
|
|
+ // let targetNode = null
|
|
|
+ // let timer = setInterval(() => {
|
|
|
+ // targetNode = document.querySelector('main')
|
|
|
+ // if (targetNode) {
|
|
|
+ // clearInterval(timer)
|
|
|
+ // onChangePageMain(port, targetNode)
|
|
|
+ // }
|
|
|
+ // }, 1000);
|
|
|
}
|