Skip to content

Instantly share code, notes, and snippets.

@tlan16
Last active February 1, 2018 13:55
Show Gist options
  • Save tlan16/04c7041a72ebc7e8b1506eff2e913ae2 to your computer and use it in GitHub Desktop.
Save tlan16/04c7041a72ebc7e8b1506eff2e913ae2 to your computer and use it in GitHub Desktop.
extract self-created dictionary from shanbay.com
// ==UserScript==
// @name New ES6-Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description shows how to use babel compiler
// @author You
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @require http://unpkg.com/xlsx/dist/shim.min.js
// @require http://unpkg.com/xlsx/dist/xlsx.full.min.js
// @require http://sheetjs.com/demos/Blob.js
// @require http://sheetjs.com/demos/FileSaver.js
// @match *://*.shanbay.com/*
// ==/UserScript==
/* jshint ignore:start */
var inline_src = (<><![CDATA[
/* jshint ignore:end */
/* jshint esnext: false */
/* jshint esversion: 6 */
// Your code here...
/* jshint ignore:start */
]]></>).toString();
var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
eval(c.code);
/* jshint ignore:end */
console.log('Hellow world');
myDictionary = {};
let getOne = (uri, pageNumber, callback) => {
uri += '?ipp=50&page=' + pageNumber;
let data = getJson(uri, callback);
};
let isLastPage = data => {
let total = data.data.total;
let pageSize = data.data.ipp;
let pageNumber = data.data.page;
return pageSize * pageNumber >= total;
};
let getJson = (uri, callback) => {
var request = new XMLHttpRequest();
if (request.status === 200) {
console.log(request.responseText);
}
request.open('GET', uri, false); // `false` makes the request synchronous
request.send(null);
if (request.status === 200) {
// Success!
let data = JSON.parse(request.responseText);
callback(data);
return data;
} else {
// We reached our target server, but it returned an error
}
};
let record = (type,data) => {
myDictionary[type] = myDictionary[type].concat(data.data.objects);
console.log(myDictionary);
};
for (let type of ['fresh']) {
myDictionary[type] = [];
myDictionary.lastPage = false;
myDictionary.pageNumber = 1;
while (myDictionary.lastPage === false) {
getOne('/api/v1/bdc/library/' + type + '/', myDictionary.pageNumber, data => {
record(type, data);
myDictionary.lastPage = isLastPage(data);
myDictionary.pageNumber++;
});
}
delete myDictionary.lastPage;
delete myDictionary.pageNumber;
}
console.log(myDictionary);
var ws_name = "SheetJS";
var wb = XLSX.utils.book_new(), ws = XLSX.utils.json_to_sheet(myDictionary.fresh);
/* add worksheet to workbook */
XLSX.utils.book_append_sheet(wb, ws, ws_name);
var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'array'});
saveAs(new Blob([wbout],{type:"application/octet-stream"}), "write.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment