card.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <div class="denet-toolbox" :class="{ 'pre-view': pre_view }">
  3. <div class="head">
  4. <span></span>
  5. <div v-show="state.show_btn && state.status == 'iframe'">
  6. <img :src="require('@/assets/svg/icon-iframe-fixed.svg')" alt class="fixed" @click="clickFixed" />
  7. <img :src="require('@/assets/svg/icon-iframe-full.svg')" alt class="full" @click="clickFull" />
  8. </div>
  9. </div>
  10. <div class="content" v-if="pre_view">
  11. <iframe :src="iframe_url" frameborder="0" sandbox></iframe>
  12. </div>
  13. <div class="content" v-else>
  14. <iframe :src="state.iframe_url" v-show="state.status == 'iframe'" ref="dom_iframe" frameborder="0"
  15. scrolling="yes" allow="camera *;microphone *"></iframe>
  16. <!-- sandbox="allow-same-origin allow-scripts allow-popups allow-top-navigation allow-forms allow-modals allow-popups-to-escape-sandbox" -->
  17. <!-- 网页错误 -->
  18. <div class="state" v-show="state.status == '网页错误'">
  19. <img :src="state.cover_url" alt="" class="cover" />
  20. <div class="info">
  21. <img :src="require('@/assets/svg/icon-iframe-error.svg')" alt />
  22. <div>Oops, this link is invalid</div>
  23. </div>
  24. </div>
  25. <!-- 加载 -->
  26. <div class="state" v-show="state.status == '加载'">
  27. <img :src="state.cover_url" alt="" class="cover" />
  28. <div class="info">
  29. <img :src="require('@/assets/svg/icon-iframe-loading.svg')" alt class="icon-loading" />
  30. </div>
  31. </div>
  32. <!-- 关闭 -->
  33. <div class="state" v-show="state.status == '关闭'">
  34. </div>
  35. <!-- 固定右上角 -->
  36. <div class="state" v-show="state.status == '固定右上角'">
  37. <div class="info">
  38. <img :src="require('@/assets/svg/icon-iframe-pinned.svg')" alt />
  39. <div class="pinned">Pinned to the top right</div>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- alert -->
  44. <div class="alert" v-show="state.show_alert">
  45. <div class="back" @click.stop="clickCancel"></div>
  46. <div class="confirm">
  47. <div class="check">
  48. <input :id="state.checkbox_id" type='checkbox' v-model="state.checkbox" />
  49. <label :for="state.checkbox_id">Don't remind</label>
  50. </div>
  51. <div class="title">Web Page Progress May Reset</div>
  52. <div class="handle">
  53. <div class="cancel" @click.stop="clickCancel">Cancel</div>
  54. <div class="continue" @click.stop="clickContinue">Continue</div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script setup>
  61. import { getChromeStorage, setChromeStorage, defineProps, sendChromeTabMessage } from "@/uilts/chromeExtension";
  62. import { getPostDetail } from '@/http/redPacket.js'
  63. import { guid, getQueryString } from "@/uilts/help";
  64. import { onMounted, reactive, ref } from "vue";
  65. import { reSetBindTwtterId } from '@/http/help.js'
  66. let dom_iframe = ref(null)
  67. let state = reactive({
  68. status: '', //
  69. show_alert: false,
  70. show_btn: false,
  71. list: [],
  72. checkbox: false,
  73. checkbox_id: `denet-${guid()}`,
  74. postId: '',
  75. tweetId: '',
  76. detail: {},
  77. handle_type: '',
  78. cover_url: require('@/assets/img/back-loading.png')
  79. })
  80. let dom = {}
  81. let props = defineProps({
  82. pre_view: {
  83. type: Boolean,
  84. default: false,
  85. },
  86. iframe_url: {
  87. type: String,
  88. default: ''
  89. }
  90. })
  91. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  92. switch (req.actionType) {
  93. // 事件传输
  94. case 'Set_ToolBox_Fixed':
  95. if (req.data.tweetId == state.tweetId && req.data.type == '关闭') {
  96. state.show_btn = true
  97. state.status = 'iframe'
  98. state.iframe_url = req.data.iframe_url
  99. }
  100. break
  101. }
  102. })
  103. const clickContinue = () => {
  104. if (state.checkbox) {
  105. setChromeStorage({ fullCheck: JSON.stringify({ fullCheck: 1 }) })
  106. }
  107. if (state.handle_type == '全屏') {
  108. handleFull()
  109. } else {
  110. handleFixed()
  111. }
  112. state.show_alert = false
  113. }
  114. onMounted(() => {
  115. if (props.pre_view) {
  116. return
  117. }
  118. state.postId = getQueryString('postId')
  119. state.tweetId = getQueryString('tweetId')
  120. sendChromeTabMessage({
  121. actionType: 'Get_ToolBox_Fixed_TweetId'
  122. }, (res) => {
  123. if (res == state.tweetId) {
  124. state.status = '固定右上角'
  125. } else {
  126. state.status = '加载'
  127. getDetail()
  128. }
  129. })
  130. })
  131. // detail函数
  132. const getDetail = () => {
  133. let iframe = dom_iframe.value
  134. iframe.onerror = () => {
  135. state.status = '网页错误'
  136. }
  137. // iframe.onload = () => {
  138. // alert(123)
  139. // if (state.status == '加载' || state.status == 'iframe') {
  140. // state.show_btn = true
  141. // state.status = 'iframe'
  142. // }
  143. // }
  144. getPostDetail({
  145. params: {
  146. postId: state.postId
  147. }
  148. }).then((res) => {
  149. if (res && res.code == 0) {
  150. state.detail = JSON.parse(res.data.postBizData)
  151. if (!res.data.srcContentId) {
  152. reSetBindTwtterId({
  153. postId: state.postId,
  154. tweetId: state.tweetId
  155. })
  156. }
  157. console.log('postBizData', state.detail)
  158. // 加载iframe
  159. // if (state.detail.viewBgImagePath) {
  160. // state.cover_url = state.detail.viewBgImagePath
  161. // }
  162. state.iframe_url = state.detail.convertUrl
  163. state.show_btn = true
  164. state.status = 'iframe'
  165. } else {
  166. state.status = '网页错误'
  167. }
  168. })
  169. }
  170. const clickCancel = () => {
  171. state.show_alert = false
  172. }
  173. const clickFixed = () => {
  174. state.handle_type = '固定右上角'
  175. getChromeStorage('fullCheck', (res) => {
  176. if (res && res.fullCheck) {
  177. // 固定
  178. handleFixed()
  179. } else {
  180. state.show_alert = true
  181. }
  182. })
  183. }
  184. // 固定
  185. const handleFull = () => {
  186. if (state.status != 'iframe' || !state.iframe_url) {
  187. return
  188. }
  189. // 切换状态
  190. state.status = '固定右上角'
  191. state.show_btn = false
  192. sendChromeTabMessage({
  193. actionType: 'Set_ToolBox_Fixed',
  194. data: {
  195. type: '全屏',
  196. iframe_url: state.iframe_url,
  197. tweetId: state.tweetId,
  198. }
  199. })
  200. // 清除当前iframe src
  201. state.iframe_url = ''
  202. }
  203. // 全屏
  204. const handleFixed = () => {
  205. // 切换状态
  206. state.show_btn = false
  207. state.status = '固定右上角'
  208. sendChromeTabMessage({
  209. actionType: 'Set_ToolBox_Fixed',
  210. data: {
  211. type: '固定右上角',
  212. iframe_url: state.iframe_url,
  213. tweetId: state.tweetId
  214. }
  215. })
  216. // 清除当前iframe src
  217. state.iframe_url = ''
  218. }
  219. const clickFull = () => {
  220. state.handle_type = '全屏'
  221. getChromeStorage('fullCheck', (res) => {
  222. if (res && res.fullCheck) {
  223. // 全屏
  224. handleFull()
  225. } else {
  226. state.show_alert = true
  227. }
  228. })
  229. }
  230. </script>
  231. <style lang="scss" >
  232. .pre-view {
  233. pointer-events: none;
  234. cursor: default;
  235. }
  236. .denet-toolbox {
  237. width: 100%;
  238. height: 100%;
  239. min-height: 100%;
  240. filter: drop-shadow(0px 4px 20px rgba(0, 0, 0, 0.2));
  241. border-radius: 12px;
  242. overflow: hidden;
  243. position: relative;
  244. .alert {
  245. text-align: center;
  246. position: absolute;
  247. top: 0;
  248. left: 0;
  249. width: 100%;
  250. height: 100%;
  251. .back {
  252. background: #000000;
  253. opacity: 0.8;
  254. position: absolute;
  255. top: 0;
  256. left: 0;
  257. width: 100%;
  258. height: 100%;
  259. cursor: auto;
  260. }
  261. .confirm {
  262. position: absolute;
  263. width: 355px;
  264. height: 180px;
  265. background: #FFFFFF;
  266. border-radius: 20px;
  267. top: 173px;
  268. left: 50%;
  269. margin-left: -180px;
  270. .title {
  271. font-weight: 600;
  272. font-size: 18px;
  273. color: #000000;
  274. margin-bottom: 34px;
  275. }
  276. .check {
  277. color: #899099;
  278. font-weight: 400;
  279. font-size: 14px;
  280. margin: 12px 15px 32px 0;
  281. text-align: right;
  282. align-content: center;
  283. justify-content: flex-end;
  284. display: flex;
  285. line-height: 17px;
  286. input {
  287. margin-right: 8px;
  288. }
  289. label {
  290. line-height: 19px;
  291. }
  292. }
  293. .handle {
  294. display: flex;
  295. justify-content: center;
  296. align-items: center;
  297. div {
  298. font-weight: 600;
  299. font-size: 16px;
  300. width: 156px;
  301. height: 47px;
  302. line-height: 47px;
  303. cursor: pointer;
  304. border-radius: 1000px;
  305. user-select: none;
  306. }
  307. .cancel {
  308. color: #000000;
  309. background: rgba(56, 154, 255, 0.01);
  310. border: 1px solid #E6E6E6;
  311. }
  312. .continue {
  313. background: #1D9BF0;
  314. font-weight: 600;
  315. margin-left: 11px;
  316. color: #FFFFFF;
  317. }
  318. }
  319. }
  320. }
  321. .head {
  322. width: 100%;
  323. height: 40px;
  324. background: #373737;
  325. display: flex;
  326. align-items: center;
  327. justify-content: space-between;
  328. div {
  329. display: flex;
  330. justify-content: center;
  331. }
  332. span {
  333. color: #FFFFFF;
  334. font-style: normal;
  335. font-weight: 500;
  336. font-size: 14px;
  337. margin-left: 16px;
  338. }
  339. img {
  340. width: 20px;
  341. height: 20px;
  342. cursor: pointer;
  343. }
  344. .full {
  345. margin-right: 16px;
  346. }
  347. .fixed {
  348. margin-right: 20px;
  349. }
  350. }
  351. .content {
  352. width: 100%;
  353. height: calc(100% - 40px);
  354. background: #686868;
  355. display: flex;
  356. align-items: center;
  357. justify-content: center;
  358. iframe {
  359. background: #fff;
  360. width: 100%;
  361. height: 100%;
  362. border: medium none;
  363. }
  364. .state {
  365. text-align: center;
  366. width: 100%;
  367. height: 100%;
  368. position: relative;
  369. display: flex;
  370. justify-content: center;
  371. align-items: center;
  372. .cover {
  373. width: 100%;
  374. height: 100%;
  375. background: #000000;
  376. opacity: 0.6;
  377. }
  378. .icon-loading {
  379. animation: loading 1s infinite linear;
  380. }
  381. .info {
  382. position: absolute;
  383. width: 100%;
  384. height: 100%;
  385. top: 0;
  386. z-index: 1;
  387. display: flex;
  388. flex-wrap: wrap;
  389. flex-direction: column;
  390. justify-content: center;
  391. img {
  392. margin-bottom: 14px;
  393. }
  394. div {
  395. margin-bottom: 40px;
  396. color: #fff;
  397. text-align: center;
  398. font-weight: 500;
  399. font-size: 22px;
  400. }
  401. .pinned {
  402. color: #8E8E8E;
  403. }
  404. }
  405. }
  406. }
  407. }
  408. @keyframes loading {
  409. from {
  410. transform: rotate(0deg);
  411. }
  412. to {
  413. transform: rotate(360deg);
  414. }
  415. }
  416. </style>