煎蛋收藏
// ==UserScript==
// @name 煎蛋收藏
// @namespace http://jandan.net/
// @version 0.1.1
// @description 收藏煎蛋链接并导出成csv格式
// @author 凉风有信
// @match https://*.jandan.net/*
// ==/UserScript==
(function() {
'use strict';
var list = new Array();
let comment_list = document.querySelectorAll('li[id*="comment-"]');
for(var i = 0; i < comment_list.length; i++) {
let html = comment_list[i].querySelector(".author").innerHTML;
let id = comment_list[i].id.replace('comment-','');
comment_list[i].querySelector(".author").innerHTML = html+`</br><a title="收藏" href="javascript:;" class="addfavorite" data-id="`+id+`">[收藏]</a>`;
let link =comment_list[i].querySelector('.righttext').children[0].getAttribute('href');
comment_list[i].querySelector('.addfavorite').onclick = function (){
love(link);
};
};
let footer = document.querySelector('#footer');
let html = footer.innerHTML;
footer.innerHTML = html + `<br><a class="loveexport" href="javascript:;">导出收藏</a> · <a class="loveclear" href="javascript:;">清空收藏</a>`;
//更新收藏数
updatestatus();
footer.querySelector(".loveexport").onclick = function(){
loveexport();
}
footer.querySelector(".loveclear").onclick = function(){
loveclear();
updatestatus();
}
function love(msg) {
let url = 'https://jandan.net'+msg;
list.push(url)
list = unique(list);
saveurl(list);
};
//去重
function unique(arr){
var x = new Set(arr);
return [...x];
}
//保存到localStorage
function saveurl(list){
let tmp = [];
if(JSON.parse(localStorage.getItem('MyLoveUrl'))){
tmp = JSON.parse(localStorage.getItem('MyLoveUrl'));
}
//console.log(typeof (tmp));
for (let i in list) {
tmp.push(list[i])
}
tmp=unique(tmp)
localStorage.setItem('MyLoveUrl',JSON.stringify(tmp));
updatestatus();
}
//更新收藏数
function updatestatus(){
let len =0;
if(JSON.parse(localStorage.getItem('MyLoveUrl'))){
len = (JSON.parse(localStorage.getItem('MyLoveUrl'))).length;
}else{
len = 0;
}
document.querySelector('#footer').querySelector(".loveexport").innerHTML=`导出收藏[${len}]`;
}
//从localStorage导出
function loveexport(){
let flag = confirm("导出收藏");
if(flag){
if(JSON.parse(localStorage.getItem('MyLoveUrl'))){
let data = (JSON.parse(localStorage.getItem('MyLoveUrl')));
let csvContent = "data:text/csv;charset=utf-8,";
for (let i in data) {
let row = data[i]+",";
csvContent += row + "\r\n";
};
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "JandanFavorite.csv");
document.body.appendChild(link); // Required for FF
link.click();
document.body.removeChild(link);
console.log(csvContent);
}
}
}
//清空localStorage
function loveclear(){
let flag = confirm("确定清空收藏?");
if(flag){
localStorage.clear();
}
}
})();