This file contains 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 zelluloza_one_list | |
// @namespace https://zelluloza.ru/grub | |
// @description Books in one list | |
// @include https://zelluloza.ru/* | |
// @include http://zelluloza.ru/* | |
// @version 0.3.5 | |
// @run-at document-end | |
// ==/UserScript== | |
var isStoped = false; | |
/* | |
* copy book window | |
*/ | |
var bookBlock = document.createElement('div'); | |
bookBlock.style.width = '800px'; | |
bookBlock.style.height = '200px'; | |
bookBlock.contentEditable = 'true'; | |
bookBlock.style.overflow = 'scroll'; | |
bookBlock.style.resize = 'both'; | |
bookBlock.hidden = true; | |
bookBlock.style.border = '2px solid white'; | |
/* | |
* copy book menu | |
*/ | |
var copyMenu = document.createElement('div'); | |
copyMenu.style.position = 'fixed'; | |
copyMenu.style.zIndex = 999; | |
copyMenu.style.display = 'block !important'; | |
copyMenu.style.border = '4px solid yellow'; | |
copyMenu.style.backgroundColor = "black"; | |
copyMenu.style.color = 'white'; | |
copyMenu.style.width = '100%'; | |
copyMenu.style.bottom = '0px'; | |
copyMenu.id = 'oneListMenu'; | |
/* | |
* | |
*/ | |
var notify = document.createElement('div'); | |
notify.innerHTML = 'Перейдите на страничку, где отображаются страницы книги. И нажмите "Run".'; | |
notify.style.fontWeight = 'bolder'; | |
/* | |
* open in new tab | |
*/ | |
var newTabBut = document.createElement('button'); | |
newTabBut.innerHTML = 'Open in new tab'; | |
newTabBut.onclick = function() { | |
var bookWin = window.open("about:blank", "book"); | |
bookWin.onload = function() { | |
bookWin.document.title = "CopyBook"; | |
var pages = bookBlock.querySelectorAll('*'); | |
var nWBody = bookWin.document.body; | |
for(var i = 0; i < pages.length; i++) { | |
var pageClone = pages[i].cloneNode(true); | |
pageClone.style.display = 'block'; | |
nWBody.appendChild(pageClone); | |
} | |
}; | |
}; | |
/* | |
* flip button | |
*/ | |
var flipButton = document.createElement('button'); | |
flipButton.innerHTML = 'Flip copy'; | |
flipButton.onclick = function() { | |
flipBookBlock(); | |
}; | |
/* | |
* clear button | |
*/ | |
var clearButton = document.createElement('button'); | |
clearButton.innerHTML = 'Clear'; | |
clearButton.onclick = function() { | |
clearCopyBook(); | |
}; | |
/* | |
* input field for dellay | |
*/ | |
var dellayInput = document.createElement('input'); | |
var dellayField = document.createElement('span'); | |
var dellayName = document.createElement('span'); | |
dellayName.innerHTML = 'dellay: '; | |
dellayField.appendChild(dellayName); | |
dellayField.appendChild(dellayInput); | |
dellayInput.type = 'number'; | |
dellayInput.defaultValue = 1500; | |
/* | |
* run button | |
*/ | |
var runButton = document.createElement('button'); | |
runButton.innerHTML = 'Run'; | |
runButton.id = 'oneListButton'; | |
runButton.onclick = function() { | |
clearCopyBook(); | |
setTimeout(function() { inBookBegin() }, 2000); | |
setTimeout(function() { copyBook(findBookPageImg(), 0) }, 4000); | |
isStoped = false; | |
//newTabBut.disabled = true; | |
changeStatus(statusVal.running); | |
}; | |
/* | |
* interrupt button | |
*/ | |
var stopButton = document.createElement('button'); | |
stopButton.innerHTML = 'Stop'; | |
stopButton.onclick = function() { | |
isStoped = true; | |
}; | |
/* | |
* status | |
*/ | |
var statusBar = document.createElement('span'); | |
var statusName = 'status: '; | |
var statusVal = { | |
'stopped' : 'stopped', | |
'running' : 'running', | |
'done' : 'done' | |
}; | |
statusBar.style.color = 'orange'; | |
statusBar.style.border = '3px solid grey'; | |
var progressBar = document.createElement('span'); | |
var progressBarName = 'progress: '; | |
progressBar.style.color = 'orange'; | |
progressBar.style.border = '3px solid grey'; | |
setProgress('none'); | |
/* | |
* flip | |
*/ | |
function flipBookBlock() { | |
bookBlock.hidden = !bookBlock.hidden; | |
} | |
function createControlMenu() { | |
copyMenu.appendChild(bookBlock); | |
copyMenu.appendChild(runButton); | |
copyMenu.appendChild(stopButton); | |
copyMenu.appendChild(flipButton); | |
copyMenu.appendChild(clearButton); | |
copyMenu.appendChild(newTabBut); | |
copyMenu.appendChild(dellayField); | |
copyMenu.appendChild(statusBar); | |
copyMenu.appendChild(progressBar); | |
copyMenu.appendChild(notify); | |
addElemToBody(copyMenu); | |
changeStatus(statusVal.stopped); | |
} | |
function getBookProgress() { | |
return document.getElementById('bookpg').innerHTML; | |
} | |
function changeStatus(status) { | |
statusBar.innerHTML = statusName + status; | |
} | |
function setProgress(progress) { | |
progressBar.innerHTML = progressBarName + progress; | |
} | |
function addElemToBody(newElem) { | |
var bodyTag = document.getElementsByTagName('body')[0]; | |
var bodyTagChilds = bodyTag.getElementsByTagName('*'); | |
bodyTag.insertBefore(newElem, bodyTagChilds[0]); | |
} | |
function getBookCanvas() { | |
var pagesCanvas = document.getElementsByTagName('canvas')[0]; | |
console.log('get canvas id: ' + pagesCanvas.id); | |
return pagesCanvas; | |
} | |
function getImgFromCanvas(bookCanvas) { | |
var img = new Image(); | |
img.src = bookCanvas.toDataURL(); | |
return img; | |
} | |
function toNextPageDown() { | |
//var rightButton = document.getElementsByClassName('boxb2')[1]; | |
//rightButton.click(); | |
bpage(1, 1); | |
console.log('toNextPageDown'); | |
} | |
function rexp100Perc(str) { | |
var re = /100\%.*/; | |
console.log('100 perc is ' + re.test(str) ); | |
return re.test(str); | |
} | |
function lastPage() { | |
var progressText = getBookProgress(); | |
var arr = progressText.split('/'); | |
var leftPageNum = +arr[0].split('-')[1]; | |
var rightPageNum = +arr[1]; | |
console.log('leftPageNum ' + leftPageNum); | |
console.log('rightPageNum ' + rightPageNum); | |
return leftPageNum === rightPageNum; | |
} | |
function isPageEnd() { | |
var progressText = document.getElementById('bookpg').innerHTML; | |
var isEnd = rexp100Perc(progressText); | |
console.log('is end ' + isEnd); | |
return isEnd; | |
} | |
function clearCopyBook() { | |
bookBlock.innerHTML = ""; | |
} | |
function inBookBegin() { | |
var beginButton = document.getElementsByClassName('boxb3')[0]; | |
beginButton.click(); | |
} | |
function copyBook(img, i) { | |
if(isStoped) { | |
changeStatus(statusVal.stopped); | |
return; | |
} | |
setProgress(getBookProgress()); | |
copyPage(img, i); | |
console.log('page numb: ' + i); | |
if( !lastPage() ) { | |
setTimeout(function() { | |
toNextPageDown(); | |
i++; | |
copyBook(findBookPageImg(), i); | |
}, dellayInput.value); | |
} else { | |
setTimeout(function() { | |
changeStatus(statusVal.done); | |
newTabBut.disabled = false; | |
}, 2100); | |
} | |
} | |
function copyPage(img, i) { | |
//cloneNode(true) | |
var image = img; | |
image.id = i | |
printPage(image); | |
} | |
function printPage(page) { | |
bookBlock.appendChild(page); | |
} | |
function getBookWindowImg() { | |
var imgs = document.querySelectorAll('#cnvwrapper img'); | |
var page; | |
for(var i = 0; i < imgs.length; i++) { | |
if(imgs[i].style.display !== 'none') { | |
page = imgs[i]; | |
break; | |
} | |
} | |
console.log('getBookWindowImg'); | |
return page; | |
} | |
function findBookPageImg() { | |
var cns = getBookCanvas(); | |
var cnsDispl = cns.style.display; | |
if(cnsDispl == 'inline' || cnsDispl == 'block' || !!!cnsDispl) { | |
console.log('get img from canvas'); | |
return getImgFromCanvas(cns); | |
} | |
var pageWindowImg = getBookWindowImg(); | |
var imdDispl = pageWindowImg.style.display; | |
if(imdDispl == 'inline' || imdDispl == 'block') { | |
console.log('get img from imgs'); | |
return pageWindowImg.cloneNode(true); | |
} | |
notify.innerHTML = 'ERROR! Book window not found.'; | |
} | |
function testBlock() { | |
} | |
window.onload = function() { | |
//testBlock(); | |
//findBookPage(); | |
createControlMenu(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment