Discussions » Development

Open Youtube in VLC player from browser - Greasemonkey

§
Posted: 2018-09-04
Edited: 2018-09-04

Open Youtube in VLC player from browser - Greasemonkey

I wanted to attempt to make a Greasemonkey script to open Youtube links in VLC Player. I managed to get one to work with right clicking a link and having a "Open as M3U" option appear at the top of the menu.

I wanted it to work on Android with Firefox and Greasemonkey, but it didn't. Any suggestions for Android?

/*
    Google Image Search Context Menu
    Add 'Search by Image' in browser context menu when you
    right click on image to search Google with that image.
    Copyright (C) 2012 LouCypher

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>
*/
// ==UserScript==
// @name            Open Youtube as M3U Context Menu
// @originalname    Google Image Search Context Menu
// @namespace       http://userscripts.org/users/12
// @description     Add 'Open Link as M3U' in browser context menu when you right click on image in Youtube.
// @version         1.2
// @author          LouCypher
// @ModifiedBy      Marky - Changed program to open Youtube links as M3U to watch on programs like VLC Player
// @license         GPL
// @resource        license https://raw.github.com/LouCypher/userscripts/master/licenses/GPL/LICENSE.txt
// @updateURL       https://userscripts.org/scripts/source/151097.meta.js
// @include         *
// @exclude         file://*
// @grant           GM_openInTab
// ==/UserScript==
var body = document.body;
body.addEventListener("contextmenu", initMenu, false);
var menu = body.appendChild(document.createElement("menu"));
menu.outerHTML = '<menu id="userscript-search-by-image" type="context">\
                    <menuitem label="Open Link As M3U"\
                              icon="data:image/png;base64,\
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\
AAAK6wAACusBgosNWgAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNXG14zYAAAEl\
SURBVDiNY/z//z8DJYCRkIKsthv/kRX9Z2BgmFalARdiIcaGKZXqcH5O+01U+ay2G3MYGBiSiXUm\
mofnsBDSjEUTMkiBe2Eq1JnZ7TcZBHhZGNythBl0lLkZODmYGX7++sdw/sZnhl3H3zF8+voHwwsY\
FkR5ijNICLMzTF31hOHnr38MHGxMDJlhMgwv3vxkWL7jJYpaJmzu0lTigWtmYGBg+PHrH8P0VU8Y\
tJV5MNRiNYCfmxmuGQZ+/PrHwMmOqRyrAX///WfgYEOV4mBjwjAUpwHHL31iyA6XgRvCwcbEkBUm\
w3DuxmcMtVgDkYONicHLVoTBSJOXgYONieHHz38Ml+98Ydh88DXDtx//CBtACmBiYGCYS4H+OYyU\
5kasgUgKAADN8WLFzlj9rgAAAABJRU5ErkJggg=="></menuitem>\
                  </menu>';
document.querySelector("#userscript-search-by-image menuitem")
        .addEventListener("click", searchImage, false);
function initMenu(Event) {
  // Executed when user right click on web page body
  // Event.target is the element you right click on
  var node = Event.target;
  var node2 = Event.target.parentNode;
  var node3 = node2.parentNode;
  var item = document.querySelector("#userscript-search-by-image menuitem");
  if (node.localName == "img") {
    body.setAttribute("contextmenu", "userscript-search-by-image");
    item.setAttribute("imageURL", node3.href);  
  } else {
    body.setAttribute("contextmenu", "userscript-search-by-image");
    item.setAttribute("imageURL", node.href);
  }
}
function addParamsToForm(aForm, aKey, aValue) {
  var hiddenField = document.createElement("input");
  hiddenField.setAttribute("type", "hidden");
  hiddenField.setAttribute("name", aKey);
  hiddenField.setAttribute("value", aValue);
  aForm.appendChild(hiddenField);
}
function download(filename, text) {
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', filename);
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}
function searchImage(Event) {
  // Executed when user click on menuitem
  // Event.target is the <menuitem> element
  var imageURL = Event.target.getAttribute("imageurl");
    var filename = "test.m3u";    
    download(filename, imageURL); 
}
§
Posted: 2018-09-04

wOxxOmMod
§
Posted: 2018-09-04

See MDN: the contextmenu attribute (on body) is obsolete and will be removed from all browsers. You'll have to handle the contextmenu event and display your menu explicitly.

§
Posted: 2018-09-05

Okay, good to know. So I disabled right click menu, and created a popup for now. That works.

The problem now is when using "download(filename, imageURL);" Android Firefox will automatically download the file and not ask to open in another program.

When you look at the download using this method the file is shown as a text file:

But when downloading from an actual link, it is shown as a media file:

I just need to figure out now how to make the Newly created file look like a media file and not a text file.

§
Posted: 2018-09-05

function download(filename, text) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', filename);

element.style.display = 'none';
document.body.appendChild(element);

element.click();

document.body.removeChild(element);

}

Post reply

Sign in to post a reply.