Skip to content

Commit 2d14a5c

Browse files
committed
数種の変更
* privacy -> visibility * インスタンスの種類によって自動でvisibilityが変わるように * Misskeyのvisibility付き投稿に対応
1 parent 44e8c84 commit 2d14a5c

File tree

6 files changed

+144
-109
lines changed

6 files changed

+144
-109
lines changed

Diff for: _locales/en/messages.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
"setting_instance": { "message": "Your instance" },
66
"setting_token": { "message": "Your token(Required permission to write)" },
7-
"setting_privacy": { "message": "Privacy" },
8-
"setting_privacy_public": { "message": "Public" },
9-
"setting_privacy_unlisted": { "message": "Unlisted" },
10-
"setting_privacy_private": { "message": "Private" },
11-
"setting_privacy_limited": { "message": "Limited(Only for implemented instances)" },
12-
"setting_privacy_direct": { "message": "Direct" },
7+
"setting_visibility": { "message": "Privacy" },
8+
9+
"setting_visibility_Mastodon_public": { "message": "Public" },
10+
"setting_visibility_Mastodon_unlisted": { "message": "Unlisted" },
11+
"setting_visibility_Mastodon_private": { "message": "Private" },
12+
"setting_visibility_Mastodon_limited": { "message": "Limited(Only for implemented instances)" },
13+
"setting_visibility_Mastodon_direct": { "message": "Direct" },
14+
15+
"setting_visibility_Misskey_public": { "message": "Public" },
16+
"setting_visibility_Misskey_home": { "message": "Home" },
17+
"setting_visibility_Misskey_followers": { "message": "Followers" },
18+
"setting_visibility_Misskey_specified": { "message": "Direct" },
19+
"setting_visibility_Misskey_private": { "message": "Private" },
20+
1321
"setting_save": { "message": "Save the changes" },
1422
"setting_close": { "message": "Discard the changes" },
1523

Diff for: _locales/ja/messages.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
"setting_instance": { "message": "所属インスタンス" },
66
"setting_token": { "message": "トークン(書き込み権限必須)" },
7-
"setting_privacy": { "message": "公開範囲" },
8-
"setting_privacy_public": { "message": "公開" },
9-
"setting_privacy_unlisted": { "message": "未収載" },
10-
"setting_privacy_private": { "message": "非公開" },
11-
"setting_privacy_limited": { "message": "限定公開(※一部のインスタンスのみ)" },
12-
"setting_privacy_direct": { "message": "ダイレクト" },
7+
"setting_visibility": { "message": "公開範囲" },
8+
9+
"setting_visibility_Mastodon_public": { "message": "公開" },
10+
"setting_visibility_Mastodon_unlisted": { "message": "未収載" },
11+
"setting_visibility_Mastodon_private": { "message": "非公開" },
12+
"setting_visibility_Mastodon_limited": { "message": "限定公開(※一部のインスタンスのみ)" },
13+
"setting_visibility_Mastodon_direct": { "message": "ダイレクト" },
14+
15+
"setting_visibility_Misskey_public": { "message": "公開" },
16+
"setting_visibility_Misskey_home": { "message": "ホーム" },
17+
"setting_visibility_Misskey_followers": { "message": "フォロワー" },
18+
"setting_visibility_Misskey_specified": { "message": "ダイレクト" },
19+
"setting_visibility_Misskey_private": { "message": "非公開" },
20+
1321
"setting_save": { "message": "変更を保存" },
1422
"setting_close": { "message": "変更を破棄" },
1523

Diff for: background.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const URLMatchers = {
2121
* @param {Number} tabId
2222
*/
2323
const notifyListeningInfo = (tabId) => {
24-
chrome.storage.local.get(["enabled", "instance", "token", "privacy"], items => {
25-
const { enabled, instance, token, privacy } = items;
24+
chrome.storage.local.get(["enabled", "instance", "token", "visibility"], items => {
25+
const { enabled, instance, token, visibility } = items;
2626

2727
if (!enabled) return;
2828
if (!instance) throw new TypeError("A config, 'instance' is invalid.");
2929
if (!token) throw new TypeError("A config, 'token' is invalid.");
30-
if (!privacy) throw new TypeError("A config, 'privacy' is invalid.");
30+
if (!visibility) throw new TypeError("A config, 'visibility' is invalid.");
3131

3232

3333

@@ -70,8 +70,8 @@ const notifyListeningInfo = (tabId) => {
7070
* @returns {Promise | void}
7171
*/
7272
const tootListeningInfo = (title, url) => {
73-
chrome.storage.local.get(["type", "instance", "token", "privacy"], items => {
74-
const { type, instance, token, privacy } = items;
73+
chrome.storage.local.get(["type", "instance", "token", "visibility"], items => {
74+
const { type, instance, token, visibility } = items;
7575

7676
const status = [
7777
"#WhatYouarePlaying",
@@ -84,10 +84,10 @@ const tootListeningInfo = (title, url) => {
8484
].join("\n");
8585

8686
switch (type) {
87-
case "NONE":
87+
case "None":
8888
throw new URIError("The instance is not acceptable.");
8989

90-
case "MASTODON":
90+
case "Mastodon":
9191
return fetch(`${instance}/api/v1/statuses`, {
9292
method: "POST",
9393

@@ -98,18 +98,20 @@ const tootListeningInfo = (title, url) => {
9898

9999
body: JSON.stringify({
100100
status,
101-
visibility: privacy
101+
visibility
102102
})
103103
});
104104

105-
case "MISSKEY":
105+
case "Misskey":
106106
return fetch(`${instance}/api/notes/create`, {
107107
method: "POST",
108108
headers: { "Content-Type": "application/json" },
109109

110110
body: JSON.stringify({
111111
i: token,
112-
text: status
112+
113+
text: status,
114+
visibility
113115
})
114116
});
115117
}

Diff for: views/lib/Instance.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
class Instance {
2+
static get Type () {
3+
return {
4+
None: "None",
5+
6+
Mastodon: "Mastodon",
7+
Misskey: "Misskey"
8+
};
9+
}
10+
11+
static get Visibility () {
12+
return {
13+
None: [""],
14+
15+
Mastodon: ["public", "unlisted", "private", "limited", "direct"],
16+
Misskey: ["public", "home", "followers", "specified", "private"]
17+
};
18+
}
19+
20+
/**
21+
* Detects a type of provided instance's url
22+
*
23+
* @param {String} instance
24+
* @return {Promise<String>}
25+
*/
26+
static detectType (instance) {
27+
//Misskey's API
28+
return fetch(`${instance}/api/stats`, {
29+
method: "POST"
30+
}).then(res => {
31+
if (res.ok) return Instance.Type.Misskey;
32+
33+
//Mastodon's API
34+
return fetch(`${instance}/api/v1/instance`).then(res => {
35+
if (res.ok) return Instance.Type.Mastodon;
36+
37+
throw "";
38+
});
39+
}).catch(() => Instance.Type.None);
40+
}
41+
42+
43+
44+
/**
45+
* Setup for handling information of instances
46+
* @param {String} url
47+
*/
48+
constructor (url) {
49+
if (!url) throw new URIError("An argument, 'url' is required.");
50+
51+
//Throws error if provided url isn't absolutely
52+
new URL(url);
53+
54+
this.url = url;
55+
Instance.detectType(url).then(type => this.type = type);
56+
}
57+
58+
/**
59+
* Dispatches provided event when it has been true
60+
*
61+
* @param {"init"} eventname
62+
* @param {Function} callback
63+
*/
64+
on (eventname, callback) {
65+
switch (eventname) {
66+
default:
67+
throw new TypeError(`Provided event, "${eventname}" is not defined`);
68+
69+
case "init":
70+
const detector = setInterval(() => {
71+
if ([this.type].every(prop => prop)) {
72+
clearInterval(detector);
73+
callback(this);
74+
}
75+
});
76+
77+
break;
78+
}
79+
}
80+
}

Diff for: views/settings/index.html

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Link Rel = "StyleSheet" Href = "../lib/Material Icons/material-icons.css" />
1111

1212
<Script Src = "../lib/Locale.js"></Script>
13+
<Script Src = "../lib/Instance.js"></Script>
1314
<Script Src = "settings.js" Defer></Script>
1415
<Link Rel = "StyleSheet" Href = "settings.css" />
1516
</Head>
@@ -50,15 +51,10 @@
5051
</Div>
5152

5253
<Div Class = "input-field">
53-
<Select ID = "privacy">
54-
<Option Name = "privacy.public" Value = "public" Locale-Message = "setting_privacy_public">Public</Option>
55-
<Option Name = "privacy.unlisted" Value = "unlisted" Locale-Message = "setting_privacy_unlisted">Unlisted</Option>
56-
<Option Name = "privacy.private" Value = "private" Locale-Message = "setting_privacy_private">Private</Option>
57-
<Option Name = "privacy.limited" Value = "limited" Locale-Message = "setting_privacy_limited">Limited(Only for implemented instances)</Option>
58-
<Option Name = "privacy.direct" Value = "direct" Locale-Message = "setting_privacy_direct">Direct</Option>
54+
<Select ID = "visibility">
5955
</Select>
6056

61-
<Label Locale-Message = "setting_privacy">Privacy</Label>
57+
<Label Locale-Message = "setting_visibility">Privacy</Label>
6258
</Div>
6359

6460
<Div>

Diff for: views/settings/settings.js

+21-80
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const instanceInputter = document.getElementById("instance");
22
const tokenInputter = document.getElementById("token");
3-
const pricacySelector = document.getElementById("privacy");
3+
const visibilitySelector = document.getElementById("visibility");
44
const enabledSwitch = document.getElementById("enabled");
55
const saveBtn = document.getElementById("btns_save");
66
const closeBtn = document.getElementById("btns_close");
@@ -16,93 +16,34 @@ const throwError = errorKey => {
1616
throw definedMessages[errorKey].message;
1717
};
1818

19-
class Instance {
20-
static get Type () {
21-
return {
22-
None: "NONE",
23-
24-
Mastodon: "MASTODON",
25-
Misskey: "MISSKEY"
26-
};
27-
}
28-
29-
/**
30-
* Detects a type of provided instance's url
31-
*
32-
* @param {String} instance
33-
* @return {Promise<String>}
34-
*/
35-
static detectType (instance) {
36-
//Misskey's API
37-
return fetch(`${instance}/api/stats`, {
38-
method: "POST"
39-
}).then(res => {
40-
if (res.ok) return Instance.Type.Misskey;
41-
42-
//Mastodon's API
43-
return fetch(`${instance}/api/v1/instance`).then(res => {
44-
if (res.ok) return Instance.Type.Mastodon;
45-
46-
throw "";
47-
});
48-
}).catch(() => Instance.Type.None);
49-
}
50-
51-
52-
53-
/**
54-
* Setup for handling information of instances
55-
* @param {String} url
56-
*/
57-
constructor (url) {
58-
if (!url) throw new URIError("An argument, 'url' is required.");
59-
60-
//Throws error if provided url isn't absolutely
61-
new URL(url);
62-
63-
this.url = url;
64-
Instance.detectType(url).then(type => this.type = type);
65-
}
66-
67-
/**
68-
* Dispatches provided event when it has been true
69-
*
70-
* @param {"init"} eventname
71-
* @param {Function} callback
72-
*/
73-
on (eventname, callback) {
74-
switch (eventname) {
75-
default:
76-
throw new TypeError(`Provided event, "${eventname}" is not defined`);
77-
78-
case "init":
79-
const detector = setInterval(() => {
80-
if ([this.type].every(prop => prop)) {
81-
clearInterval(detector);
82-
callback(this);
83-
}
84-
});
85-
86-
break;
87-
}
88-
}
89-
}
90-
9119

9220

9321
window.addEventListener("DOMContentLoaded", () => {
9422
document.querySelectorAll("Select").forEach(select => M.FormSelect.init(select));
9523
});
9624

9725
window.addEventListener("DOMContentLoaded", () => {
98-
chrome.storage.local.get(["enabled", "type", "instance", "token", "privacy"], items => {
99-
if (items.enabled) enabledSwitch.checked = items.enabled;
100-
if (items.instance) instanceInputter.value = items.instance;
101-
if (items.token) tokenInputter.value = items.token;
102-
if (items.privacy) pricacySelector.namedItem(`privacy.${items.privacy}`).selected = true;
26+
chrome.storage.local.get(["enabled", "type", "instance", "token", "visibility"], items => {
27+
const { enabled, type, instance, token, visibility } = items;
28+
29+
if (enabled) enabledSwitch.checked = enabled;
30+
if (instance) instanceInputter.value = instance;
31+
if (token) tokenInputter.value = token;
32+
33+
if (type && type !== Instance.Type.None) {
34+
for (let visibility of Instance.Visibility[type]) {
35+
const option = new Option(definedMessages[`setting_visibility_${type}_${visibility}`].message, visibility);
36+
option.setAttribute("Name", `visibility.${visibility}`);
37+
option.setAttribute("Locale-Message", `setting_visibility_${type}_${visibility}`);
38+
39+
visibilitySelector.add(option);
40+
}
41+
}
42+
43+
if (visibility && Instance.Visibility[type].includes(visibility)) visibilitySelector.namedItem(`visibility.${visibility}`).selected = true;
10344

10445
M.updateTextFields();
105-
M.FormSelect.init(pricacySelector);
46+
M.FormSelect.init(visibilitySelector);
10647
});
10748

10849
enabledSwitch.addEventListener("change", event => {
@@ -124,7 +65,7 @@ window.addEventListener("DOMContentLoaded", () => {
12465
type,
12566
instance: instance.url,
12667
token: tokenInputter.value,
127-
privacy: pricacySelector.value
68+
visibility: visibilitySelector.value
12869
});
12970

13071
window.close();

0 commit comments

Comments
 (0)