editor.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <div class="editor-wrapper">
  3. <div class="top">
  4. <div class="title">
  5. Enter Link to Embed in Tweet
  6. </div>
  7. <div class="search-wrapper">
  8. <input class="input" type="text" v-model="siteUrl" placeholder="Enter link">
  9. <div class="btn" @click="searchHandler()">
  10. <img :src="require('@/assets/svg/icon-tool-box-search-arrow.svg')" />
  11. </div>
  12. </div>
  13. <div class="desc">
  14. <img :src="linkInputDescImage" alt="">
  15. </div>
  16. </div>
  17. <div class="bottom">
  18. <div class="content">
  19. <div class="cate-item history-wrapper" v-if="historyList.length">
  20. <div class="cate">
  21. <img :src="require('@/assets/svg/icon-tool-app-history.svg')" />
  22. </div>
  23. <div class="app-list">
  24. <div class="app" v-for="(app, idx) in historyList" :key="idx" @click="clickHistoryAppHandler(app)">
  25. <div class="img-wrapper">
  26. <img class="img" :class="{ 'small-img': !app.appId }" :src="app.iconPath" :onerror="imgOnError" />
  27. </div>
  28. <div class="name">
  29. {{ app.name }}
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <div class="cate-item" v-for="(item) in appList" :key="item.cateId">
  35. <div class="cate">
  36. <img :src="item.iconPath">
  37. </div>
  38. <div class="app-list">
  39. <div class="app" v-for="(app, idx) in item.apps" :key="idx" @click="clickAppHandler(app)">
  40. <img class="app-img" :src="app.iconPath" alt="">
  41. <div class="name">
  42. {{ app.name }}
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </template>
  51. <script setup>
  52. import { ref, defineProps, defineEmits, onMounted } from "vue";
  53. import axios from 'axios';
  54. import { message } from "ant-design-vue";
  55. import { convertUrl, getAllPostEditorAppData, checkInputUrlInBlacklist } from "@/http/toolBoxApi";
  56. import { setChromeStorage, getChromeStorage } from "@/uilts/chromeExtension"
  57. import { checkURL, debounce } from "@/uilts/help"
  58. const props = defineProps({
  59. linkInputDescImage: {
  60. type: String,
  61. default: '',
  62. },
  63. });
  64. let siteUrl = ref('');
  65. let selectAppGuideData = {};
  66. let openWindowList = [];
  67. let historyList = ref([])
  68. let appList = ref();
  69. const emits = defineEmits(["changeShowCom"]);
  70. const searchHandler = async (_params) => {
  71. let siteTitle = '', favicon = '';
  72. let timer = null;
  73. if (!siteUrl.value) {
  74. return;
  75. }
  76. siteUrl.value = siteUrl.value.trim();
  77. if (!checkURL(siteUrl.value)) {
  78. message.info('Incorrect URL entered');
  79. //提示
  80. return;
  81. }
  82. const loadingHide = message.loading('loading...', 0);
  83. timer = setTimeout(() => {
  84. loadingHide();
  85. message.error('Page loading failed');
  86. }, 1000 * 15);
  87. if(!_params) {
  88. let blackListRes = await checkInputUrlInBlacklist({
  89. params: {
  90. url: siteUrl.value
  91. }
  92. })
  93. if(blackListRes.code == 0) {
  94. if(blackListRes.data) {
  95. loadingHide();
  96. clearTimeout(timer);
  97. message.info('This site is not supported');
  98. return;
  99. }
  100. }
  101. }
  102. let siteRes = await axios.get(siteUrl.value);
  103. let currentApp = {
  104. appId: '',
  105. cateId: '',
  106. createType: '',
  107. defaultUrl: siteUrl.value,
  108. guideData: '',
  109. iconPath: '',
  110. interactType: '',
  111. linkImagePath: '',
  112. name: '',
  113. }
  114. if (siteRes) {
  115. if (siteRes.headers['content-type'].indexOf('text/html') < 0 || siteRes.request.status > 403) {
  116. loadingHide();
  117. message.error('Page loading failed');
  118. return;
  119. }
  120. let urlObj = new URL(siteUrl.value);
  121. if (siteRes.data) {
  122. siteTitle = getTitleByHtmlStr(siteRes.data);
  123. if (!siteTitle) {
  124. siteTitle = urlObj.hostname;
  125. currentApp.defaultTit = siteTitle;
  126. }
  127. currentApp.name = siteTitle;
  128. }
  129. favicon = urlObj.origin + '/favicon.ico';
  130. }
  131. currentApp.iconPath = favicon;
  132. if(_params) {
  133. currentApp = _params;
  134. }
  135. let convertRes = await convertUrl({ params: { originUrl: siteUrl.value } });
  136. let params = { convertUrl: siteUrl.value,
  137. originUrl: siteUrl.value,
  138. appId: currentApp.appId,
  139. linkImagePath: currentApp.linkImagePath,
  140. currentApp };
  141. loadingHide();
  142. clearTimeout(timer);
  143. if (convertRes && convertRes.code == 0) {
  144. let { convertUrl } = convertRes.data || {};
  145. params.convertUrl = convertUrl;
  146. }
  147. emits('changeShowCom', params)
  148. }
  149. const getTitleByHtmlStr = (str = '') => {
  150. let tag_start = '<title>'
  151. let tag_end = '</title>'
  152. let index1 = str.indexOf(tag_start) + tag_start.length;
  153. let index2 = str.indexOf(tag_end);
  154. if (index1 < tag_start.length || index2 < 0) {
  155. return '';
  156. }
  157. return str.substring(index1, index2) || '';
  158. };
  159. const clickHistoryAppHandler = debounce(function(params) {
  160. if (params.appId) {
  161. clickAppHandler(params);
  162. } else {
  163. siteUrl.value = params.defaultUrl;
  164. searchHandler(params);
  165. }
  166. }, 800);
  167. const clickAppHandler = debounce(function(params) {
  168. let { createType, defaultUrl, appId, linkImagePath } = params;
  169. switch (createType) {
  170. case 1:
  171. emits('changeShowCom', { convertUrl: defaultUrl, originUrl: defaultUrl, appId, linkImagePath, currentApp: params })
  172. break;
  173. case 2:
  174. openWindow(params);
  175. break;
  176. }
  177. }, 800);
  178. const openWindow = (params) => {
  179. chrome.windows.getCurrent({},
  180. function (window) {
  181. if (window && window.state == "fullscreen") {
  182. chrome.windows.update(window.id, {
  183. state: 'normal'
  184. }, function () {
  185. setTimeout(() => {
  186. createGuideWindow(params, true);
  187. }, 1000)
  188. })
  189. } else {
  190. createGuideWindow(params);
  191. }
  192. })
  193. };
  194. const createGuideWindow = (params, isUpdate = false) => {
  195. openWindowList = [];
  196. selectAppGuideData = {};
  197. let windowWith = window.screen.width - 500;
  198. let guideUrl = chrome.runtime.getURL('/iframe/tool-box-guide.html');
  199. setChromeStorage({ selectGuideApp : JSON.stringify(params)}, async () => {
  200. let window1 = await chrome.windows.create({
  201. width: windowWith,
  202. type: 'normal',
  203. url: params.defaultUrl,
  204. state: 'normal'
  205. })
  206. openWindowList.push(window1);
  207. let window2 = await chrome.windows.create({
  208. width: 500,
  209. type: 'popup',
  210. url: guideUrl,
  211. left: windowWith,
  212. state: 'normal'
  213. })
  214. openWindowList.push(window2);
  215. setChromeStorage({ guideAppWindowList: JSON.stringify({list: openWindowList})});
  216. })
  217. }
  218. const getAppList = () => {
  219. getAllPostEditorAppData({ params: {} }).then(res => {
  220. if (res.code == 0) {
  221. appList.value = res.data || [];
  222. }
  223. })
  224. }
  225. const onRuntimeMsg = () => {
  226. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  227. switch (req.actionType) {
  228. case 'CONTENT_GUIDE_APPLY_APP':
  229. siteUrl.value = req.data.siteUrl;
  230. searchHandler(req.data.selectGuideApp);
  231. break;
  232. }
  233. })
  234. }
  235. const getHistoryList = async () => {
  236. let { list = [] } = await getChromeStorage('toolBoxAppHistoryData') || {};
  237. historyList.value = list;
  238. };
  239. const imgOnError = (e) => {
  240. let img = e.srcElement;
  241. img.src = require('@/assets/img/icon-default-app-logo.png');
  242. img.onerror = null;
  243. }
  244. onMounted(() => {
  245. getHistoryList();
  246. getAppList();
  247. onRuntimeMsg();
  248. })
  249. </script>
  250. <style lang="scss" scoped>
  251. .editor-wrapper {
  252. width: 100%;
  253. height: 100%;
  254. .top {
  255. padding: 25px 40px 30px 40px;
  256. border-bottom: 0.5px solid #D1D9DD;
  257. box-sizing: border-box;
  258. .title {
  259. font-weight: 500;
  260. font-size: 20px;
  261. }
  262. .search-wrapper {
  263. margin: 20px 0;
  264. border-radius: 8px;
  265. width: 100%;
  266. height: 49px;
  267. display: flex;
  268. align-items: center;
  269. .input {
  270. background: #F1F3F4;
  271. border: none;
  272. outline: none;
  273. font-weight: 400;
  274. font-size: 16px;
  275. color: #636363;
  276. height: 100%;
  277. width: calc(100% - 49px);
  278. padding-left: 20px;
  279. border-bottom-left-radius: 8px;
  280. border-top-left-radius: 8px;
  281. }
  282. .btn {
  283. width: 49px;
  284. height: 49px;
  285. background: #1D9BF0;
  286. border-bottom-right-radius: 8px;
  287. border-top-right-radius: 8px;
  288. display: flex;
  289. align-items: center;
  290. justify-content: center;
  291. cursor: pointer;
  292. }
  293. }
  294. .desc {
  295. img {
  296. width: 315px;
  297. height: 14px;
  298. object-fit: cover;
  299. }
  300. }
  301. }
  302. .bottom {
  303. width: 100%;
  304. height: calc(100% - 185px);
  305. overflow-y: auto;
  306. .content {
  307. width: 100%;
  308. padding: 36px 30px 20px 50px;
  309. box-sizing: border-box;
  310. .history-wrapper {
  311. .app-list {
  312. .img-wrapper {
  313. width: 60px;
  314. height: 60px;
  315. border-radius: 10px;
  316. margin-bottom: 10px;
  317. border: 1px solid #E5E5E5;
  318. box-sizing: border-box;
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. .img {
  323. width: 100%;
  324. height: 100%;
  325. border-radius: 10px;
  326. }
  327. .small-img {
  328. width: 25px;
  329. height: 25px;
  330. }
  331. }
  332. }
  333. }
  334. .cate-item {
  335. min-height: 110px;
  336. display: flex;
  337. margin-bottom: 12px;
  338. .cate {
  339. width: 20px;
  340. height: 110px;
  341. margin-right: 26px;
  342. display: flex;
  343. align-items: center;
  344. margin-top: -10px;
  345. img {
  346. width: 20px;
  347. height: 20px;
  348. }
  349. }
  350. .app-list {
  351. display: flex;
  352. align-content: center;
  353. flex-wrap: wrap;
  354. .app {
  355. display: flex;
  356. flex-direction: column;
  357. justify-content: center;
  358. align-items: center;
  359. width: 110px;
  360. height: 110px;
  361. cursor: pointer;
  362. .app-img {
  363. width: 60px;
  364. height: 60px;
  365. border-radius: 10px;
  366. margin-bottom: 10px;
  367. border: 1px solid #E5E5E5;
  368. box-sizing: border-box;
  369. }
  370. .name {
  371. font-weight: 500;
  372. font-size: 12px;
  373. color: #636363;
  374. width: 100%;
  375. height: 15px;
  376. text-overflow: ellipsis;
  377. white-space: nowrap;
  378. overflow: hidden;
  379. text-align: center;
  380. box-sizing: border-box;
  381. padding: 0 5px;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. </style>