@sugarcube/plugin-google
Version:
A plugin for SugarCube to interact with some google services.
61 lines (46 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.reverseImagesFromFile = exports.images = exports.search = void 0;
var _fp = require("lodash/fp");
var _nightmare = _interopRequireDefault(require("nightmare"));
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
require("nightmare-upload")(_nightmare.default);
require("babel-polyfill");
const browse = (0, _fp.curry)((show, url) => {
const nightmare = (0, _nightmare.default)({
show
});
return nightmare.goto(url);
});
const html = n => n.end().evaluate(() => document.body.innerHTML); // eslint-disable-line no-undef
const scrolledHtml = (0, _fp.curry)((scrollCount, nightmare) => {
// eslint-disable-next-line no-undef
const scrollHeight = () => document.body.scrollHeight; // eslint-disable-next-line func-names
const run = Promise.coroutine(function* () {
for (let i = 0; i < scrollCount; i += 1) {
const height = yield nightmare.evaluate(scrollHeight);
yield nightmare.scrollTo(height, 0).wait(1000);
} // TODO: Not sure why I have to call `html` here, the only way I found.
return html(nightmare);
});
return run();
});
const search = (0, _fp.curry)((url, headless, term) => (0, _fp.flow)([browse(headless), n => n.type("#lst-ib", term).click("input[name='btnK']").wait("div.srg"), html])(url));
exports.search = search;
const images = (0, _fp.curry)((url, scrollCount, headless, term) => (0, _fp.flow)([(0, _utils.urlify)({
tbm: "isch",
q: term
}), browse(headless), scrolledHtml(scrollCount)])(url));
exports.images = images;
const reverseImagesFromFile = (0, _fp.curry)((url, headless, path) => (0, _fp.flow)([browse(headless), n => n.click("a.gsst_a").wait("#qbf a").click("#qbf a").wait("#qbfile").upload("#qbfile", path).wait(5000), // TODO: Maybe there is a better wait condition.
html])(url));
exports.reverseImagesFromFile = reverseImagesFromFile;
var _default = {
search,
images,
reverseImagesFromFile
};
exports.default = _default;