Created
October 28, 2016 07:50
-
-
Save tor4kichi/5ea925832be5887a873ac8e7c8394ba5 to your computer and use it in GitHub Desktop.
ニコニコ動画のURLをコンテキストメニューからniconico://プロトコルに変換してタブを開き、不要なタブを閉じる(タブを閉じる動作がこのままでは機能しない?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var NiocvideoContentIdRegex = /^https?:\/\/(?:www|live)\.nicovideo.jp\/watch\/((?:sm|lv|so)\d*|\d*)/; | |
var NiocvideoShortContentIdRegex = /^https?:\/\/nico.ms\/((?:sm|lv|so)\d*|\d*)/; | |
var TargetUrls = ["*://www.nicovideo.jp/*", "*://live.nicovideo.jp/*", "*://nico.ms/*"]; | |
function isNiconicoUrlWithContentId(url) | |
{ | |
return NiocvideoContentIdRegex.test(url) || NiocvideoShortContentIdRegex.text(url); | |
} | |
function extractNiconicoContentId(url) | |
{ | |
var match = url.match(NiocvideoContentIdRegex); | |
console.log("match -> " + match); | |
if (match != null && match.length >= 2) | |
{ | |
return match[1]; | |
} | |
match = url.match(NiocvideoShortContentIdRegex); | |
console.log("short match -> " + match); | |
if (match != null && match.length >= 2) | |
{ | |
return match[1]; | |
} | |
return null; | |
} | |
function convertToNiconicoProtocolUrl(url) | |
{ | |
var contentId = extractNiconicoContentId(url); | |
if (contentId != null) | |
{ | |
return "niconico://" + contentId; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
function OnTabCreated(tabInfo) | |
{ | |
console.log("tab id " + tabInfo.id); | |
chrome.tabs.remove(tabInfo.id, () => | |
{ | |
if (chrome.runtime.lastError) { | |
console.log("failed: " + chrome.runtime.lastError); | |
} else { | |
console.log("removed tab"); | |
} | |
}); | |
} | |
function OpenNiconicoProtocol(niocnicoUrl) | |
{ | |
chrome.tabs.create({url: niocnicoUrl, active:false}, OnTabCreated); | |
} | |
/* | |
ニコニコ動画で開くためのコンテキストメニュー | |
*/ | |
chrome.contextMenus.create({ | |
id: "play", | |
title: chrome.i18n.getMessage("contextMenuItemPlay"), | |
contexts: ["link"], | |
targetUrlPatterns: TargetUrls, | |
onclick : (info, tab) => | |
{ | |
console.log(info); | |
var niconicoUri = convertToNiconicoProtocolUrl(info.linkUrl); | |
if (niconicoUri != null) | |
{ | |
console.log("[PLAY] " + niconicoUri); | |
OpenNiconicoProtocol(niconicoUri); | |
} | |
} | |
}); | |
chrome.contextMenus.create({ | |
id: "separator", | |
contexts: ["link"], | |
targetUrlPatterns:TargetUrls, | |
type: "separator" | |
}); | |
chrome.contextMenus.create({ | |
id: "add-playlist", | |
title: chrome.i18n.getMessage("contextMenuItemNicoAddToPlaylist"), | |
contexts: ["link"], | |
targetUrlPatterns:TargetUrls, | |
onclick : (info, tab) => | |
{ | |
console.log(info.linkUrl); | |
var niconicoUri = convertToNiconicoProtocolUrl(info.linkUrl); | |
if (niconicoUri != null) | |
{ | |
var niconicoUriWithAddPlaylist = niconicoUri + "?addplaylist=1"; | |
console.log("[Add Playlist] " + niconicoUriWithAddPlaylist); | |
OpenNiconicoProtocol(niconicoUriWithAddPlaylist); | |
} | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "__MSG_extensionName__", | |
"description": "__MSG_extensionDescription__", | |
"version": "1.0", | |
"applications": { | |
"gecko": { | |
"id": "sample@tor4kichi" | |
} | |
}, | |
"permissions": [ | |
"http://*/*", | |
"https://*/*", | |
"tabs", | |
"contextMenus", | |
"webNavigation" | |
], | |
"background": { | |
"scripts": ["background/background.js"] | |
}, | |
"default_locale": "ja" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment