Skip to content

Commit

Permalink
alpha dist for luogu
Browse files Browse the repository at this point in the history
  • Loading branch information
swwind committed Jan 18, 2019
1 parent c5fef14 commit e57c385
Show file tree
Hide file tree
Showing 28 changed files with 321 additions and 138 deletions.
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
],
"comma-style": [
"error",
"first"
"last"
],
"complexity": "error",
"computed-property-spacing": [
Expand Down Expand Up @@ -113,7 +113,7 @@
"max-lines": "error",
"max-lines-per-function": [
"error",
80
150
],
"max-nested-callbacks": "error",
"max-params": "error",
Expand Down Expand Up @@ -211,7 +211,7 @@
"no-sync": "error",
"no-tabs": "error",
"no-template-curly-in-string": "error",
"no-ternary": "error",
"no-ternary": "off",
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.vscode/
package-lock.json
yarn.lock
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ language: node_js
node_js:
- node
install:
- npm install
- yarn install
script:
# check grammar
- npm run eslint
- npm run stylelint
- yarn run eslint
- yarn run stylelint
# check if files in `dist/` is up-to-date
- mv dist _dist
- npm run build
- diff dist _dist
- mv dist _dist && yarn run build && diff dist _dist
25 changes: 2 additions & 23 deletions dist/bzoj.user.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/luogu.user.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.0.1",
"description": "Useful user scripts for OIers",
"dependencies": {
"leancloud-storage": "^3.11.1"
},
"devDependencies": {
"@babel/core": "^7.2.2",
Expand All @@ -28,7 +27,8 @@
"typescript-eslint-parser": "^21.0.2",
"webpack": "^4.28.4",
"webpack-cli": "^3.2.1",
"webpack-stream": "^5.2.1"
"webpack-stream": "^5.2.1",
"leancloud-storage": "^3.11.1"
},
"scripts": {
"eslint": "eslint \"src/**/*.ts\"",
Expand Down
2 changes: 1 addition & 1 deletion src/bzoj/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ downloadURL: https://github.com/sxyz-edu/user-scripts/raw/alpha/dist/bzoj.user.j
supportURL: https://github.com/sxyz-edu/user-scripts

# leancloud api
# require: https://cdn1.lncld.net/static/js/3.0.4/av-min.js
require: https://cdn1.lncld.net/static/js/3.0.4/av-min.js
2 changes: 1 addition & 1 deletion src/bzoj/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* - Other: red
*/

import { getProblemList } from './getProblemList';
import getProblemList from './getProblemList';

document.addEventListener('DOMContentLoaded', () => {
if (window.location.pathname !== '/JudgeOnline/userinfo.php') {
Expand Down
4 changes: 2 additions & 2 deletions src/bzoj/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ document.addEventListener('DOMContentLoaded', () => {
}

const elem = [
document.querySelector('title+center')
, document.querySelector('div.content+center')
document.querySelector('title+center'),
document.querySelector('div.content+center')
];
const pid = Number(location.href.split('=')[1]);

Expand Down
10 changes: 6 additions & 4 deletions src/bzoj/getProblemList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const parseProblems = (str: string): number[] => {
return (str.match(pRegex) || []).map((str) => Number(str.slice(2, 4)));
}

export interface PassedList {
interface PassedList {
passedlist: number[];
updateAt: number;
}

export const getProblemList = (uid: string): Promise<PassedList> => {
const getProblemList = (uid: string): Promise<PassedList> => {
return new Promise((resolve, reject) => {
const saved = localStorage.getItem(uid);
if (saved) {
Expand All @@ -36,12 +36,14 @@ export const getProblemList = (uid: string): Promise<PassedList> => {
.then((res) => res.text())
.then((res) => {
const save = {
'passedlist': parseProblems(res)
, 'updateAt': Number(new Date())
'passedlist': parseProblems(res),
'updateAt': Number(new Date())
};
localStorage.setItem(uid, JSON.stringify(save));
resolve(save);
})
.catch(reject);
});
}

export default getProblemList;
2 changes: 1 addition & 1 deletion src/bzoj/mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Display a check mark if you accept this problem
*/

import { getProblemList } from './getProblemList';
import getProblemList from './getProblemList';

document.addEventListener('DOMContentLoaded', () => {
if (window.location.pathname !== '/JudgeOnline/show.php' &&
Expand Down
6 changes: 3 additions & 3 deletions src/bzoj/sameRefer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ const main = (data: string[][]): void => {
e.style.color = 'blue';
if (url1 || url2) {
e.innerHTML = [
'See Also:'
, createLink(url1, res1)
, createLink(url2, res2)
'See Also:',
createLink(url1, res1),
createLink(url2, res2)
].join(' ');
if (ele.parentElement) {
ele.parentElement.insertBefore(e, ele);
Expand Down
2 changes: 0 additions & 2 deletions src/bzoj/star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Star problems
*/

import * as AV from 'leancloud-storage';

/**
* Provides data storage
*/
Expand Down
2 changes: 1 addition & 1 deletion src/luogu/_config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Scripts for Luogu
namespace: https://www.luogu.org/
version: 0.5.0-alpha.2
version: 0.5.0-alpha.3
match:
- https://www.luogu.org/*
- https://www.luogu.com.cn/*
Expand Down
13 changes: 5 additions & 8 deletions src/luogu/autoLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const shouldLoad = (): boolean => {
return scrollTop + 2000 > height && scrollTop + 1800 < height;
}

document.addEventListener('DOMContentLoaded', () => {
export default () => {

if (!(/^\/discuss\/show\/\d+$/).test(window.location.pathname)) {
// this is not a discuss page
Expand All @@ -45,13 +45,12 @@ document.addEventListener('DOMContentLoaded', () => {
.then((res) => res.text())
.then((res) => {
const regex = /<article[\s\S]*?<\/article>/ig;
let result = regex.exec(res);
while (result = regex.exec(res)) {
(res.match(regex) || []).slice(1).forEach((result) => {
loading = false;
const articles = document.querySelectorAll('article');
const last = articles[articles.length - 1];
last.outerHTML += result[0];
}
last.outerHTML += result;
});
})
.catch((err) => {
// some network error...
Expand All @@ -69,6 +68,4 @@ document.addEventListener('DOMContentLoaded', () => {
nav.remove();
}

});

/* eslint no-cond-assign: 0 */
};
10 changes: 5 additions & 5 deletions src/luogu/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ const getProblemList = (uid: string): Promise<ProblemList> => {
const passedlist = parseProblems(parseResult(data[0]));
const triedlist = parseProblems(parseResult(data[2]));
const save = {
passedlist
, triedlist
, 'updateAt': Number(new Date())
passedlist,
triedlist,
'updateAt': Number(new Date())
}
localStorage.setItem(uid, JSON.stringify(save));
resolve(save);
Expand All @@ -87,7 +87,7 @@ const displayNumber = (num: number): void => {
}
}

document.addEventListener('DOMContentLoaded', () => {
export default () => {
if (window.location.pathname === '/space/show') {
const anotherMatch = (/uid=(\d+)/).exec(window.location.href);
if (!anotherMatch) {
Expand Down Expand Up @@ -130,4 +130,4 @@ document.addEventListener('DOMContentLoaded', () => {
displayNumber(num);
});
}
});
};
20 changes: 14 additions & 6 deletions src/luogu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
* entry point
*/

import './style.scss';
import './settings';

import './settings.ts';
import './autoLoad.ts';
import './compare.ts';
import './mark.ts';
import './search.ts';
import autoLoad from './autoLoad';
import compare from './compare';
import mark from './mark';
import search from './search';

const bind = (e: any) => {
document.addEventListener('DOMContentLoaded', <EventListener> e);
}

bind(autoLoad);
bind(compare);
bind(mark);
bind(search);
4 changes: 2 additions & 2 deletions src/luogu/mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const queryForScore = (uid: number, pid: string): Promise<number> => {
});
}

document.addEventListener('DOMContentLoaded', () => {
export default () => {

const pidMatch = (/\/problemnew\/show\/(\w+)/i).exec(window.location.href);
if (!pidMatch) {
Expand Down Expand Up @@ -85,4 +85,4 @@ document.addEventListener('DOMContentLoaded', () => {
waitForLoaded();
});

});
};
5 changes: 2 additions & 3 deletions src/luogu/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ const replaceSearchBar = (): void => {
setTimeout(searchBar, 500);
}

document.addEventListener('DOMContentLoaded', () => {

export default () => {
if (window.location.pathname === '/') {
// is home page
replaceBtn();
}
replaceSearchBar();
});
};
65 changes: 57 additions & 8 deletions src/luogu/settings.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
import ConfigWindow from '../utils/config';
import ControlCSS from '../utils/ccss';

import html from '../utils/html';
import style from './style.useable.scss';


const languages = {
'zh_CN': {
'bgi': '背景图片',
'opc': '卡片透明度',
'thm': '使用特色主题'
}
}

const language = languages.zh_CN;

const configs = [];

configs.push({
'key': 'Background Image URL'
, 'type': 'text'
, 'value': 'https://gallery.swwind.me/2018-12/15.jpg'
'key': language.bgi,
'type': 'text',
'value': 'https://gallery.swwind.me/2018-08/12.jpg'
});
configs.push({
'key': 'Use Custom Theme'
, 'type': 'checkbox'
, 'value': true
'key': language.thm,
'type': 'checkbox',
'value': true
});
configs.push({
'key': language.opc,
'type': 'number',
'value': 0.75
});

const ccss = new ControlCSS(new Map());

new ConfigWindow(configs, (e) => {
console.log(e);
const configWindow = new ConfigWindow(configs, (data) => {
if (data.get(language.thm)) {
style.use();
} else {
style.unuse();
}
ccss.set('--lg-wallpaper', `url(${<string> data.get(language.bgi)})`);
const opacity = <number> data.get(language.opc);
ccss.set('--lg-opacity', opacity.toString());
});

export default () => {
const a = html('a.icon-btn.color-none');
const i = html('i.fas.fa-cog');
a.appendChild(i);
a.addEventListener('click', () => {
configWindow.show();
});

const call = () => {
const nav = document.querySelector('.user-nav');
if (nav) {
const as = nav.querySelectorAll('a.icon-btn');
nav.insertBefore(a, as[1].nextElementSibling);
} else {
setTimeout(call, 500);
}
}
setTimeout(call, 500);
}
File renamed without changes.
31 changes: 31 additions & 0 deletions src/utils/ccss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import html from './html';

/**
* Dynamic control your css global variables
*/
export default class ControlCSS {
map: Map<string, string>;

style: HTMLStyleElement;

constructor (map: Map<string, string>) {
this.map = map;
this.style = <HTMLStyleElement> html('style');
document.addEventListener('DOMContentLoaded', () => {
document.head.appendChild(this.style);
});
this.flush();
}

flush () {
const v = Array.from(this.map).map((a) => a.join(':'))
.join(';');
this.style.innerHTML = `:root{${v}}`;
}

set (type: string, value: string) {
this.map.set(type, value);
this.flush();
}
}

Loading

0 comments on commit e57c385

Please sign in to comment.