简体   繁体   English

未捕获的语法错误:无效或意外的令牌(Chrome 扩展)

[英]Uncaught SyntaxError: Invalid or unexpected token (Chrome Extension)

I'm basically trying to create an extension that finds and replaces a single image, but the background script keeps coming up with a SyntaxError no matter how much I fiddle with it.我基本上是在尝试创建一个扩展来查找和替换单个图像,但是无论我怎么摆弄它,后台脚本都会不断出现 SyntaxError。

Screenshot of error that comes up出现错误的屏幕截图

I'm following this tutorial for the most part, except I'm looking to replace a single image instead of multiple.我大部分时间都在遵循本教程,除了我要替换单个图像而不是多个图像。 I have pasted the code I have so far with specific details redacted:我已经粘贴了到目前为止的代码,并编辑了具体细节:

manifest.json清单.json

{
"manifest_version" : 2,
"name" : "Test",
"version" : "0.1",
"description" : "test",
"web_accessible_resources" : [
"*.png"
],
"icons" : {
"128": "test.png"
},
"background" : {
"scripts" : ["bgp.js"]
},
"browser_action" : {
"default_icon" : "test.png"
},
"content_scripts" : [
{
"matches" : [
"https://test.com/, https://test.com/*"
],
"js" : ["content.js"]
}
]
} 

bgp.js bgp.js

console.log(“Background running”);
chrome.browserAction.onClicked.addListener(buttonClicked);
function buttonClicked(tab)
{
let msg = {
txt : “Hello”
}
chrome.tabs.sendMessage(tab.id,msg);
}
}

content.js (adding for the sake of completion) content.js (为了完成而添加)

console.log('replacing image');
chrome.runtime.onMessage.addListener(replace);

function replace()
{

let imgs = document.querySelector(".example");

let url = chrome.extension.getURL("test.png");


   img.src = url;
console.log(url);

}

You're using the wrong quotation marks in your background script.您在后台脚本中使用了错误的引号。 You're using but you should use " or ' .你正在使用但你应该使用"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM