adasdasdasdas

This commit is contained in:
2023-12-25 01:49:59 +08:00
parent 29ba707cde
commit 81d90f7a04
14 changed files with 1794 additions and 408 deletions

View File

@@ -14,6 +14,9 @@
</el-upload>
</template>
<script>
import {parseByString } from "bookmark-file-parser"
import axios from "axios";
import Vue from "vue";
export default {
name: "Import",
data() {
@@ -27,73 +30,6 @@ export default {
},
computed: {},
methods: {
getBookmarksStrRootNode(str) {
// 创建iframe
let iframe = document.createElement('iframe')
document.body.appendChild(iframe)
iframe.style.display = 'none'
// 添加书签dom字符串
iframe.contentWindow.document.documentElement.innerHTML = str
// 获取书签树根节点
return iframe.contentWindow.document.querySelector('dl')
},
walkBookmarksTree (root) {
let result = []
// 深度优先遍历
let walk = (node, list) => {
let els = node.children
if (els && els.length > 0) {
for (let i = 0; i < els.length; i++) {
let item = els[i]
// p标签或h3标签直接跳过
if (item.tagName === 'P' || item.tagName === 'H3') {
continue
}
// 文件夹不用创建元素
if (item.tagName === 'DL') {
walk(els[i], list)
} else {// DT节点
let child = null
// 判断是否是文件夹
let children = item.children
let isDir = false
for(let j = 0; j < children.length; j++) {
if (children[j].tagName === 'H3' || children[j].tagName === 'DL') {
isDir = true
}
}
// 文件夹
if (isDir) {
child = {
name: item.tagName === 'DT' ? item.querySelector('h3') ? item.querySelector('h3').innerText : '' : '',
folder: true,
children: []
}
walk(els[i], child.children)
} else {// 书签
let _item = item.querySelector('a')
let icon=$('a').attr('ICON');
console.log(icon);
child = {
name: _item.innerText,
url: _item.href,
icon:icon,
folder: false,
}
}
list.push(child)
}
}
}
}
walk(root, result)
return result
},
analysisBookmarksStr(str) {
let root = this.getBookmarksStrRootNode(str)
let result = this.walkBookmarksTree(root)
console.log(result);
},
handleChange(file) {
let _this=this;
let reader = new FileReader(); //先new 一个读文件的对象 FileReader
@@ -112,11 +48,56 @@ export default {
// let snippets = new TextDecoder('gb2312').decode(ints); //二进制缓存区内容转化成中文(即也就是读取到的内容)
let snippets = new TextDecoder('utf-8').decode(ints); //二进制缓存区内容转化成中文(即也就是读取到的内容)
// console.log("读取的内容如下:");
// console.log(snippets);
_this.analysisBookmarksStr(snippets);
if(snippets){
_this.disposeJson(parseByString(snippets));
}
};
},
disposeJson(json) {
let _this = this;
if (localStorage.getItem('userId')) {
axios({
method: 'post',
url: '/api/UrlAndCate/disposeJson',
params: {
userId:localStorage.getItem('userId'),
},
data:json
}).then(function (res) {
if(res.data.code!==200){
Vue.prototype.$notify.error({
title: '错误',
message: "插入失败!",
offset: 0
});
}else {
Vue.prototype.$notify({
title: '成功',
message: ('i', {style: 'color: teal'}, "插入成功!"),
type: 'success',
offset: 0
});
}
}).catch((error) => {
Vue.prototype.$notify.error({
title: '错误',
message: error,
offset: 0
});
})
} else {
Vue.prototype.$notify.error({
title: '错误',
message: '登录状态失效,请重新登录!',
offset: 0
});
}
},
}
}