index.vue 11 KB

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