Skip to content

Commit

Permalink
WIP 通知元を含めるように
Browse files Browse the repository at this point in the history
  • Loading branch information
syusui-s committed Jun 2, 2018
1 parent bd1ce86 commit 829b174
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions YouTubeCommentNotifier.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class Message {
* @param {string} iconUrl 投稿者のアイコン
* @param {?string} badgeType 投稿者のバッジ
* @param {string} body メッセージの本体
* @param {string} source メッセージの流れている放送を表す文字列(放送名など)
*/
constructor(author, iconUrl, badgeType, body) {
Object.assign(this, { author, iconUrl, badgeType, body, });
constructor(author, iconUrl, badgeType, body, source) {
Object.assign(this, { author, iconUrl, badgeType, body, source, });
}

/**
Expand Down Expand Up @@ -98,27 +99,30 @@ class NotificatonMessage {
message.author,
message.iconUrl,
message.body,
message.source,
);
}

/**
* @param {string} title 通知のタイトル
* @param {string} iconUrl 通知のアイコン
* @param {string} body 通知の本文
* @param {string} body 通知を行った配信
*/
constructor(title, iconUrl, body) {
Object.assign(this, { title, iconUrl, body });
constructor(title, iconUrl, body, source) {
Object.assign(this, { title, iconUrl, body, source });
}

/**
* Notification APIを用いて、通知する
*/
async notify() {
notify() {
// TODO 直接依存しない形にする
return new window.Notification(this.title || '', {
icon: this.iconUrl,
body: this.body,
});
return new window.Notification(
`${this.title || ''} (@${this.source})`, {
icon: this.iconUrl,
body: this.body,
});
}
}

Expand All @@ -128,10 +132,11 @@ class NotificatonMessage {
class NotificationService {
/**
* @param {object} notifySound 通知音を鳴らしてくれるような仕組みを持つオブジェクト
* @param {function} selectWindow ウィンドウを選択する函数
* @param {array<RegExp>} authorNamePatterns 通知したいメッセージの著者の名前にマッチするパターンの配列
*/
constructor(notifySound, authorNamePatterns) {
Object.assign(this, { notifySound, authorNamePatterns });
constructor(notifySound, selectWindow, authorNamePatterns) {
Object.assign(this, { notifySound, selectWindow, authorNamePatterns });
}

/**
Expand All @@ -141,10 +146,14 @@ class NotificationService {
*/
notify(message) {
if (message.isModerator() || message.matchNameSome(this.authorNamePatterns)) {
NotificatonMessage
const notification = NotificatonMessage
.fromMessage(message)
.notify();

notification.addEventListener('click', () => {
this.selectWindow();
});

this.notifySound.play();
}
}
Expand All @@ -169,12 +178,7 @@ class NotificationService {
async requestPermission() {
const result = await Notification.requestPermission();

if (result === 'granted') {
return true;
}

window.alert('Notification APIで通知の許可がありません。通知を受け取るには、通知を許可してください。');
return false;
return result === 'granted';
}
}

Expand All @@ -189,15 +193,17 @@ async function main() {
/^(Uka's room| Airi| |Zombi-Ko Channel|Yuhi Riri Official||||Roboco Ch\. - |Kanae Channel|| \[Ω Sisters\]|VTuber|Akabane Channel|YouTuber|MeguRoom|Gilzaren III Season1||Games|||DeepWebUnderground|Hibiki Ao|| |YouTube|||||!||mituki neu|Tsunohane Akagi Vtube|\*channel|| ||||Kanata Hikari \/ LYTOYoutuber|Hoonie friends|| \/ youtuber|VR|DD||-||||使||YouTuber|| Shirokenyoutuber|\/ ODDAI|Kuzuha Channel||| |Channel|Channel||icoch|poemcore tokyo|DOLL GAL millna|Channel|YouTuber|| |youtuber| naisen channel| Tenjin Kotone|-spite-||youtuber|TV -Carnivorous Plants videos-|CHANNEL|Arcadia L\.E\. Project||\/| \/ YouTuber|\/VTuber||Reratan| Channel|Channel|\/YouTuber|\/VR|Mel Channel |φ||2\.5&|||Kite Channel|!!! )$/,
/^(Kimino Yumeka Official|||Sophia|AI ||RAY WAKANA||MIAL||| \/ G3Games|\.\/DotChannel\.|||||YouTuber|||| )$/
];
const notificationService = new NotificationService(notifySound, regexps);

const selectWindow = () => window.focus();
const notificationService = new NotificationService(notifySound, selectWindow, regexps);

if (notificationService.notSupported()) {
window.console.error('Notification がサポートされていません');
window.alert('ブラウザが通知機能にされていません');
return;
}

if (! await notificationService.requestPermission()) {
window.console.error('Notificatonの権限がありません');
window.alert('通知の許可がありません。通知を受け取るには、通知を許可してください。');
return;
}

Expand All @@ -215,6 +221,8 @@ async function main() {
if (! chatItemList)
return;

const title = document.querySelector('#container > h1').textContent;

const toMessage = chatItem => {
const nameElem = chatItem.querySelector('#author-name');
const iconElem = chatItem.querySelector('yt-img-shadow > img#img');
Expand All @@ -226,7 +234,7 @@ async function main() {
const body = bodyElem.textContent;
const badgeType = nameElem.getAttribute('type');

return new Message(name, iconUrl, badgeType, body);
return new Message(name, iconUrl, badgeType, body, title);
}
};

Expand Down

0 comments on commit 829b174

Please sign in to comment.