index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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">
  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. httpContentToBack({
  130. url: `/post/getDetail`,
  131. params: {
  132. postId: state.postId
  133. }
  134. }, (res) => {
  135. if (res && res.code == 0) {
  136. state.detail = JSON.parse(res.data.postBizData)
  137. console.log('postBizData', state.detail)
  138. // 加载iframe
  139. let iframe = dom_iframe.value
  140. // state.detail.originUrl = 'https://www.bilibili.com'
  141. iframe.onerror = () => {
  142. state.status = '网页错误'
  143. }
  144. iframe.onload = function () {
  145. state.status = ''
  146. }
  147. setTimeout(() => {
  148. state.iframe_url = state.detail.originUrl
  149. }, 1000)
  150. } else{
  151. state.status = ''
  152. }
  153. })
  154. }
  155. const clickCancel = () => {
  156. state.show_alert = false
  157. }
  158. const clickFixed = () => {
  159. state.handle_type = '固定'
  160. getChromeStorage('fullCheck', (res) => {
  161. if (res && res.fullCheck) {
  162. // 固定
  163. handleFixed()
  164. } else {
  165. state.show_alert = true
  166. }
  167. })
  168. }
  169. // 固定
  170. const handleFull = () => {
  171. if (state.status || !state.iframe_url) {
  172. return
  173. }
  174. // 切换状态
  175. state.status = '关闭'
  176. // 操作全屏dom
  177. let dom = document.querySelector('#denet-tool-box-fixed')
  178. dom.style.cssText = `
  179. width:100%;
  180. height: 100%;
  181. position: fixed;
  182. right: 0px;
  183. top: 0px;`
  184. dom.style.display = 'block'
  185. state.show_btn = false
  186. sendEventInfo({
  187. event_type: 'ToolBox_To_Fixed',
  188. data: {
  189. iframe_url: state.iframe_url,
  190. tweetId: state.tweetId
  191. }
  192. })
  193. // 清除当前iframe src
  194. state.iframe_url = ''
  195. }
  196. // 全屏
  197. const handleFixed = () => {
  198. // 切换状态
  199. state.show_btn = false
  200. state.status = '固定右上角'
  201. // 操作全屏dom
  202. let dom = document.querySelector('#denet-tool-box-fixed')
  203. dom.style.display = 'block'
  204. dom.style.cssText = `
  205. width: 505px;
  206. height: 544px;
  207. position: fixed;
  208. right: 10px;
  209. top: 10px;`
  210. sendEventInfo({
  211. event_type: 'ToolBox_To_Fixed',
  212. data: {
  213. iframe_url: state.iframe_url,
  214. tweetId: state.tweetId
  215. }
  216. })
  217. // 清除当前iframe src
  218. state.iframe_url = ''
  219. }
  220. const clickFull = () => {
  221. state.handle_type = '全屏'
  222. getChromeStorage('fullCheck', (res) => {
  223. if (res && res.fullCheck) {
  224. // 全屏
  225. handleFull()
  226. } else {
  227. state.show_alert = true
  228. }
  229. })
  230. }
  231. </script>
  232. <style lang="scss">
  233. .pre-view {
  234. pointer-events: none;
  235. cursor: default;
  236. }
  237. .denet-toolbox {
  238. width: 100%;
  239. 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. span {
  329. color: #FFFFFF;
  330. font-style: normal;
  331. font-weight: 500;
  332. font-size: 14px;
  333. margin-left: 16px;
  334. }
  335. img {
  336. width: 20px;
  337. height: 20px;
  338. cursor: pointer;
  339. }
  340. .full {
  341. margin-right: 16px;
  342. }
  343. .fixed {
  344. margin-right: 20px;
  345. }
  346. }
  347. .content {
  348. width: 100%;
  349. height: calc(100% - 40px);
  350. background: #686868;
  351. display: flex;
  352. align-items: center;
  353. justify-content: center;
  354. iframe {
  355. width: 100%;
  356. height: 100%;
  357. }
  358. .state {
  359. text-align: center;
  360. img {
  361. margin-bottom: 14px;
  362. }
  363. .icon-loading {
  364. animation: loading 1s infinite linear;
  365. }
  366. div {
  367. margin-bottom: 40px;
  368. color: #8E8E8E;
  369. text-align: center;
  370. font-weight: 500;
  371. font-size: 22px;
  372. }
  373. }
  374. }
  375. }
  376. @keyframes loading {
  377. from {
  378. transform: rotate(0deg);
  379. }
  380. to {
  381. transform: rotate(360deg);
  382. }
  383. }
  384. </style>