Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add chrome(ium) support to extension #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions extension/manifest-chrome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{

"manifest_version": 3,
"name": "Pipewire Screenaudio",
"version": "0.3.4",

"description": "Passthrough pipewire audio to WebRTC screenshare",

"action": {
"default_popup": "assets/web/html/popup.html"
},

"icons": {
"16": "assets/icons/icon.svg",
"32": "assets/icons/icon.svg",
"48": "assets/icons/icon.svg",
"64": "assets/icons/icon.svg"
},

"content_scripts": [
{
"matches": ["<all_urls>"],
"run_at": "document_start",
"js": ["scripts/injector.js"],
"all_frames": true
}
],

"background": {
"service_worker": "scripts/background.js"
},

"permissions": [
"nativeMessaging"
],

"web_accessible_resources": [
{
"matches": ["<all_urls>"],
"resources": [ "scripts/index.js" ]
}
]

}
13 changes: 7 additions & 6 deletions extension/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ async function runQueuedCommands () {
const { inMap, outMap } = command.maps || {};

if (inMap) {
inMap.forEach(([storageKey, argKey]) => {
args[argKey] = window.localStorage.getItem(storageKey);
});
for (const [storageKey, argKey] of inMap) {
args[argKey] = await chrome.runtime.sendMessage({ messageName: "get-storage", storageKey: storageKey })
}
}

console.log(args)
const result = await sendNativeMessage(command.messageName, command.cmd, args, command.maps)
console.log(result)

if (outMap) {
outMap.forEach(([storageKey, resultKey]) => {
window.localStorage.setItem(storageKey, (resultKey ? result[resultKey] : null));
});
for (const [storageKey, resultKey] of outMap) {
const value = resultKey ? result[resultKey] : null
await chrome.runtime.sendMessage({ messageName: 'set-storage', storageKey: storageKey, value: value })
}
}

if (command.event) {
Expand Down
2 changes: 1 addition & 1 deletion extension/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function overrideGdm () {
})
const [track] = captureSystemAudioStream.getAudioTracks()
let fakegdm;
if (new RegExp('^(.+\.)?discord.com$').test(window.location.host) && sessionType === "wayland") {
if (new RegExp('^(.+\.)?discord.com$').test(window.location.host) && sessionType === "wayland" && typeof browser != 'undefined') {
fakegdm = navigator.mediaDevices.chromiumGetDisplayMedia({
video: true
})
Expand Down
2 changes: 1 addition & 1 deletion extension/scripts/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function injectCode (src) {
script.onload = function () {
console.log('pipewire-screenaudio script injected')

browser.runtime
chrome.runtime
.sendMessage({ messageName: MESSAGE_NAME, message: 'get-session-type' })
.then(({ type }) => {
window.postMessage({ message: "set-session-type", type })
Expand Down
22 changes: 18 additions & 4 deletions extension/scripts/popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MESSAGE_NAME = 'com.icedborn.pipewirescreenaudioconnector'
const EXT_VERSION = browser.runtime.getManifest().version
const EXT_VERSION = chrome.runtime.getManifest().version

const dropdown = document.getElementById('dropdown')
const message = document.getElementById('message')
Expand Down Expand Up @@ -224,7 +224,7 @@ function onReload (response) {
updateGui()
}

function onResponse (response) {
async function onResponse (response) {
if (!checkVersionMatch(response.version)) {
message.innerText = `Version mismatch\nExtension: ${EXT_VERSION}\nNative: ${response.version}`
dropdown.hidden = true
Expand All @@ -234,7 +234,12 @@ function onResponse (response) {
settings.addEventListener('click', async () => {
window.open('settings.html')
})
chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'GetNodes', args: [] }).then(onReload, onError)
if (typeof browser == 'undefined') {
// Due to chrome behavior (bug?) we need to await for this to not crash the extension
await chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'GetNodes', args: [] }).then(onReload, onError)
} else {
chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'GetNodes', args: [] }).then(onReload, onError)
}
nodesLoop = setInterval(() => { chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'GetNodes', args: [] }).then(onReload, onError) }, 1000)
window.localStorage.setItem('nodesList', null)
updateGui()
Expand All @@ -246,7 +251,7 @@ function onError (error) {
dropdown.hidden = true
}

function handleMessage (message) {
function handleMessage (message, sender, sendResponse) {
if (message === 'mic-id-updated') {
setMicId(window.localStorage.getItem('micId'), true)

Expand All @@ -258,6 +263,15 @@ function handleMessage (message) {
if (message === 'mic-id-removed') {
updateGui()
}

if (message.messageName === 'set-storage') {
window.localStorage.setItem(message.storageKey, message.value)
sendResponse(true)
}

if (message.messageName === 'get-storage') {
sendResponse(window.localStorage.getItem(message.storageKey))
}
}

chrome.runtime.onMessage.addListener(handleMessage)
Expand Down
6 changes: 6 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
projectRoot="$( cd -- "$(dirname "$0")" > /dev/null 2>&1 ; pwd -P )"

mkdir -p ~/.mozilla/native-messaging-hosts
mkdir -p ~/.config/chromium/NativeMessagingHosts
mkdir -p ~/.config/google-chrome/NativeMessagingHosts
mkdir -p ~/.config/microsoft-edge/NativeMessagingHosts

sed "s|/usr/lib/pipewire-screenaudio|$projectRoot/native|g" $projectRoot/native/native-messaging-hosts/firefox.json > ~/.mozilla/native-messaging-hosts/com.icedborn.pipewirescreenaudioconnector.json
sed "s|/usr/lib/pipewire-screenaudio|$projectRoot/native|g" $projectRoot/native/native-messaging-hosts/chromium.json > ~/.config/chromium/NativeMessagingHosts/com.icedborn.pipewirescreenaudioconnector.json
sed "s|/usr/lib/pipewire-screenaudio|$projectRoot/native|g" $projectRoot/native/native-messaging-hosts/chromium.json > ~/.config/google-chrome/NativeMessagingHosts/com.icedborn.pipewirescreenaudioconnector.json
sed "s|/usr/lib/pipewire-screenaudio|$projectRoot/native|g" $projectRoot/native/native-messaging-hosts/chromium.json > ~/.config/microsoft-edge/NativeMessagingHosts/com.icedborn.pipewirescreenaudioconnector.json