Skip to content

Commit

Permalink
Use type safety for Danbooru variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Apr 3, 2020
1 parent 38e3d7a commit e959475
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 33 deletions.
14 changes: 10 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
],
"overrides": [
{
"files": "src/js/components/api/responses/*.ts",
"files": [
"src/js/components/api/responses/*.ts",
"src/js/components/api/Danbooru.ts"
],
"rules": {
"camelcase": "off"
"camelcase": "off",
"@typescript-eslint/camelcase": "off"
}
}
],
Expand All @@ -41,8 +45,10 @@
"error",
{
"allow": [
"^GM_"
]
"^GM_",
"^Danbooru"
],
"properties": "never"
}
],
"@typescript-eslint/consistent-type-assertions": "error",
Expand Down
30 changes: 30 additions & 0 deletions src/js/components/api/Danbooru.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface Dabooru {
notice(input: string): void;
error(input: string): void;
Blacklist: DanbooruBlacklist;
Post: DanbooruPost;
Note: DanbooruNote;
Utility: DanbooruUtility;
}

interface DanbooruBlacklist {
apply(): void;
initialize_anonymous_blacklist(): void;
initialize_all(): void;
}

interface DanbooruPost {
vote(postid: number, scoreDifference: number): void;
}

interface DanbooruNote {
Box: DanbooruNoteBox;
}

interface DanbooruNoteBox {
scale_all(): void;
}

interface DanbooruUtility {
disableShortcuts: boolean;
}
4 changes: 2 additions & 2 deletions src/js/components/data/Hotkeys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare const Danbooru;
import { Util } from "../structure/Util";

const validKeys = [
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", ".", ",",
Expand All @@ -17,7 +17,7 @@ export class Hotkeys {
private listeners: string[] = [];

private constructor() {
Danbooru.Utility.disableShortcuts = true;
Util.Danbooru.Utility.disableShortcuts = true;
}

private static getInstance(): Hotkeys {
Expand Down
4 changes: 4 additions & 0 deletions src/js/components/structure/Util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Dabooru } from "../api/Danbooru";

declare const GM_xmlhttpRequest;
declare const Danbooru;

/**
* Common utilities used in other modules
*/
export class Util {

public static Danbooru: Dabooru = Danbooru;

/**
* Converts time from absolute format to relative (i.e. "5 minutes ago")
Expand Down
4 changes: 2 additions & 2 deletions src/js/modules/general/Miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ export class Miscellaneous extends RE6Module {
if (selection === "") {
const jsonData: ApiForumPost = await Api.getJson(requestURL);
strippedBody = jsonData.body.replace(/\[quote\](?:.|\n|\r)+?\[\/quote\][\n\r]*/gm, "");
strippedBody = `[quote]"` + $parent.data('creator') + `":/user/show/` + $parent.data('creator-id') + `said:\n` + strippedBody + `\n[/quote]`;
strippedBody = `[quote]"` + $parent.data('creator') + `":/user/show/` + $parent.data('creator-id') + ` said:\n` + strippedBody + `\n[/quote]`;
} else {
strippedBody = `[quote]"` + $parent.data('creator') + `":/user/show/` + $parent.data('creator-id') + `said:\n` + selection + `\n[/quote]`;
strippedBody = `[quote]"` + $parent.data('creator') + `":/user/show/` + $parent.data('creator-id') + ` said:\n` + selection + `\n[/quote]`;
}

if (($textarea.val() + "").length > 0) { strippedBody = "\n\n" + strippedBody; }
Expand Down
7 changes: 3 additions & 4 deletions src/js/modules/post/ImageScaler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Post } from "../../components/data/Post";
import { Form } from "../../components/structure/Form";
import { PageDefintion } from "../../components/data/Page";
import { ModuleController } from "../../components/ModuleController";

declare const Danbooru;
import { Util } from "../../components/structure/Util";

const IMAGE_SIZES = [
{ value: "sample", name: "Sample" },
Expand Down Expand Up @@ -118,7 +117,7 @@ export class ImageScaler extends RE6Module {
this.image.parent().addClass("loading");

this.image.on("load", () => {
Danbooru.Note.Box.scale_all();
Util.Danbooru.Note.Box.scale_all();
this.image.parent().removeClass("loading");
});

Expand Down Expand Up @@ -149,7 +148,7 @@ export class ImageScaler extends RE6Module {
}
}

Danbooru.Note.Box.scale_all();
Util.Danbooru.Note.Box.scale_all();
}

}
7 changes: 3 additions & 4 deletions src/js/modules/post/PostViewer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Post, ViewingPost } from "../../components/data/Post";
import { RE6Module, Settings } from "../../components/RE6Module";
import { PageDefintion } from "../../components/data/Page";

declare const Danbooru;
import { Util } from "../../components/structure/Util";

/**
* Add various symbols to the tilebar depending on the posts state
Expand Down Expand Up @@ -87,12 +86,12 @@ export class PostViewer extends RE6Module {

/** Emulates a click on the upvote button */
private triggerUpvote(): void {
Danbooru.Post.vote(Post.getViewingPost().getId(), 1);
Util.Danbooru.Post.vote(Post.getViewingPost().getId(), 1);
}

/** Emulates a click on the downvote button */
private triggerDownvote(): void {
Danbooru.Post.vote(Post.getViewingPost().getId(), -1);
Util.Danbooru.Post.vote(Post.getViewingPost().getId(), -1);
}

/** Toggles the favorite state */
Expand Down
19 changes: 8 additions & 11 deletions src/js/modules/search/BlacklistEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { PageDefintion } from "../../components/data/Page";
import { Post, ViewingPost } from "../../components/data/Post";
import { User } from "../../components/data/User";
import { PostFilter } from "../../components/data/PostFilter";

/* eslint-disable @typescript-eslint/camelcase */

declare const Danbooru;
import { Util } from "../../components/structure/Util";

/**
* Blacklist Enhancer
Expand Down Expand Up @@ -43,9 +40,9 @@ export class BlacklistEnhancer extends RE6Module {
super.create();

//Override default blacklist function
Danbooru.Blacklist.apply = (): void => { return; };
Danbooru.Blacklist.initialize_anonymous_blacklist = (): void => { return; };
Danbooru.Blacklist.initialize_all = (): void => { return; };
Util.Danbooru.Blacklist.apply = (): void => { return; };
Util.Danbooru.Blacklist.initialize_anonymous_blacklist = (): void => { return; };
Util.Danbooru.Blacklist.initialize_all = (): void => { return; };

this.modifyDOM();

Expand Down Expand Up @@ -125,20 +122,20 @@ export class BlacklistEnhancer extends RE6Module {
* Removes or adds a tag to the users blacklist
*/
private async toggleBlacklistTag(tag): Promise<void> {
Danbooru.notice("Getting current blacklist");
Util.Danbooru.notice("Getting current blacklist");
let currentBlacklist = (await User.getCurrentSettings()).blacklisted_tags.split("\n");

if (currentBlacklist.indexOf(tag) === -1) {
currentBlacklist.push(tag);
User.getInstance().addBlacklistFilter(tag);
Danbooru.notice("Adding " + tag + " to blacklist");
Util.Danbooru.notice("Adding " + tag + " to blacklist");
} else {
currentBlacklist = currentBlacklist.filter(e => e !== tag);
User.getInstance().removeBlacklistFilter(tag);
Danbooru.notice("Removing " + tag + " from blacklist");
Util.Danbooru.notice("Removing " + tag + " from blacklist");
}
await User.setSettings({ blacklisted_tags: currentBlacklist.join("\n") });
Danbooru.notice("Done!");
Util.Danbooru.notice("Done!");
this.applyBlacklist();
}

Expand Down
6 changes: 2 additions & 4 deletions src/js/modules/subscriptions/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { Util } from "../../components/structure/Util";
import { ModuleController } from "../../components/ModuleController";
import { DomUtilities } from "../../components/structure/DomUtilities";

declare const Danbooru;

export class SubscriptionManager extends RE6Module {

//should notifications be cleared once seen?
Expand Down Expand Up @@ -287,11 +285,11 @@ export class SubscriptionManager extends RE6Module {
if (currentlySubbed) {
delete cache[timestamps[i]][updateTimestamp];
delete data[update.id];
Danbooru.notice("Successfully unsubbed!");
Util.Danbooru.notice("Successfully unsubbed!");
} else {
cache[timestamps[i]][updateTimestamp] = update;
data[update.id] = {};
Danbooru.notice("Successfully resubbed!");
Util.Danbooru.notice("Successfully resubbed!");
}
sub.instance.pushSettings("cache", cache);
sub.instance.pushSettings("data", data);
Expand Down
2 changes: 0 additions & 2 deletions src/js/modules/upload/TinyAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { ApiTag } from "../../components/api/responses/ApiTag";
import { Modal } from "../../components/structure/Modal";
import { AvoidPosting } from "../../components/data/AvoidPosting";

declare const Danbooru;

export class TinyAlias extends RE6Module {

private $textarea: JQuery<HTMLElement>;
Expand Down

0 comments on commit e959475

Please sign in to comment.