I am attempting to create a chrome extension that scrubs a URL and opens the URL in a new tab. However I keep getting the same error as this (content_script error). I’ve followed instructions, but I believe I just don’t understand where I’m going wrong. Here is the full code:
manifest.json
{
"name": "Link scrub",
"description": "Removes redirectors from links",
"version": "0.1",
"permissions": ["contextMenus", "tabs"],
"background_page" : "background.html"
"content_scripts": [{
"js" : ["linkscrub.js"]
}];
}
linkscrub.js
chrome.contextMenus.create({
"title" : "Link Trap",
"type" : "normal",
"contexts" : ["link"],
"onclick" : modifyLink
});
function modifyLink(info, tab) {
chrome.extension.sendRequest({
"nurl" = info.linkURL,
function(response) {
console.log("linkscrub failed: " + response.farewell)
}
});
}
background.html
<script>
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
link = "";
link = sender.nurl;
link = link.match("url=\b(.*?)&link");
chrome.tabs.create({
"url": link,
"selected" : false
});
if(chrome.extension.lastError)
sendResponse({farewell : chrome.extension.lastError.message});
else
sendResponse({farewell : "Success")};
});
<script>
It throws error because you cannot use
chrome.contextMenus.*API inside a content script.You don’t need a content script for this task, just move everything from
linkscrub.jsinto your background page (also you won’t be needing those requests).