Browse Source

[edit][getTitleByHtmlStr]

zhangwei 2 years ago
parent
commit
38c0d78503
1 changed files with 62 additions and 61 deletions
  1. 62 61
      src/view/iframe/publish/tool-box/child/editor.vue

+ 62 - 61
src/view/iframe/publish/tool-box/child/editor.vue

@@ -21,10 +21,9 @@
             <img :src="require('@/assets/svg/icon-tool-app-history.svg')" />
           </div>
           <div class="app-list">
-            <div class="app" v-for="(app, idx) in historyList" :key="idx"
-              @click="clickHistoryAppHandler(app)">
+            <div class="app" v-for="(app, idx) in historyList" :key="idx" @click="clickHistoryAppHandler(app)">
               <div class="img-wrapper">
-                <img class="img" :class="{'small-img': !app.appId}" :src="app.iconPath" :onerror="imgOnError" />
+                <img class="img" :class="{ 'small-img': !app.appId }" :src="app.iconPath" :onerror="imgOnError" />
               </div>
               <div class="name">
                 {{ app.name }}
@@ -59,10 +58,10 @@ import { getChromeStorage } from "@/uilts/chromeExtension"
 import { checkURL } from "@/uilts/help"
 
 const props = defineProps({
-    linkInputDescImage: {
-        type: String,
-        default: '',
-    },
+  linkInputDescImage: {
+    type: String,
+    default: '',
+  },
 });
 
 
@@ -81,18 +80,18 @@ const searchHandler = async () => {
   let siteTitle = '', favicon = '';
   let timer = null;
 
-  
-  if(!siteUrl.value) {
+
+  if (!siteUrl.value) {
     return;
   }
 
   siteUrl.value = siteUrl.value.trim();
 
-  if(!checkURL(siteUrl.value)) {
+  if (!checkURL(siteUrl.value)) {
     message.info('Incorrect URL entered');
     //提示
     return;
-  } 
+  }
   const loadingHide = message.loading('loading...', 0);
   timer = setTimeout(() => {
     loadingHide();
@@ -101,21 +100,21 @@ const searchHandler = async () => {
 
   let siteRes = await axios.get(siteUrl.value);
 
-  if(siteRes) {
-    if(siteRes.headers['content-type'].indexOf('text/html') < 0 || siteRes.request.status > 399) {
+  if (siteRes) {
+    if (siteRes.headers['content-type'].indexOf('text/html') < 0 || siteRes.request.status > 399) {
       // 提示
       return;
     }
     let urlObj = new URL(siteUrl.value);
-    if(siteRes.data) {
+    if (siteRes.data) {
       siteTitle = getTitleByHtmlStr(siteRes.data);
-      if(!siteTitle) {
+      if (!siteTitle) {
         siteTitle = urlObj.hostname;
-      } 
+      }
       console.log(siteTitle)
     }
     favicon = urlObj.origin + '/favicon.ico';
-  } 
+  }
 
   let currentApp = {
     appId: '',
@@ -129,23 +128,25 @@ const searchHandler = async () => {
     name: siteTitle,
   }
 
-  let convertRes = await convertUrl({params: {originUrl: siteUrl.value}});
+  let convertRes = await convertUrl({ params: { originUrl: siteUrl.value } });
   let params = { convertUrl: siteUrl.value, originUrl: siteUrl.value, appId: '', currentApp };
 
   loadingHide();
   clearTimeout(timer);
 
-  if(convertRes && convertRes.code == 0) {
-    let {convertUrl} = convertRes.data || {};
+  if (convertRes && convertRes.code == 0) {
+    let { convertUrl } = convertRes.data || {};
     params.convertUrl = convertUrl;
   }
   emits('changeShowCom', params)
 }
 
-const getTitleByHtmlStr = (str) => {
-  let index1 = str.indexOf('<title>') + 7;
-  let index2 = str.indexOf('</title>');
-  if(index2 < 0) {
+const getTitleByHtmlStr = (str = '') => {
+  let tag_start = '<title>'
+  let tag_end = '</title>'
+  let index1 = str.indexOf(tag_start) + tag_start.length;
+  let index2 = str.indexOf(tag_end);
+  if (index1 < 0 || index2 < 0) {
     return '';
   }
 
@@ -153,7 +154,7 @@ const getTitleByHtmlStr = (str) => {
 };
 
 const clickHistoryAppHandler = (params) => {
-  if(params.appId) {
+  if (params.appId) {
     clickAppHandler(params);
   } else {
     siteUrl.value = params.defaultUrl;
@@ -175,11 +176,11 @@ const clickAppHandler = (params) => {
 
 const openWindow = (params) => {
   chrome.windows.getCurrent({},
-    function(window) {
-      if(window && window.state == "fullscreen") {
-        chrome.windows.update(window.id,{
+    function (window) {
+      if (window && window.state == "fullscreen") {
+        chrome.windows.update(window.id, {
           state: 'normal'
-        }, function() {
+        }, function () {
           setTimeout(() => {
             createGuideWindow(params, true);
           }, 1000)
@@ -187,7 +188,7 @@ const openWindow = (params) => {
       } else {
         createGuideWindow(params);
       }
-  })
+    })
 };
 
 const createGuideWindow = (params, isUpdate = false) => {
@@ -215,14 +216,14 @@ const createGuideWindow = (params, isUpdate = false) => {
     openWindowList.push(window);
   })
 
-  if(params.guideData) {
+  if (params.guideData) {
     selectAppGuideData = JSON.parse(params.guideData);
   }
 }
 
 const getAppList = () => {
-  getAllPostEditorAppData({params: {}}).then(res => {
-    if(res.code == 0) {
+  getAllPostEditorAppData({ params: {} }).then(res => {
+    if (res.code == 0) {
       appList.value = res.data || [];
     }
   })
@@ -234,13 +235,13 @@ const onRuntimeMsg = () => {
     sendResponse('ok')
     switch (req.actionType) {
       case 'CONTENT_GET_GUIDE_DATA':
-        chrome.runtime.sendMessage({ 
-            actionType: "CONTENT_EDIT_SEND_GUIDE_DATA", 
-            data: {
-              guideData: selectAppGuideData,
-              windowData: openWindowList
-            }
-        },(response) => {});
+        chrome.runtime.sendMessage({
+          actionType: "CONTENT_EDIT_SEND_GUIDE_DATA",
+          data: {
+            guideData: selectAppGuideData,
+            windowData: openWindowList
+          }
+        }, (response) => { });
         break;
       case 'CONTENT_GUIDE_APPLY_APP':
         siteUrl.value = req.data.siteUrl;
@@ -251,7 +252,7 @@ const onRuntimeMsg = () => {
 }
 
 const getHistoryList = async () => {
-  let {list = []} = await getChromeStorage('toolBoxAppHistoryData') || {};
+  let { list = [] } = await getChromeStorage('toolBoxAppHistoryData') || {};
   historyList.value = list;
 };
 
@@ -340,28 +341,28 @@ onMounted(() => {
       .history-wrapper {
 
         .app-list {
-            .img-wrapper {
-              width: 60px;
-              height: 60px;
+          .img-wrapper {
+            width: 60px;
+            height: 60px;
+            border-radius: 10px;
+            margin-bottom: 10px;
+            border: 1px solid #E5E5E5;
+            box-sizing: border-box;
+            display: flex;
+            align-items: center;
+            justify-content: center;
+
+            .img {
+              width: 100%;
+              height: 100%;
               border-radius: 10px;
-              margin-bottom: 10px;
-              border: 1px solid #E5E5E5;
-              box-sizing: border-box;
-              display: flex;
-              align-items: center;
-              justify-content: center;
-
-              .img {
-                width: 100%;
-                height: 100%;
-                border-radius: 10px;
-              }
-
-              .small-img {
-                width: 25px;
-                height: 25px;
-              }
             }
+
+            .small-img {
+              width: 25px;
+              height: 25px;
+            }
+          }
         }
       }