Last active
May 25, 2021 13:57
-
-
Save netwjx/5ee2618c9f09f4992813a86e178d99f3 to your computer and use it in GitHub Desktop.
mtime2douban
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name mtime2douban | |
// @namespace mtime2douban | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match http://my.mtime.com/*/seen | |
// @match http://my.mtime.com/*/wantSee | |
// @match https://search.douban.com/movie/subject_search* | |
// @match https://movie.douban.com/subject/* | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant GM_addStyle | |
// ==/UserScript== | |
(function() { | |
GM_addStyle(` | |
.mtime2douban { | |
position: relative; | |
z-index: 999; | |
top: 35px; | |
font-size: 14px; | |
background: #fff; | |
} | |
.mtime2douban button { | |
padding: 2px 6px; | |
border: 1px solid; | |
} | |
.mtime2douban button:hover { | |
background: #ccc; | |
} | |
.mtime2douban button:focus { | |
background: #999; | |
} | |
`); | |
let ele = $('<div class="mtime2douban"></div>'); | |
let moviesStore = GM_getValue('mtime2douban', []);; | |
let { host } = location; | |
if (~host.indexOf('mtime.com')) { | |
injectMtime(); | |
} else if (~host.indexOf('search.douban.com')) { | |
injectDouban(); | |
} else if (~host.indexOf('movie.douban.com')) { | |
injectDoubanSubject(); | |
} | |
function injectMtime() { | |
$('<button type="button">Save</button>').click(storeMovies).appendTo(ele); | |
$('<button type="button">Show</button>').click(() => { | |
console.table(moviesStore.slice(-30)); | |
console.log(`count: ${moviesStore.length}, page: ${moviesStore.length/10}`) | |
}).appendTo(ele).click(); | |
$('<button type="button">Clear</button>').click(() => { | |
GM_setValue('mtime2douban', []); | |
moviesStore = []; | |
console.log(moviesStore); | |
}).appendTo(ele); | |
inject(); | |
function inject(){ | |
$('.list_cont').prepend(ele)[0] || setTimeout(inject, 100); | |
} | |
let testCount = -1; | |
function storeMovies() { | |
let movies = $('.list_cont h4').map((i, e) => { | |
let [_, name, nameEn, year] = e.innerText.trim().match(/^(.+?)(?: \/ (.+?))?(?: (\d+))?$/); | |
let director = $(e).next().text().slice('导演: '.length); | |
let star = $(e).next().next().text().trim().slice('主演: '.length).trim().replace(/[\r\n]/g, ''); | |
return { | |
name, | |
nameEn, | |
year, | |
director, | |
star, | |
}; | |
}).get(); | |
moviesStore.push(...movies); | |
GM_setValue('mtime2douban', moviesStore); | |
console.log(`${moviesStore.length} ${movies.slice(-1)[0].name}`); | |
let next = $('.btn-next:enabled').click(); | |
if(next[0] && testCount--) { | |
setTimeout(storeMovies, 1200); | |
} | |
} | |
} | |
function injectDouban() { | |
$('.title a').attr('target', '_blank'); | |
let cur = GM_getValue('current', -1); | |
let next = $('<button type="button">Continue</button>').click(e => { | |
go(cur + 1); | |
}).appendTo(ele); | |
$(document).keypress(e => { | |
if(e.key === 'c' && e.target.nodeName !== 'INPUT') { | |
next.click(); | |
} | |
}); | |
$(`<button type="button">Current: ${cur + 1}/${moviesStore.length} </button>`).click(e => { | |
let i = prompt('Go index?'); | |
go(i - 1); | |
}).appendTo(ele); | |
let item = moviesStore[cur]; | |
if(item){ | |
$(`<div>${item.name}<br> ${item.nameEn} ${item.year || ''}<br>${item.director} ${item.star}</div>`).appendTo(ele) | |
} | |
inject(); | |
function inject(){ | |
$('.root').prepend(ele)[0] || setTimeout(inject, 100); | |
} | |
function go(i) { | |
i = parseInt(i, 10); | |
let item = moviesStore[i]; | |
if(item) { | |
GM_setValue('current', i); | |
$('#inp-query').val(item.name).closest('form').find('input[type=submit]').click(); | |
} | |
} | |
} | |
function injectDoubanSubject(){ | |
if(~$('.a_stars').html().indexOf('我的评价')) { | |
return; | |
} | |
autoSave(); | |
function autoSave() { | |
$('#dialog input[value=保存]').click()[0] || setTimeout(autoSave, 100); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment