Created
October 26, 2019 19:37
-
-
Save sidigi/539cf87c1a786146eb88d43a52d4b5a9 to your computer and use it in GitHub Desktop.
parser
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 New Userscript | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match http://fssprus.ru/iss/ip* | |
// @grant GM_getResourceText | |
// @resource importFile http://www.sharecsv.com/dl/bc759d3e19f680660a1bdfb721cf13ad/data.csv | |
// ==/UserScript== | |
(function() { | |
"use strict"; | |
const mapData = [ | |
'input[name="is[last_name]"]', | |
'input[name="is[first_name]"]', | |
'input[name="is[patronymic]"]' | |
]; | |
const submitDom = "#btn-sbm"; | |
const rowResultTable = "table.list.border.table tr"; | |
const hasResultsDom = ".results-frame"; | |
const timeout = 5; | |
const downloadFileName = "data"; | |
/***Program consts***/ | |
const startParam = "start"; | |
const localStorageIndex = "_data"; | |
const currentRowStorage = "_cur_row"; | |
/*Programm classes */ | |
const CsvFile = { | |
toArray(allText, delimeter = ";") { | |
var allTextLines = allText.split(/\r\n|\n/); | |
var lines = []; | |
for (var i = 0; i < allTextLines.length - 1; i++) { | |
var data = allTextLines[i].split(delimeter); | |
lines.push(data); | |
} | |
return lines; | |
}, | |
load() { | |
return this.toArray(GM_getResourceText("importFile")) || []; | |
}, | |
download() { | |
var rowsCsv = StorageManager.get(); | |
let csvContent = | |
"data:text/csv;charset=utf-8," + | |
rowsCsv.map(e => e.join(",")).join("\n"); | |
var encodedUri = encodeURI(csvContent); | |
var link = document.createElement("a"); | |
var time = new Date() | |
.toISOString() | |
.slice(0, 19) | |
.replace("T", "_"); | |
link.setAttribute("href", encodedUri); | |
link.setAttribute("download", `${downloadFileName}-${time}.csv`); | |
document.body.appendChild(link); | |
console.info("Downloading file..."); | |
link.click(); | |
} | |
}; | |
const StorageManager = { | |
dataKey: localStorageIndex, | |
incrementKey: currentRowStorage, | |
clear() { | |
localStorage.removeItem(this.dataKey); | |
localStorage.setItem(this.incrementKey, 0); | |
}, | |
get() { | |
return JSON.parse(localStorage.getItem(this.dataKey)) || []; | |
}, | |
set(data) { | |
localStorage.setItem(this.dataKey, JSON.stringify(data)); | |
}, | |
add(data) { | |
this.set([...this.get(), ...data]); | |
} | |
}; | |
const ImportManager = { | |
startUrlParam: startParam, | |
domSubmitBtn: submitDom, | |
domFormControls: mapData, | |
domResults: hasResultsDom, | |
data: [], | |
start() { | |
StorageManager.clear(); | |
console.info("Start..."); | |
alert("Start..."); | |
location.href = UrlWorker.removeParam( | |
window.location, | |
this.startUrlParam | |
); | |
}, | |
finish() { | |
console.info("Finished..."); | |
alert("Finished"); | |
CsvFile.download(); | |
}, | |
step() { | |
return localStorage.getItem(StorageManager.incrementKey) * 1; | |
}, | |
setStep(step) { | |
localStorage.setItem(StorageManager.incrementKey, step); | |
}, | |
stepIncrement() { | |
this.setStep(this.step() + 1); | |
}, | |
isFirstStep() { | |
return this.step() === 0; | |
}, | |
isLastStep() { | |
return this.step() == this.data.length; | |
}, | |
hasNextStep() { | |
return this.data[this.step() + 1] !== "undefined"; | |
}, | |
isBreak() { | |
return !$(this.domResults).length || $("#captcha-popup").length; | |
}, | |
fillData() { | |
console.info("Fill data..."); | |
const step = this.step(); | |
if (this.hasNextStep()) { | |
this.stepIncrement(); | |
} | |
this.fillDataPerHit(this.data[step]); | |
console.info("Sending form..."); | |
$(this.domSubmitBtn).click(); | |
}, | |
fillDataPerHit(data = []) { | |
console.info("For: " + data.join(" ")); | |
data.forEach((item, index) => { | |
$(this.domFormControls[index]).val(item); | |
}); | |
} | |
}; | |
const UrlWorker = { | |
removeParam(uri, param) { | |
let url = new URL(uri); | |
let params = new URLSearchParams(url.search.slice(1)); | |
params.delete(param); | |
return url.origin + url.pathname + "?" + params.toString(); | |
}, | |
hasParam(param) { | |
const url = new URL(window.location); | |
return url.searchParams.get(param) !== null; | |
} | |
}; | |
/*********Program*************/ | |
//If start | |
if (UrlWorker.hasParam(startParam)) { | |
ImportManager.start(); | |
return; | |
} | |
ImportManager.data = CsvFile.load(); | |
if (ImportManager.isFirstStep()) { | |
setTimeout(() => { | |
ImportManager.fillData(); | |
}, 1000); | |
} | |
const interval = setInterval(() => { | |
console.info("Searching results..."); | |
if (ImportManager.isBreak()) { | |
return; | |
} | |
console.info("Find results..."); | |
const rowsCsv = []; | |
$(rowResultTable).each((index, item) => { | |
var row = []; | |
$(item) | |
.find("td") | |
.each((i, td) => row.push($(td).text())); | |
rowsCsv.push(row); | |
}); | |
StorageManager.add(rowsCsv); | |
if (ImportManager.isLastStep()) { | |
clearInterval(interval); | |
ImportManager.finish(); | |
return; | |
} | |
ImportManager.fillData(); | |
}, timeout * 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment