Skip to content

Commit

Permalink
大規模変更
Browse files Browse the repository at this point in the history
* マニフェスト変更
* ベースシステム変更
  • Loading branch information
GenbuHase committed Jun 2, 2018
1 parent a1bb301 commit 0a130e6
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 166 deletions.
2 changes: 1 addition & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"app_description": { "message": "An extension for Mastodon that toots what you're listening to" },
"app_description": { "message": "An extension of Google Chrome for Mastodon, which toots what you're listening to right now" },
"app_setting": { "message": "Setting" },

"setting_instance": { "message": "Your instance" },
Expand Down
2 changes: 1 addition & 1 deletion _locales/ja/messages.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"app_description": { "message": "Mastodonに再生中の曲をトゥートします" },
"app_description": { "message": "Mastodonに再生中の曲情報をトゥートします" },
"app_setting": { "message": "設定" },

"setting_instance": { "message": "所属インスタンス" },
Expand Down
143 changes: 0 additions & 143 deletions app.js

This file was deleted.

154 changes: 154 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
const URLMatchers = {
YouTube: {
hostSuffix: "youtube.com", pathPrefix: "/watch",
urlMatches: "https://\\w+\.youtube\.com/watch\\?(?:.|&)*v=.+"
},

Niconico: {
hostSuffix: "nicovideo.jp", pathPrefix: "/watch",
urlMatches: "http://\\w+\.nicovideo\.jp/watch/.+"
},

TwitCasting: {
hostEquals: "twitcasting.tv",
urlMatches: "https://twitcasting\.tv/[\\w\\-:]+(/?|/movie/\\d+)$"
}
};



/**
* @param {Number} tabId
*/
const notifyListeningInfo = (tabId) => {
(function looper (tabId) {
chrome.tabs.get(tabId, tabInfo => {
const { status, title, url } = tabInfo;

if (status === "loading") {
setTimeout(looper(tabId), 200);
return;
}

chrome.notifications.create(null, {
type: chrome.notifications.TemplateType.BASIC,

title,
message: url,
iconUrl: "icons/icon48.png"
});

tootListeningInfo(title, url);
});
})(tabId);
};

/**
* @param {String} title
* @param {String} url
*
* @returns {Promise}
*/
const tootListeningInfo = (title, url) => {
chrome.storage.local.get(["enabled", "instance", "token", "privacy"], items => {
const { instance, token, privacy } = items;

if (!instance) throw new TypeError("A config, 'instance' is invalid.");
if (!token) throw new TypeError("A config, 'token' is invalid.");
if (!privacy) throw new TypeError("A config, 'privacy' is invalid.");

return fetch(`${instance}/api/v1/statuses`, {
method: "POST",

headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},

body: JSON.stringify({
status: [
"#WhatYouarePlaying",
"Now playing🎶",
"",
`【${title}】`,
url
].join("\n"),

visibility: privacy
})
});
});
};



chrome.webNavigation.onDOMContentLoaded.addListener(
/**
* @param {Object} details
* @param {Number} details.tabId
* @param {String} details.url
* @param {Number} details.processId
* @param {Number} details.frameId
* @param {Number} details.timeStamp
*/
details => {
notifyListeningInfo(details.tabId);
},

{
url: [
URLMatchers.YouTube,
URLMatchers.Niconico,
URLMatchers.TwitCasting
]
}
);

const recentHistories = [];
chrome.webNavigation.onHistoryStateUpdated.addListener(
/**
* @param {Object} details
* @param {Number} details.tabId
* @param {String} details.url
* @param {Number} details.processId
* @param {Number} details.frameId
* @param {any} details.transitionType
* @param {Array<any>} details.transitionQualifiers
* @param {Number} details.timeStamp
*/
details => {
recentHistories.push(details);

if (recentHistories.length === 3) {
setTimeout(() => notifyListeningInfo(details.tabId), 1000);
recentHistories.splice(0, 3);

return;
}

new Promise(resolve => {
const currentLength = recentHistories.length;
let milliseconds = 0;

let detector = setInterval(() => {
milliseconds++;

if (milliseconds >= 1000) {
clearInterval(detector);
resolve();
}

if (currentLength !== recentHistories.length) clearInterval(detector);
}, 1);
}).then(() => {
notifyListeningInfo(details.tabId);
recentHistories.splice(0, 3);
});
},

{
url: [
URLMatchers.YouTube
]
}
);
45 changes: 31 additions & 14 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,46 @@
"description": "__MSG_app_description__",
"version": "4.0",
"version_name": "v4.0",
"homepage_url": "https://github.com/GenbuHase/WhatYouarePlaying",
"default_locale": "en",

"icons": {
"48": "icon48.png"
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},

"permissions": [
"tabs",
"storage"
],

"content_scripts": [
{
"matches": [ "https://www.youtube.com/*", "http://www.nicovideo.jp/*", "https://twitcasting.tv/*" ],
"js": [ "app.js" ]
}
"notifications",
"storage",
"webNavigation",

"https://*.youtube.com/watch?*",
"http://*.nicovideo.jp/watch/*",
"https://twitcasting.tv/*"
],

"background": {
"scripts": ["background.js"],
"persistent": false
},

"browser_action": {
"default_title": "__MSG_app_setting__ | What You're Playing",
"default_popup": "view/index.html"
"default_icon": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},

"default_title": "Settings | What You're Playing",
"default_popup": "views/settings/index.html"
},

"options_ui": {
"page": "views/settings/index.html",
"open_in_tab": false
},

"options_page": "view/index.html"
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"homepage_url": "https://github.com/GenbuHase/WhatYouarePlaying"
}
File renamed without changes.
14 changes: 7 additions & 7 deletions views/index.html → views/settings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<HTML>
<Head>
<Meta Charset = "UTF-8" />
<Title>Setting | What You're Playing</Title>
<Title>Settings | What You're Playing</Title>

<Script Src = "lib/materialize.js"></Script>
<Link Rel = "StyleSheet" Href = "lib/materialize.css" />
<Link Rel = "StyleSheet" Href = "lib/Material Icons/material-icons.css" />
<Script Src = "../lib/materialize.js"></Script>
<Link Rel = "StyleSheet" Href = "../lib/materialize.css" />
<Link Rel = "StyleSheet" Href = "../lib/Material Icons/material-icons.css" />

<Script Src = "locale.js"></Script>
<Script Src = "main.js" Defer></Script>
<Link Rel = "StyleSheet" Href = "main.css" />
<Script Src = "../lib/Locale.js"></Script>
<Script Src = "settings.js" Defer></Script>
<Link Rel = "StyleSheet" Href = "settings.css" />
</Head>

<Body>
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 0a130e6

Please sign in to comment.