|
@@ -0,0 +1,301 @@
|
|
|
+/* eslint-disable */
|
|
|
+import { getChromeStorage, setChromeStorage } from '../uilts/chromeExtension.js'
|
|
|
+import { getTtwitterRequestToken, twitterLogin } from '../server/twitter.js'
|
|
|
+
|
|
|
+let tweetBtn;
|
|
|
+
|
|
|
+let deBtn, deBtn1, deBtn2;
|
|
|
+
|
|
|
+export function contentTwitterPinLogin(port) {
|
|
|
+ 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 })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+let authToken = ''
|
|
|
+export function backTwitterPinLoginToken() {
|
|
|
+ // 1.判断是否登陆了
|
|
|
+ getChromeStorage('userInfo', (res) => {
|
|
|
+ // 没有登陆
|
|
|
+ if (!res) {
|
|
|
+ getTtwitterRequestToken().then((res) => {
|
|
|
+ authToken = res.data.authToken
|
|
|
+ chrome.tabs.create({
|
|
|
+ url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+export function backTwitterPinLoginCode(code) {
|
|
|
+ // 关闭code页面
|
|
|
+ chrome.tabs.query({}, (tab) => {
|
|
|
+ for (let i in tab) {
|
|
|
+ console.log(tab[i])
|
|
|
+ if (tab[i].url == 'https://api.twitter.com/oauth/authorize') {
|
|
|
+ chrome.tabs.remove(tab[i].id)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // 发送请求
|
|
|
+ // token,code
|
|
|
+ twitterLogin(authToken, code).then(res => {
|
|
|
+ if (res.code == 0) {
|
|
|
+ setChromeStorage({ userInfo: JSON.stringify(res.data) })
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function getUserInfo (cb) {
|
|
|
+ getChromeStorage('userInfo', (res) => {
|
|
|
+ cb && cb(res);
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 渲染要插入的dom,初始化逻辑
|
|
|
+ * @param port
|
|
|
+ */
|
|
|
+export function renderDom (port) {
|
|
|
+ let {deBtn: deNet, deBtn1: deNet1, deBtn2: deNet2} = _createBtnDom(port);
|
|
|
+ deBtn = deNet;
|
|
|
+ deBtn1 = deNet1;
|
|
|
+ deBtn2 = deNet2;
|
|
|
+ setTimeout(() => {
|
|
|
+ _addIframe();
|
|
|
+ _addDeNetBtn();
|
|
|
+ _getSliderTwitterBtn();
|
|
|
+ }, 800)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 监听dialog内点击原生发布按钮事件
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _publishTweetEvent (cb) {
|
|
|
+ let publishTweetBtn = document.querySelector('div[role="dialog"]').querySelector('div[data-testid="tweetButton"]');
|
|
|
+ publishTweetBtn.addEventListener('click', function () {
|
|
|
+ setTimeout(() => {
|
|
|
+ let twitterArtId = _getTwitterArtId();
|
|
|
+ cb && cb(twitterArtId)
|
|
|
+ }, 1000)
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取插入的iframe
|
|
|
+ * @returns {HTMLElement}
|
|
|
+ */
|
|
|
+function getIframe () {
|
|
|
+ let iframe = document.getElementById('iframe-test');
|
|
|
+ return iframe;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 展示give弹窗
|
|
|
+ */
|
|
|
+export function showGiveDialogHandler() {
|
|
|
+ getIframe().contentWindow.postMessage({actionType : 'showGiveDialog'}, '*');
|
|
|
+}
|
|
|
+
|
|
|
+export function showIframeHandler () {
|
|
|
+ getIframe().style.display = 'block';
|
|
|
+}
|
|
|
+
|
|
|
+export function hideIframeHandler () {
|
|
|
+ getIframe().style.display = 'none';
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 展示twitter原生发布框
|
|
|
+ */
|
|
|
+export function showTwitterPublishDialogHandler () {
|
|
|
+ tweetBtn.click();
|
|
|
+ _publishTweetEvent();
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 在输入推文区插入deNet按钮
|
|
|
+ * @param parent
|
|
|
+ * @param dom
|
|
|
+ * @param isClick
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _addDeNetEditBtn (parent, dom, isClick = false) {
|
|
|
+ setTimeout(() => {
|
|
|
+ if (parent) {
|
|
|
+ parent.parentNode.insertBefore(dom, parent.nextSibling);
|
|
|
+ } else {
|
|
|
+ setTimeout(() => {
|
|
|
+ parent = _getScheduleDom(isClick);
|
|
|
+ parent.parentNode.insertBefore(dom, parent.nextSibling);
|
|
|
+ }, 1000)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 在dialog插入deNet按钮
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _addDeNetBtnToDialog () {
|
|
|
+ setTimeout(() => {
|
|
|
+ let dialogScheduleBtn = _getScheduleDom(true);
|
|
|
+ _addDeNetEditBtn(dialogScheduleBtn, deBtn2);
|
|
|
+ }, 800)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取左侧twitter按钮
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _getSliderTwitterBtn () {
|
|
|
+ tweetBtn = document.querySelector('a[data-testid="SideNav_NewTweet_Button"]');
|
|
|
+ tweetBtn.addEventListener('click', function () {
|
|
|
+ _addDeNetBtnToDialog();
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 添加deNet按钮
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _addDeNetBtn () {
|
|
|
+ setTimeout(() => {
|
|
|
+ let node = document.querySelector('header[role="banner"]').querySelector('.r-1habvwh');
|
|
|
+ if (node) {
|
|
|
+ node.appendChild(deBtn);
|
|
|
+ }
|
|
|
+
|
|
|
+ let dialogScheduleBtn = _getScheduleDom(false);
|
|
|
+ _addDeNetEditBtn(dialogScheduleBtn, deBtn1);
|
|
|
+ }, 800)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取推文输入框内dom,用于插入deNet
|
|
|
+ * @param isDialogInner
|
|
|
+ * @returns {Element}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _getScheduleDom (isDialogInner = false) {
|
|
|
+ let scheduleBtn;
|
|
|
+ if (isDialogInner) {
|
|
|
+ scheduleBtn = document.querySelector('div[role="dialog"]').querySelector('[data-testid="createPollButton"]');
|
|
|
+ } else {
|
|
|
+ // eslint-disable-next-line no-unused-vars
|
|
|
+ let toolBar = document.querySelector('div[data-testid="toolBar"]');
|
|
|
+ if (toolBar) {
|
|
|
+ scheduleBtn = toolBar.querySelector('div[data-testid="createPollButton"]');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return scheduleBtn;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 插入iframe到页面
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _addIframe () {
|
|
|
+ // let span = document.createElement('span');
|
|
|
+ // const shadowRoot = span.attachShadow({mode: 'closed'})
|
|
|
+ let iframe = document.createElement('iframe');
|
|
|
+ iframe.src = chrome.runtime.getURL('/iframe/test.html')
|
|
|
+ iframe.id = 'iframe-test'
|
|
|
+ iframe.style.cssText = 'position:fixed;top:0px;right:0;display:block; width:100%;height:100%;z-index:0; border: medium none;display:none';
|
|
|
+ // shadowRoot.appendChild(iframe);
|
|
|
+ // document.body.appendChild(span)
|
|
|
+ document.getElementById('layers').appendChild(iframe)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取发布推文id
|
|
|
+ * @returns {string}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _getTwitterArtId () {
|
|
|
+ let firstTwitterArtId = document.querySelector('article[data-testid="tweet"]').querySelector('a[dir="auto"]').getAttribute('href');
|
|
|
+ return firstTwitterArtId;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 点击deNet按钮处理
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _deNetBtnClick(port) {
|
|
|
+ getUserInfo((res) => {
|
|
|
+ if (res) {
|
|
|
+ showGiveDialogHandler();
|
|
|
+ } else {
|
|
|
+ port.postMessage({ state: 'CONTENT_TWITTER_LOGIN' })
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设置发布内容
|
|
|
+ * @param content
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _setPublishContent (content) {
|
|
|
+ setTimeout(() => {
|
|
|
+ document.execCommand("insertText", false, content);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 创建deNet按钮 添加到页面
|
|
|
+ * @returns {{deBtn2: HTMLDivElement, deBtn1: HTMLDivElement, deBtn: HTMLSpanElement}}
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+function _createBtnDom (port) {
|
|
|
+ let deBtn = document.createElement('span');
|
|
|
+ const shadowRoot = deBtn.attachShadow({mode: 'closed'})
|
|
|
+ const shadowDiv = document.createElement('div');
|
|
|
+ shadowDiv.innerText = 'DeNet';
|
|
|
+ shadowDiv.id = 'de-btn';
|
|
|
+ shadowDiv.style.cssText = 'width:220px;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:15px;font-weight:700;border-radius:100px;cursor: pointer;';
|
|
|
+ shadowRoot.appendChild(shadowDiv);
|
|
|
+
|
|
|
+ const deBtn1 = document.createElement('div');
|
|
|
+ const smallDeBtnStyle = 'width:30px;height: 30px;text-align:center;line-height:30px;background: linear-gradient(274.8deg, #FF9900 -3.69%, #BD00FF 69.71%, #00F0FF 122.65%);color:#fff;font-size:12px;font-weight:500;border-radius:4px;cursor: pointer;';
|
|
|
+ deBtn1.innerText = 'De';
|
|
|
+ deBtn1.id = 'de-btn1';
|
|
|
+ deBtn1.style.cssText = smallDeBtnStyle;
|
|
|
+
|
|
|
+ const deBtn2 = document.createElement('div');
|
|
|
+ deBtn2.innerText = 'De';
|
|
|
+ deBtn2.id = 'de-btn2';
|
|
|
+ deBtn2.style.cssText = smallDeBtnStyle;
|
|
|
+
|
|
|
+ deBtn.addEventListener('click', () => {
|
|
|
+ _deNetBtnClick(port);
|
|
|
+ })
|
|
|
+ deBtn1.addEventListener('click', () => {
|
|
|
+ _deNetBtnClick(port);
|
|
|
+ })
|
|
|
+ deBtn2.addEventListener('click', () => {
|
|
|
+ _deNetBtnClick(port);
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ deBtn,
|
|
|
+ deBtn1,
|
|
|
+ deBtn2
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* setInterval(() => {
|
|
|
+ let toolBar = document.querySelector('div[data-testid="toolBar"]');
|
|
|
+ let innerDeIcon = document.getElementById('de-icon');
|
|
|
+ if (toolBar && !innerDeIcon) {
|
|
|
+ // let dialogScheduleBtn = _getScheduleDom(false);
|
|
|
+ // _addDeNetEditBtn(dialogScheduleBtn, deBtn1);
|
|
|
+ }
|
|
|
+}, 800) */
|