card.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. if (state.status == '加载' || state.status == 'iframe') {
  139. state.show_btn = true
  140. state.status = 'iframe'
  141. }
  142. }
  143. getPostDetail({
  144. params: {
  145. postId: state.postId
  146. }
  147. }).then((res) => {
  148. if (res && res.code == 0) {
  149. state.detail = JSON.parse(res.data.postBizData)
  150. if (!res.data.srcContentId) {
  151. reSetBindTwtterId({
  152. postId: state.postId,
  153. tweetId: state.tweetId
  154. })
  155. }
  156. console.log('postBizData', state.detail)
  157. // 加载iframe
  158. if (state.detail.viewBgImagePath) {
  159. state.cover_url = state.detail.viewBgImagePath
  160. }
  161. state.iframe_url = state.detail.convertUrl
  162. } else {
  163. state.status = '网页错误'
  164. }
  165. })
  166. }
  167. const clickCancel = () => {
  168. state.show_alert = false
  169. }
  170. const clickFixed = () => {
  171. state.handle_type = '固定右上角'
  172. getChromeStorage('fullCheck', (res) => {
  173. if (res && res.fullCheck) {
  174. // 固定
  175. handleFixed()
  176. } else {
  177. state.show_alert = true
  178. }
  179. })
  180. }
  181. // 固定
  182. const handleFull = () => {
  183. if (state.status != 'iframe' || !state.iframe_url) {
  184. return
  185. }
  186. // 切换状态
  187. state.status = '固定右上角'
  188. state.show_btn = false
  189. sendChromeTabMessage({
  190. actionType: 'Set_ToolBox_Fixed',
  191. data: {
  192. type: '全屏',
  193. iframe_url: state.iframe_url,
  194. tweetId: state.tweetId,
  195. }
  196. })
  197. // 清除当前iframe src
  198. state.iframe_url = ''
  199. }
  200. // 全屏
  201. const handleFixed = () => {
  202. // 切换状态
  203. state.show_btn = false
  204. state.status = '固定右上角'
  205. sendChromeTabMessage({
  206. actionType: 'Set_ToolBox_Fixed',
  207. data: {
  208. type: '固定右上角',
  209. iframe_url: state.iframe_url,
  210. tweetId: state.tweetId
  211. }
  212. })
  213. // 清除当前iframe src
  214. state.iframe_url = ''
  215. }
  216. const clickFull = () => {
  217. state.handle_type = '全屏'
  218. getChromeStorage('fullCheck', (res) => {
  219. if (res && res.fullCheck) {
  220. // 全屏
  221. handleFull()
  222. } else {
  223. state.show_alert = true
  224. }
  225. })
  226. }
  227. </script>
  228. <style lang="scss" >
  229. .pre-view {
  230. pointer-events: none;
  231. cursor: default;
  232. }
  233. .denet-toolbox {
  234. width: 100%;
  235. height: 100%;
  236. min-height: 100%;
  237. filter: drop-shadow(0px 4px 20px rgba(0, 0, 0, 0.2));
  238. border-radius: 12px;
  239. overflow: hidden;
  240. position: relative;
  241. .alert {
  242. text-align: center;
  243. position: absolute;
  244. top: 0;
  245. left: 0;
  246. width: 100%;
  247. height: 100%;
  248. .back {
  249. background: #000000;
  250. opacity: 0.8;
  251. position: absolute;
  252. top: 0;
  253. left: 0;
  254. width: 100%;
  255. height: 100%;
  256. cursor: auto;
  257. }
  258. .confirm {
  259. position: absolute;
  260. width: 355px;
  261. height: 180px;
  262. background: #FFFFFF;
  263. border-radius: 20px;
  264. top: 173px;
  265. left: 50%;
  266. margin-left: -180px;
  267. .title {
  268. font-weight: 600;
  269. font-size: 18px;
  270. color: #000000;
  271. margin-bottom: 34px;
  272. }
  273. .check {
  274. color: #899099;
  275. font-weight: 400;
  276. font-size: 14px;
  277. margin: 12px 15px 32px 0;
  278. text-align: right;
  279. align-content: center;
  280. justify-content: flex-end;
  281. display: flex;
  282. line-height: 17px;
  283. input {
  284. margin-right: 8px;
  285. }
  286. label {
  287. line-height: 19px;
  288. }
  289. }
  290. .handle {
  291. display: flex;
  292. justify-content: center;
  293. align-items: center;
  294. div {
  295. font-weight: 600;
  296. font-size: 16px;
  297. width: 156px;
  298. height: 47px;
  299. line-height: 47px;
  300. cursor: pointer;
  301. border-radius: 1000px;
  302. user-select: none;
  303. }
  304. .cancel {
  305. color: #000000;
  306. background: rgba(56, 154, 255, 0.01);
  307. border: 1px solid #E6E6E6;
  308. }
  309. .continue {
  310. background: #1D9BF0;
  311. font-weight: 600;
  312. margin-left: 11px;
  313. color: #FFFFFF;
  314. }
  315. }
  316. }
  317. }
  318. .head {
  319. width: 100%;
  320. height: 40px;
  321. background: #373737;
  322. display: flex;
  323. align-items: center;
  324. justify-content: space-between;
  325. div {
  326. display: flex;
  327. justify-content: center;
  328. }
  329. span {
  330. color: #FFFFFF;
  331. font-style: normal;
  332. font-weight: 500;
  333. font-size: 14px;
  334. margin-left: 16px;
  335. }
  336. img {
  337. width: 20px;
  338. height: 20px;
  339. cursor: pointer;
  340. }
  341. .full {
  342. margin-right: 16px;
  343. }
  344. .fixed {
  345. margin-right: 20px;
  346. }
  347. }
  348. .content {
  349. width: 100%;
  350. height: calc(100% - 40px);
  351. background: #686868;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. iframe {
  356. background: #fff;
  357. width: 100%;
  358. height: 100%;
  359. border: medium none;
  360. }
  361. .state {
  362. text-align: center;
  363. width: 100%;
  364. height: 100%;
  365. position: relative;
  366. display: flex;
  367. justify-content: center;
  368. align-items: center;
  369. .cover {
  370. width: 100%;
  371. height: 100%;
  372. background: #000000;
  373. opacity: 0.6;
  374. }
  375. .icon-loading {
  376. animation: loading 1s infinite linear;
  377. }
  378. .info {
  379. position: absolute;
  380. width: 100%;
  381. height: 100%;
  382. top: 0;
  383. z-index: 1;
  384. display: flex;
  385. flex-wrap: wrap;
  386. flex-direction: column;
  387. justify-content: center;
  388. img {
  389. margin-bottom: 14px;
  390. }
  391. div {
  392. margin-bottom: 40px;
  393. color: #fff;
  394. text-align: center;
  395. font-weight: 500;
  396. font-size: 22px;
  397. }
  398. .pinned {
  399. color: #8E8E8E;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. @keyframes loading {
  406. from {
  407. transform: rotate(0deg);
  408. }
  409. to {
  410. transform: rotate(360deg);
  411. }
  412. }
  413. </style>