Skip to content

Commit

Permalink
feat(scripts): add script package
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Feb 10, 2022
1 parent fe60df6 commit 6f03fb4
Show file tree
Hide file tree
Showing 28 changed files with 986 additions and 3 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 3.0.0-beta (2022-02-09)


### Bug Fixes

* **package.json:** fix dependency security ([948662a](https://github.com/enncy/online-course-script/commit/948662a580aab60a3ff70c64111aa36c40b5a4ce))


### Features

* **app and web:** add elctron-builder in app , init page view in web ([fe60df6](https://github.com/enncy/online-course-script/commit/fe60df65dfeed1607ab89a941fc6b6eb627132fc))
* **init:** init project ([0675ac6](https://github.com/enncy/online-course-script/commit/0675ac6a631e8946a52e7e4e655b28faee8248d4))
* **packages:** init packages : web app scripts ([24e5386](https://github.com/enncy/online-course-script/commit/24e5386ec86dec33cc696fda6b5956785e2c1359))



2 changes: 1 addition & 1 deletion packages/app/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, BrowserWindow, Menu, ipcMain } = require("electron");
const { app, BrowserWindow, ipcMain } = require("electron");
const Store = require("electron-store");

app.commandLine.appendSwitch("enable-webgl");
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions packages/scripts/lib/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./qa"), exports);
6 changes: 6 additions & 0 deletions packages/scripts/lib/src/qa/handler/completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.completionHandler = void 0;
function completionHandler(answers, target) {
}
exports.completionHandler = completionHandler;
39 changes: 39 additions & 0 deletions packages/scripts/lib/src/qa/handler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOptionSimilarly = void 0;
var string_similarity_1 = __importDefault(require("string-similarity"));
// export async function handleQuestion(container: string | Element, answer: AnswerProvider, question: QuestionProvider) {
// const element = fromElementLike(container);
// const type = question.type(element);
// const title = question.title(element).textContent;
// if (title) {
// const options = question.options(element);
// const answers = await answer({ title, options: options.map((option) => option.textContent || ""), type });
// if (type === "single_choice") {
// return singleChoiceHandler(answers, options);
// }
// if (type === 'multiple_choice') {
// return multipleChoiceHandler(answers, options);
// }
// if (type === 'judgment') {
// return judgmentHandler(answers, options);
// }
// if (type === 'completion') {
// return singleChoiceHandler(answers, options);
// }
// }
// }
function getOptionSimilarly(answers, options) {
return options.map(function (option) {
if (option.textContent) {
return string_similarity_1.default.findBestMatch(option.textContent, answers).bestMatch.rating;
}
else {
return 0;
}
});
}
exports.getOptionSimilarly = getOptionSimilarly;
24 changes: 24 additions & 0 deletions packages/scripts/lib/src/qa/handler/judgment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.judgmentHandler = exports.startWithRight = exports.wrong = exports.right = void 0;
var similarity = require("string-similarity");
exports.right = "是|对|正确|√|对的|是的|正确的|true|yes|YES|Yes";
exports.wrong = "否|错|错误|x|错的|不正确的|不正确|不是|不是的|false|no|NO|No";
exports.startWithRight = true;
function judgmentHandler(answers, options) {
var target = similarity.findBestMatch(answers[0], exports.right.split("|").concat(exports.wrong.split("|"))).bestMatch.target;
var index = 1;
// 开始选择
if (exports.startWithRight) {
if (RegExp(exports.right).test(target)) {
index = 0;
}
}
else {
if (RegExp(exports.wrong).test(target)) {
index = 0;
}
}
options[index].click();
}
exports.judgmentHandler = judgmentHandler;
13 changes: 13 additions & 0 deletions packages/scripts/lib/src/qa/handler/multiple.choice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.multipleChoiceHandler = void 0;
var _1 = require(".");
function multipleChoiceHandler(answers, options) {
var ratings = (0, _1.getOptionSimilarly)(answers, options);
for (var i = 0; i < ratings.length; i++) {
if (ratings[i] > 0.6) {
options[i].click();
}
}
}
exports.multipleChoiceHandler = multipleChoiceHandler;
10 changes: 10 additions & 0 deletions packages/scripts/lib/src/qa/handler/single.choice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.singleChoiceHandler = void 0;
var _1 = require(".");
function singleChoiceHandler(answers, options) {
var ratings = (0, _1.getOptionSimilarly)(answers, options);
var max = ratings.sort()[ratings.length - 1];
options[ratings.findIndex(function (r) { return r === max; })].click();
}
exports.singleChoiceHandler = singleChoiceHandler;
153 changes: 153 additions & 0 deletions packages/scripts/lib/src/qa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QA = exports.fromElementLike = exports.fromElementArrayLike = exports.questionTypeWords = exports.getQuestionType = exports.getOptions = void 0;
var completion_1 = require("./handler/completion");
var judgment_1 = require("./handler/judgment");
var multiple_choice_1 = require("./handler/multiple.choice");
var single_choice_1 = require("./handler/single.choice");
var question_type_resolver_1 = require("./question.type.resolver");
var option_resolver_1 = require("./option.resolver");
Object.defineProperty(exports, "getOptions", { enumerable: true, get: function () { return option_resolver_1.getOptions; } });
var question_type_resolver_2 = require("./question.type.resolver");
Object.defineProperty(exports, "getQuestionType", { enumerable: true, get: function () { return question_type_resolver_2.getQuestionType; } });
Object.defineProperty(exports, "questionTypeWords", { enumerable: true, get: function () { return question_type_resolver_2.questionTypeWords; } });
function fromElementArrayLike(target, container) {
if (container === void 0) { container = document.body; }
if (typeof target === "string") {
var el = container.querySelectorAll(target);
if (el === null) {
return undefined;
}
else {
return Array.from(el);
}
}
else if (typeof target === "function") {
return Reflect.apply(target, document, [container]);
}
else {
return target;
}
}
exports.fromElementArrayLike = fromElementArrayLike;
function fromElementLike(target, container) {
if (container === void 0) { container = document.body; }
if (typeof target === "string") {
var el = container.querySelector(target);
if (el === null) {
return undefined;
}
else {
return el;
}
}
else if (typeof target === "function") {
return Reflect.apply(target, document, [container]);
}
else {
return target;
}
}
exports.fromElementLike = fromElementLike;
var QA = /** @class */ (function () {
function QA(options) {
this.questions = [];
this.options = Object.assign(QA.default, options);
this.handleQuestionTemplate();
}
/**
* resolve {@link QuestionTemplate} to {@link Question}
*/
QA.prototype.handleQuestionTemplate = function (question) {
var _a, _b, _c, _d, _e, _f, _g;
if (question === void 0) { question = QA.default.question; }
for (var _i = 0, _h = fromElementArrayLike(question.container) || []; _i < _h.length; _i++) {
var container = _h[_i];
var el = container;
var title = fromElementLike(question.title, el);
var type = (0, question_type_resolver_1.getQuestionType)((title === null || title === void 0 ? void 0 : title.textContent) || "") || (0, question_type_resolver_1.getQuestionType)(el);
console.log({
container: container,
title: title,
type: type,
});
if (type) {
var qe = {
container: container,
type: type,
title: fromElementLike(question.title, el),
single: type === "single_choice"
? {
text: fromElementLike((_a = question.single) === null || _a === void 0 ? void 0 : _a.text, el),
target: fromElementLike((_b = question.single) === null || _b === void 0 ? void 0 : _b.target, el),
}
: {},
multiple: type === "multiple_choice"
? {
text: fromElementLike((_c = question.multiple) === null || _c === void 0 ? void 0 : _c.text, el),
target: fromElementLike((_d = question.multiple) === null || _d === void 0 ? void 0 : _d.target, el),
}
: {},
judgment: type === "judgment"
? {
text: fromElementLike((_e = question.judgment) === null || _e === void 0 ? void 0 : _e.text, el),
target: fromElementLike((_f = question.judgment) === null || _f === void 0 ? void 0 : _f.target, el),
}
: {},
completion: type === "completion"
? {
target: fromElementLike((_g = question.completion) === null || _g === void 0 ? void 0 : _g.target, el),
}
: {},
};
this.questions.push(qe);
}
}
};
QA.default = {
type: question_type_resolver_1.getQuestionType,
answer: function () { return ({ question: "", answers: [] }); },
question: {
container: "ul",
title: "div span",
single: {
text: "li",
target: "li input",
handler: single_choice_1.singleChoiceHandler,
},
multiple: {
text: "li",
target: "li input",
handler: multiple_choice_1.multipleChoiceHandler,
},
judgment: {
text: "li",
target: "li input",
isRightInFirst: true,
right: ["是", "对", "正确", "√", "对的", "是的", "正确的", "true", "yes", "YES", "Yes"],
wrong: [
"否",
"错",
"错误",
"x",
"错的",
"不正确的",
"不正确",
"不是",
"不是的",
"false",
"no",
"NO",
"No",
],
handler: judgment_1.judgmentHandler,
},
completion: {
target: "textarea",
handler: completion_1.completionHandler,
},
},
};
return QA;
}());
exports.QA = QA;
15 changes: 15 additions & 0 deletions packages/scripts/lib/src/qa/option.resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOptions = void 0;
var question_type_resolver_1 = require("./question.type.resolver");
/**
* resolve option string of specify element
*/
function getOptions(question, target) {
var options = Array.from(question.querySelectorAll(target));
var questionType = (0, question_type_resolver_1.getQuestionType)(question);
return options.map(function (option) {
return questionType === "completion" ? "" : option.innerText;
});
}
exports.getOptions = getOptions;

0 comments on commit 6f03fb4

Please sign in to comment.