None of the issues I found are up to date or specific to my case.
I was following this tutorial https://www.youtube.com/watch?v=0n809nd4Zu4&ab_channel=freeCodeCamp. But, I adapted the code to fit what I needed.
For my background.js I followed what the tutorial did in determining whether a new URL was loaded.
// background.js
chrome.tabs.onUpdated.addListener((tabId, tab) => {
if (tab.url && tab.url.includes("ebay.com/sch")) {
const queryParameters = tab.url.split(/3&_/)[1];
console.log(queryParameters)
const urlParameters = new URLSearchParams(queryParameters);
chrome.tabs.sendMessage(tabId, {
type: "NEW",
searchID: urlParameters.get("nkw")
});
}
})
However I get the error in console Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
In the content script file I have
// contentScript.js
chrome.runtime.onMessage.addListener((obj, sender, response) => {
const { type, value, searchId } = obj;
console.log(type)
if (type === "NEW") {
currentSearch = searchId
newSearchLoaded();
}
});
// manifest.json
{
"name": "eBay Listing Remover",
"version": "0.1.0",
"description": "Remove unwanted eBay listings from search.",
"permissions": ["storage", "tabs"],
"host_permissions": ["https://*.ebay.com/*"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["https://*.ebay.com/*"],
"js": ["contentScript.js"]
}
],
"web_accessible_resources": [
{
"resources": [
"assets/bookmark.png",
"assets/play.png",
"assets/delete.png",
"assets/save.png"
],
"matches": ["https://*.ebay.com/*"]
}
],
"action": {
"default_icon": {
"16": "assets/ext-icon.png",
"24": "assets/ext-icon.png",
"32": "assets/ext-icon.png"
},
"default_title": "My YT Bookmarks",
"default_popup": "popup.html"
},
"manifest_version": 3
}
Edit:
Updated manifest.json
{
"name": "eBay Listing Remover",
"version": "0.1.0",
"description": "Remove unwanted eBay listings from search.",
"permissions": ["storage", "tabs"],
"host_permissions": ["https://*.ebay.com/*"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["https://*.ebay.com/*"],
"run_at": "document_end",
"js": ["contentScript.js"]
}
],
"web_accessible_resources": [
{
"resources": [
"assets/bookmark.png",
"assets/play.png",
"assets/delete.png",
"assets/save.png"
],
"matches": ["https://*.ebay.com/*"]
}
],
"action": {
"default_icon": {
"16": "assets/ext-icon.png",
"24": "assets/ext-icon.png",
"32": "assets/ext-icon.png"
},
"default_title": "My YT Bookmarks",
"default_popup": "popup.html"
},
"manifest_version": 3
}
Updated background.js
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab.url && tab.url.includes("ebay.com/sch")) {
const queryParameters = tab.url.split(/3&_/)[1];
console.log(queryParameters)
const urlParameters = new URLSearchParams(queryParameters);
chrome.tabs.sendMessage(tabId, {
type: "NEW",
searchID: urlParameters.get("nkw")
});
}
})
CodePudding user response:
// background.js
chrome.tabs.onUpdated.addListener((tabId, changedInfo, tab) => {
if (changeInfo.status === "complete" && tab.url && tab.url.includes("ebay.com/sch")) {
const queryParameters = tab.url.split(/3&_/)[1];
console.log(queryParameters)
const urlParameters = new URLSearchParams(queryParameters);
chrome.tabs.sendMessage(tabId, {
type: "NEW",
searchID: urlParameters.get("nkw")
});
}
})
Please try this code. Also, please change the run_at to document_end
in manifest.json