Skip to content

Commit

Permalink
feat: manifest v3
Browse files Browse the repository at this point in the history
  • Loading branch information
eastmarch committed Nov 30, 2023
1 parent 2b2ccb3 commit 95373d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
5 changes: 5 additions & 0 deletions background-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
importScripts('background.js');
} catch (e) {
console.error(e);
}
40 changes: 22 additions & 18 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
'use strict';

chrome.runtime.onInstalled.addListener(function() {
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "chr2mpv",
title: "Play link in MPV",
contexts: ["link"],
id: 'chr2mpv',
title: 'Play link in MPV',
contexts: ['link'],
});
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "chr2mpv") playInMPV(info.linkUrl);
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId == 'chr2mpv') playInMPV(tab.id, info.linkUrl, false);
});

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({code: `(${String(pausePlayback)})();`});
playInMPV(tab.url);
chrome.action.onClicked.addListener((tab) => {
playInMPV(tab.id, tab.url, true);
});

function playInMPV(targetURL) {
var link = new URL(targetURL);
link.protocol = "ytdl:";
// chrome.tabs.update({ url: link.href });
chrome.tabs.executeScript({code: `window.location.href = "${link.href}";`});
function playInMPV(tabID, targetURL, pausePlayback) {
let link = targetURL.replace(new RegExp('^(http|https)://', 'i'), 'ytdl://');
chrome.scripting.executeScript({
target: { tabId: tabID },
func: redirectMPV,
args: [link, pausePlayback],
});
}

function pausePlayback() {
var htmlPlayer = document.querySelector("video");
if(htmlPlayer != null) {
htmlPlayer.pause();
function redirectMPV(redirectURL, pausePlayback) {
if (pausePlayback) {
let htmlPlayer = document.querySelector('video');
if (htmlPlayer) {
htmlPlayer.pause();
}
}
window.location.href = redirectURL;
}
15 changes: 7 additions & 8 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
{
"name": "chr2mpv",
"version": "1.1",
"manifest_version": 2,
"version": "1.2",
"manifest_version": 3,
"description": "Tries to play the current URL on MPV. Right-click on links or press ALT+M to activate.",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7KzhDu23iF29ixXsTXg3yin52AIiQ70VtigLNq/Z/eQtmopiYwhzvtqZtE2E75Eu7dY1+SefOpVyvUoPLrzKoy/XgaUG1UTS7qmdsjIKt5dWcVqg3JjiZ8ErNLhIRoUGWo3Ihi6xqigJDYy3nTgAhRSFRA31kLBrhCo00Vlm1LbK26H/WvSInr57N2dNmY1z2Em7IeMn109JSrfFKbrYPItuo2qh5/7R350h9x/Ijr4q0KUIIbEao4RS2Fp+aS4fnd9Vpu+B6bkOqXlNB7km3Zht6EZqOhHOCNve9sGa+i64TCiERFlgOQIiTCQNs5aChcgxhnwzC91GI1D5jU3n7wIDAQAB",
"permissions": ["activeTab", "contextMenus"],
"permissions": ["activeTab", "contextMenus", "scripting"],
"icons": {
"128": "img/icon_128.png",
"64": "img/icon_64.png",
"32": "img/icon_32.png",
"16": "img/icon_16.png"
},
"browser_action": {
"default_title": "chr2mpv"
"action": {
"default_title": "Open in MPV"
},
"background": {
"scripts": ["background.js"],
"persistent": false
"service_worker": "background-wrapper.js"
},
"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"default": "Alt+M"
}
Expand Down

0 comments on commit 95373d7

Please sign in to comment.