Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Open websites with VLC. You must install the vlc:// protocol as well: https://github.com/stefansundin/vlc-protocol
function open(url, tabId) {
console.log(`vlc://${url}`);
// Open a new tab to start the prompt
chrome.tabs.create({url: `vlc://${url}`}, function(newtab) {
// Focus the old tab
chrome.tabs.update(tabId, {active: true});
// You have 10 seconds to click the button
// If the tab is closed too soon, the button no longer works :(
setTimeout(function(){
chrome.tabs.remove([newtab.id]);
}, 10000);
});
}
function listener(message) {
if (message == "open-vlc") {
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
var tab = tabs[0];
open(tab.url, tab.id);
});
}
}
chrome.runtime.onMessage.addListener(listener);
chrome.commands.onCommand.addListener(listener);
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "open-vlc") {
open(info.linkUrl || info.frameUrl, tab.id);
}
})
chrome.contextMenus.create({
id: "open-vlc",
title: "Open with VLC",
contexts: ["link", "frame"],
});
#!/bin/bash -ex
V=$(cat manifest.json | jq -Mr .version)
rm -f "open-with-vlc-$V.zip"
zip -r "open-with-vlc-$V.zip" . -x '*.git*' -x '*.DS_Store' -x '*Thumbs.db' -x '*.sh' -x '*.zip'
{
"name": "Open with VLC",
"version": "1.2.2",
"description": "Open websites with VLC. You must install the vlc:// protocol as well. You should use this together with playlist parsers.",
"homepage_url": "https://gist.github.com/stefansundin/af4a90025a1ea72d7dff74251d009ede",
"author": "Stefan Sundin",
"icons": {
"16": "icon.png",
"48": "icon.png",
"128": "icon.png"
},
"background": {
"scripts": [ "background.js" ]
},
"options_ui": {
"page": "options.html",
"chrome_style": true
},
"browser_action": {
"default_icon": {
"16": "icon.png",
"19": "icon.png",
"38": "icon.png"
},
"default_title": "Open page with VLC",
"default_popup": "popup.html"
},
"commands": {
"open-vlc": {
"suggested_key": { "default": "Alt+Shift+V" },
"description": "Open page with VLC"
}
},
"permissions": [
"tabs",
"contextMenus",
"storage"
],
"manifest_version": 2
}
body {
width: 700px;
min-height: 215px;
}
#extension_version {
float: right;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Options</title>
<script src="options.js"></script>
<link rel="stylesheet" type="text/css" href="options.css">
</head>
<body>
<h1>
Options
<small id="extension_version"></small>
</h1>
<p><label><input type="checkbox" id="open_immediately"> Open immediately</label></p>
<p>Keyboard shortcut: <a id="shortcut" href="chrome://extensions/configureCommands">error</a>.</p>
</body>
</html>
var version = `v${chrome.runtime.getManifest().version}`
var default_options = {
open_immediately: false
};
document.addEventListener("DOMContentLoaded", function() {
document.querySelector("#extension_version").textContent = version;
var open_immediately = document.getElementById("open_immediately");
chrome.storage.sync.get(default_options, function(items) {
open_immediately.checked = items.open_immediately;
});
open_immediately.addEventListener("change", function() {
var new_options = {
open_immediately: open_immediately.checked
};
chrome.storage.sync.set(new_options, function(){});
});
var shortcut = document.querySelector("#shortcut");
shortcut.addEventListener("click", function() {
chrome.tabs.create({url: "chrome://extensions/configureCommands"});
});
chrome.commands.getAll(function(commands) {
commands.forEach(function(command) {
if (command.name == "open-vlc") {
shortcut.textContent = command.shortcut || "not set";
}
});
});
});
body {
width: 300px;
padding: 10px;
}
h2 {
margin-top: 0;
border-bottom: 1px solid #ccc;
padding-bottom: 0.125em;
}
#extension_version {
float: right;
}
button {
width: 100%;
height: 30px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="popup.css">
<script src="options.js"></script>
<script src="popup.js"></script>
</head>
<body>
<h2>
<span>Open with VLC</span>
<small id="extension_version"></small>
</h2>
<button id="open">Open</button>
<p><label><input type="checkbox" id="open_immediately"> Open immediately</label></p>
<p>Keyboard shortcut: <a id="shortcut" href="chrome://extensions/configureCommands">error</a>.</p>
<p>Note: The <a target="_blank" href="https://github.com/stefansundin/vlc-protocol"><tt>vlc://</tt> protocol handler</a> is required.</p>
</body>
</html>
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
// do not open immediately if this is e.g. the New Tab page
// this gives you an easy way to uncheck the checkbox
var url = tabs[0].url;
if (!url.startsWith("http")) return;
chrome.storage.sync.get(default_options, function(items) {
if (items.open_immediately) {
chrome.runtime.sendMessage("open-vlc");
window.close();
}
});
});
document.addEventListener("DOMContentLoaded", function() {
document.querySelector("#open").addEventListener("click", function() {
chrome.runtime.sendMessage("open-vlc");
window.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment