-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate to manifest v3, fix extension showAction rule
- Loading branch information
Showing
5 changed files
with
201 additions
and
164 deletions.
There are no files selected for viewing
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,69 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
console.log("[YTGREP] BackgroundJS init"); | ||
|
||
// extension only active on youtube watch page | ||
const default_rule = { | ||
id: "default_rule", | ||
id: "enable_action", | ||
conditions: [ | ||
new chrome.declarativeContent.PageStateMatcher({ | ||
pageUrl: {hostEquals: "www.youtube.com"}, | ||
css: ["video"] | ||
}) | ||
pageUrl: { | ||
hostEquals: "www.youtube.com", | ||
schemes: ["https"], | ||
pathContains: "watch", | ||
}, | ||
}), | ||
], | ||
actions: [ new chrome.declarativeContent.ShowPageAction() ] | ||
} | ||
actions: [new chrome.declarativeContent.ShowAction()], | ||
}; | ||
|
||
chrome.runtime.onInstalled.addListener(function() { | ||
console.log('ytGrep installed successfully!'); | ||
chrome.runtime.onInstalled.addListener(function () { | ||
console.log("ytGrep installed successfully!"); | ||
chrome.action.disable(); | ||
|
||
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { | ||
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () { | ||
chrome.declarativeContent.onPageChanged.addRules([default_rule]); | ||
}); | ||
}); | ||
|
||
// setIcon when on youtube.com | ||
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ | ||
if(message.type === "showPageAction"){ | ||
chrome.pageAction.setIcon({ | ||
tabId: sender.tab.id, | ||
path: {"16": "./assets/icons/ytGrep16.png", "24": "./assets/icons/ytGrep24.png"} | ||
}); | ||
} | ||
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { | ||
if (message.type === "showPageAction") { | ||
console.log("[YTGREP::showPageAction] init"); | ||
chrome.declarativeContent.onPageChanged.getRules( | ||
["enable_action"], | ||
(rules) => { | ||
if (!rules[0]) { | ||
console.log("enable_action rule was not set, adding now."); | ||
chrome.declarativeContent.onPageChanged.removeRules( | ||
undefined, | ||
function () { | ||
chrome.declarativeContent.onPageChanged.addRules([default_rule]); | ||
} | ||
); | ||
} | ||
} | ||
); | ||
chrome.action.setIcon({ | ||
tabId: sender.tab.id, | ||
path: { | ||
16: "./assets/icons/ytGrep16.png", | ||
24: "./assets/icons/ytGrep24.png", | ||
}, | ||
}); | ||
} | ||
}); | ||
|
||
chrome.tabs.onRemoved.addListener(function(tabId) { | ||
chrome.tabs.onRemoved.addListener(function (tabId) { | ||
try { | ||
chrome.storage.local.remove(tabId.toString(), function(){ | ||
if(chrome.runtime.lastError === undefined){ | ||
chrome.storage.local.remove(tabId.toString(), function () { | ||
if (chrome.runtime.lastError === undefined) { | ||
console.log("Removed:", tabId); | ||
}else{ | ||
} else { | ||
console.log(chrome.runtime.lastError); | ||
} | ||
}); | ||
} catch (e){ | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,59 @@ | ||
// init | ||
console.log('ytGREP extension loaded!'); | ||
console.log("ytGREP extension loaded!"); | ||
|
||
// message to background.js to setIcon | ||
chrome.runtime.sendMessage({type:'showPageAction'}); | ||
chrome.runtime.sendMessage({ type: "showPageAction" }); | ||
|
||
// receive from webpage via window | ||
// transmit to extension via runtime | ||
window.addEventListener("message", function(event) { | ||
// We only accept messages from ourselves | ||
if (event.source != window) | ||
return; | ||
|
||
if (event.data.type && (event.data.type === "CAPS")) { | ||
// console.log("Message from webpage: " + event.data.text); | ||
if(typeof chrome.app.isInstalled!=='undefined'){ | ||
chrome.runtime.sendMessage({type: "CAPS", status: event.data.text, capsArr: event.data.capsArr}, function(response) { | ||
// console.log('Message from extension:', response.reply); | ||
}); | ||
window.addEventListener( | ||
"message", | ||
function (event) { | ||
// We only accept messages from ourselves | ||
if (event.source != window) return; | ||
|
||
if (event.data.type && event.data.type === "CAPS") { | ||
// console.log("Message from webpage: " + event.data.text); | ||
if (typeof chrome.app.isInstalled !== "undefined") { | ||
chrome.runtime.sendMessage( | ||
{ | ||
type: "CAPS", | ||
status: event.data.text, | ||
capsArr: event.data.capsArr, | ||
}, | ||
function (response) { | ||
// console.log('Message from extension:', response.reply); | ||
} | ||
); | ||
} | ||
} | ||
} | ||
}, false); | ||
}, | ||
false | ||
); | ||
|
||
// listening to extension | ||
chrome.runtime.onMessage.addListener( | ||
function(request, sender, sendResponse){ | ||
if(request.type === "PLAYER" && request.action === "SEEK"){ | ||
window.postMessage({type: "PLAYER", action: "SEEK",time: request.time}, "*"); | ||
} | ||
|
||
if(request.type === "PLAYER" && request.action === "PLAY"){ | ||
window.postMessage({type: "PLAYER", action: "PLAY"}, "*"); | ||
} | ||
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { | ||
if (request.type === "PLAYER" && request.action === "SEEK") { | ||
window.postMessage( | ||
{ type: "PLAYER", action: "SEEK", time: request.time }, | ||
"*" | ||
); | ||
} | ||
|
||
if(request.type === "PLAYER" && request.action === "PAUSE"){ | ||
window.postMessage({type: "PLAYER", action: "PAUSE"}, "*"); | ||
} | ||
if (request.type === "PLAYER" && request.action === "PLAY") { | ||
window.postMessage({ type: "PLAYER", action: "PLAY" }, "*"); | ||
} | ||
) | ||
|
||
if (request.type === "PLAYER" && request.action === "PAUSE") { | ||
window.postMessage({ type: "PLAYER", action: "PAUSE" }, "*"); | ||
} | ||
}); | ||
|
||
// inject ytPlayer into the webpage | ||
let script = document.createElement('script'); | ||
script.src = chrome.runtime.getURL('inject/ytPlayer.js'); | ||
script.onload = function() { | ||
this.remove(); | ||
let script = document.createElement("script"); | ||
script.id = "ytGrep"; | ||
script.src = chrome.runtime.getURL("inject/ytPlayer.js"); | ||
script.onload = function () { | ||
this.remove(); | ||
}; | ||
(document.head || document.documentElement).appendChild(script); | ||
|
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
Oops, something went wrong.