-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update browser extension to manifest v3
- Loading branch information
1 parent
7695a06
commit 7bd1c9b
Showing
4 changed files
with
108 additions
and
45 deletions.
There are no files selected for viewing
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,25 +1,33 @@ | ||
window.browser = (() => window.msBrowser || window.browser || window.chrome)(); | ||
|
||
const endpoint = 'https://screenshot.rocks/api/setImage' | ||
const endpoint = 'https://screenshot.rocks/api/setImage'; | ||
|
||
const postData = (url, data) => { | ||
browser.tabs.create( | ||
{url: browser.runtime.getURL("post.html")}, (tab) => { | ||
chrome.tabs.create( | ||
{ url: chrome.runtime.getURL("post.html") }, | ||
(tab) => { | ||
const handler = (tabId, changeInfo) => { | ||
if (tabId === tab.id && changeInfo.status === "complete") { | ||
browser.tabs.onUpdated.removeListener(handler); | ||
browser.tabs.sendMessage(tabId, {url: url, data: data}); | ||
chrome.tabs.onUpdated.removeListener(handler); | ||
chrome.tabs.sendMessage(tabId, { url: url, data: data }).catch(error => { | ||
console.error("Error sending message:", error); | ||
}); | ||
} | ||
}; | ||
|
||
browser.tabs.onUpdated.addListener(handler); | ||
browser.tabs.sendMessage(tab.id, {url: url, data: data}); | ||
chrome.tabs.onUpdated.addListener(handler); | ||
} | ||
); | ||
}; | ||
|
||
browser.browserAction.onClicked.addListener(() => { | ||
browser.tabs.captureVisibleTab((screenshotUrl) => { | ||
postData(endpoint, {"image": screenshotUrl}); | ||
chrome.action.onClicked.addListener(() => { | ||
chrome.tabs.captureVisibleTab((screenshotUrl) => { | ||
postData(endpoint, { "image": screenshotUrl }); | ||
}); | ||
}); | ||
|
||
// Listen for connections from the content script | ||
chrome.runtime.onConnect.addListener(function(port) { | ||
port.onMessage.addListener(function(msg) { | ||
if (msg.ready) { | ||
console.log("Content script is ready"); | ||
} | ||
}); | ||
}); | ||
}); |
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
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
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,22 +1,26 @@ | ||
window.browser = (() => window.msBrowser || window.browser || window.chrome)(); | ||
// Establish a connection with the background script | ||
const port = chrome.runtime.connect({name: "screenshot"}); | ||
|
||
const handler = (message) => { | ||
browser.runtime.onMessage.removeListener(handler); | ||
const form = document.createElement("form"); | ||
form.setAttribute("method", "post"); | ||
form.setAttribute("action", message.url); | ||
form.setAttribute('enctype', 'application/x-www-form-urlencoded'); | ||
// Inform the background script that the content script is ready | ||
port.postMessage({ready: true}); | ||
|
||
for (const key in message.data) { | ||
const hiddenField = document.createElement("input"); | ||
hiddenField.setAttribute("type", "hidden"); | ||
hiddenField.setAttribute("name", key); | ||
hiddenField.setAttribute("value", message.data[key]); | ||
form.appendChild(hiddenField); | ||
} | ||
// Listen for messages from the background script | ||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { | ||
if (message.url && message.data) { | ||
const form = document.createElement("form"); | ||
form.setAttribute("method", "post"); | ||
form.setAttribute("action", message.url); | ||
form.setAttribute('enctype', 'application/x-www-form-urlencoded'); | ||
|
||
document.body.appendChild(form); | ||
form.submit(); | ||
} | ||
for (const key in message.data) { | ||
const hiddenField = document.createElement("input"); | ||
hiddenField.setAttribute("type", "hidden"); | ||
hiddenField.setAttribute("name", key); | ||
hiddenField.setAttribute("value", message.data[key]); | ||
form.appendChild(hiddenField); | ||
} | ||
|
||
browser.runtime.onMessage.addListener(handler); | ||
document.body.appendChild(form); | ||
form.submit(); | ||
} | ||
}); |