index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <div class="denet-toolbox" @click.stop="clickHead" ref="dom_toolbox" :class="{ 'pre-view': pre_view }">
  3. <div class="head" @click.stop="clickHead">
  4. <span></span>
  5. <div v-show="state.show_btn && state.status == ''">
  6. <img :src="require('@/assets/img/icon-fixed.png')" alt class="fixed" @click.stop="clickFixed" />
  7. <img :src="require('@/assets/img/icon-full.png')" alt class="full" @click.stop="clickFull" />
  8. <!-- <img :src="require('@/assets/img/icon-close.png')" alt class="full" @click="clickFull" /> -->
  9. </div>
  10. </div>
  11. <div class="content" v-if="pre_view">
  12. <iframe :src="iframe_url" frameborder="0"></iframe>
  13. </div>
  14. <div class="content" v-else>
  15. <iframe :src="state.iframe_url" v-show="state.status == ''" ref="dom_iframe" frameborder="0"></iframe>
  16. <!-- 网页错误 -->
  17. <div class="state" v-show="state.status == '网页错误'">
  18. <img :src="require('@/assets/img/icon-page-fail.png')" alt />
  19. <div>Oops, this link is invalid</div>
  20. </div>
  21. <!-- 加载 -->
  22. <div class="state" v-show="state.status == '加载'">
  23. <img :src="require('@/assets/img/icon-loading-gray.png')" alt class="icon-loading" />
  24. </div>
  25. <!-- 关闭 -->
  26. <div class="state" v-show="state.status == '关闭'">
  27. </div>
  28. <!-- 固定右上角 -->
  29. <div class="state" v-show="state.status == '固定右上角'">
  30. <img :src="require('@/assets/img/icon-fixed-gray.png')" alt />
  31. <div>Pinned to the top right</div>
  32. </div>
  33. </div>
  34. <!-- alert -->
  35. <div class="alert" v-show="state.show_alert">
  36. <div class="back" @click.stop="clickCancel"></div>
  37. <div class="confirm">
  38. <div class="check">
  39. <input :id="state.checkbox_id" type='checkbox' v-model="state.checkbox" />
  40. <label :for="state.checkbox_id">Don't remind</label>
  41. </div>
  42. <div class="title">Web Page Progress May Reset</div>
  43. <div class="handle">
  44. <div class="cancel" @click.stop="clickCancel">Cancel</div>
  45. <div class="continue" @click.stop="clickContinue">Continue</div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </template>
  51. <script setup>
  52. import { getChromeStorage, setChromeStorage, httpContentToBack, defineProps } from "@/uilts/chromeExtension";
  53. import { guid } from "@/uilts/help";
  54. import { sendEventInfo } from "@/uilts/event";
  55. import { onMounted, reactive, ref } from "vue";
  56. let dom_toolbox = ref(null)
  57. let dom_iframe = ref(null)
  58. let state = reactive({
  59. status: '', //
  60. show_alert: false,
  61. show_btn: true,
  62. list: [],
  63. checkbox: false,
  64. checkbox_id: `denet-${guid()}`,
  65. postId: '',
  66. tweetId: '',
  67. detail: {},
  68. dom: {
  69. root: null,
  70. iframe: null
  71. },
  72. handle_type: ''
  73. })
  74. let props = defineProps({
  75. pre_view: {
  76. type: Boolean,
  77. default: false,
  78. },
  79. iframe_url: {
  80. type: String,
  81. default: ''
  82. }
  83. })
  84. window.addEventListener("onEvent", e => {
  85. let info = e.detail
  86. switch (info.event_type) {
  87. // 事件传输
  88. case 'ToolBox_Fixed_Close':
  89. if (info.data.tweetId == state.tweetId) {
  90. state.show_btn = true
  91. state.status = ''
  92. state.iframe_url = info.data.url
  93. }
  94. break
  95. }
  96. });
  97. const clickHead = () => {
  98. //
  99. }
  100. const clickContinue = () => {
  101. if (state.checkbox) {
  102. setChromeStorage({ fullCheck: JSON.stringify({ fullCheck: 1 }) })
  103. }
  104. if (state.handle_type == '全屏') {
  105. handleFull()
  106. } else {
  107. handleFixed()
  108. }
  109. state.show_alert = false
  110. }
  111. onMounted(() => {
  112. if (props.pre_view) {
  113. return
  114. }
  115. // twitterid
  116. // postid
  117. console.log('dom_toolbox', dom_toolbox.value)
  118. if (dom_toolbox.value) {
  119. state.dom_root = dom_toolbox.value.closest('div[data-tweet-id]')
  120. if (state.dom_root) {
  121. state.postId = state.dom_root.dataset.postId || ''
  122. state.tweetId = state.dom_root.dataset.tweetId || ''
  123. }
  124. }
  125. getDetail()
  126. })
  127. // detail函数
  128. const getDetail = () => {
  129. state.status = '加载'
  130. httpContentToBack({
  131. url: `/post/getDetail`,
  132. params: {
  133. postId: state.postId
  134. }
  135. }, (res) => {
  136. if (res && res.code == 0) {
  137. state.detail = JSON.parse(res.data.postBizData)
  138. console.log('postBizData', state.detail)
  139. // 加载iframe
  140. let iframe = dom_iframe.value
  141. // state.detail.convertUrl = 'https://www.bilibili.com'
  142. // iframe.onerror = () => {
  143. // state.status = '网页错误'
  144. // }
  145. // iframe.onload = function () {
  146. // if (state.status != '固定右上角') {
  147. // state.status = ''
  148. // }
  149. // }
  150. setTimeout(() => {
  151. state.iframe_url = state.detail.convertUrl
  152. state.status = ''
  153. }, 1000)
  154. } else {
  155. state.status = '网页错误'
  156. }
  157. })
  158. }
  159. const clickCancel = () => {
  160. state.show_alert = false
  161. }
  162. const clickFixed = () => {
  163. state.handle_type = '固定'
  164. getChromeStorage('fullCheck', (res) => {
  165. if (res && res.fullCheck) {
  166. // 固定
  167. handleFixed()
  168. } else {
  169. state.show_alert = true
  170. }
  171. })
  172. }
  173. // 固定
  174. const handleFull = () => {
  175. if (state.status || !state.iframe_url) {
  176. return
  177. }
  178. // 切换状态
  179. state.status = '关闭'
  180. // 操作全屏dom
  181. let dom = document.querySelector('#denet-tool-box-fixed')
  182. dom.style.cssText = `
  183. width:100%;
  184. height: 100%;
  185. position: fixed;
  186. right: 0px;
  187. top: 0px;`
  188. dom.style.display = 'block'
  189. state.show_btn = false
  190. sendEventInfo({
  191. event_type: 'ToolBox_To_Fixed',
  192. data: {
  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. // 操作全屏dom
  206. let dom = document.querySelector('#denet-tool-box-fixed')
  207. dom.style.display = 'block'
  208. dom.style.cssText = `
  209. width: 505px;
  210. height: 544px;
  211. position: fixed;
  212. right: 10px;
  213. top: 10px;`
  214. sendEventInfo({
  215. event_type: 'ToolBox_To_Fixed',
  216. data: {
  217. iframe_url: state.iframe_url,
  218. tweetId: state.tweetId
  219. }
  220. })
  221. // 清除当前iframe src
  222. state.iframe_url = ''
  223. }
  224. const clickFull = () => {
  225. state.handle_type = '全屏'
  226. getChromeStorage('fullCheck', (res) => {
  227. if (res && res.fullCheck) {
  228. // 全屏
  229. handleFull()
  230. } else {
  231. state.show_alert = true
  232. }
  233. })
  234. }
  235. </script>
  236. <style lang="scss">
  237. .pre-view {
  238. pointer-events: none;
  239. cursor: default;
  240. }
  241. .denet-toolbox {
  242. width: 100%;
  243. height: 100%;
  244. filter: drop-shadow(0px 4px 20px rgba(0, 0, 0, 0.2));
  245. border-radius: 12px;
  246. overflow: hidden;
  247. position: relative;
  248. .alert {
  249. text-align: center;
  250. position: absolute;
  251. top: 0;
  252. left: 0;
  253. width: 100%;
  254. height: 100%;
  255. .back {
  256. background: #000000;
  257. opacity: 0.8;
  258. position: absolute;
  259. top: 0;
  260. left: 0;
  261. width: 100%;
  262. height: 100%;
  263. cursor: auto;
  264. }
  265. .confirm {
  266. position: absolute;
  267. width: 355px;
  268. height: 180px;
  269. background: #FFFFFF;
  270. border-radius: 20px;
  271. top: 173px;
  272. left: 50%;
  273. margin-left: -180px;
  274. .title {
  275. font-weight: 600;
  276. font-size: 18px;
  277. color: #000000;
  278. margin-bottom: 34px;
  279. }
  280. .check {
  281. color: #899099;
  282. font-weight: 400;
  283. font-size: 14px;
  284. margin: 12px 15px 32px 0;
  285. text-align: right;
  286. align-content: center;
  287. justify-content: flex-end;
  288. display: flex;
  289. line-height: 17px;
  290. input {
  291. margin-right: 8px;
  292. }
  293. label {
  294. line-height: 19px;
  295. }
  296. }
  297. .handle {
  298. display: flex;
  299. justify-content: center;
  300. align-items: center;
  301. div {
  302. font-weight: 600;
  303. font-size: 16px;
  304. width: 156px;
  305. height: 47px;
  306. line-height: 47px;
  307. cursor: pointer;
  308. border-radius: 1000px;
  309. user-select: none;
  310. }
  311. .cancel {
  312. color: #000000;
  313. background: rgba(56, 154, 255, 0.01);
  314. border: 1px solid #E6E6E6;
  315. }
  316. .continue {
  317. background: #1D9BF0;
  318. font-weight: 600;
  319. margin-left: 11px;
  320. color: #FFFFFF;
  321. }
  322. }
  323. }
  324. }
  325. .head {
  326. width: 100%;
  327. height: 40px;
  328. background: #373737;
  329. display: flex;
  330. align-items: center;
  331. justify-content: space-between;
  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. width: 100%;
  360. height: 100%;
  361. }
  362. .state {
  363. text-align: center;
  364. img {
  365. margin-bottom: 14px;
  366. }
  367. .icon-loading {
  368. animation: loading 1s infinite linear;
  369. }
  370. div {
  371. margin-bottom: 40px;
  372. color: #8E8E8E;
  373. text-align: center;
  374. font-weight: 500;
  375. font-size: 22px;
  376. }
  377. }
  378. }
  379. }
  380. @keyframes loading {
  381. from {
  382. transform: rotate(0deg);
  383. }
  384. to {
  385. transform: rotate(360deg);
  386. }
  387. }
  388. </style>