我正在尝试构建Chrome扩展程序。我的内容脚本是具有功能集的javascript类。
myClass = class {
constructor() {
console.log('Script loaded');
}
get one() {}
get two() {}
get three() {}
}
obj = new myClass();
当我单击chrome扩展名时,其想法是在javascript之上加载,我应该能够使用obj调用所有方法。当我从控制台的构造函数中收到“脚本已加载”消息时,当我尝试使用obj调用方法时,却出现了以下错误。
obj is not defined.
如果我在控制台中复制并粘贴整个内容脚本,然后尝试使用obj调用类方法,那么它将正常工作。我在这里做什么错了?
Manifest.json
{
"manifest_version":2,
"name":"ext1",
"description":"some desc",
"version":"1.0.1",
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"permissions":[
"contextMenus",
"activeTab"
]
}
background.js
var contextMenus = {};
contextMenus.ext1 =
chrome.contextMenus.create(
{ "title": "some title" },
function () {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
}
}
);
chrome.contextMenus.onClicked.addListener(contextMenuHandler);
function contextMenuHandler(info, tab) {
if (info.menuItemId === contextMenus.ext1) {
chrome.tabs.executeScript({
file: 'js/ext1.js'
});
}
}
答案 0 :(得分:0)
上下文可能有问题。您是否可以尝试分配obj
和myClass
来阻止范围变量let
或const
。示例:
const myClass = class {
constructor() {
console.log('Script loaded');
}
get one() {}
get two() {}
get three() {}
}
const obj = new myClass();