index.vue 11 KB

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