【发布时间】:2017-05-20 02:03:56
【问题描述】:
我正在尝试创建一个 chrome 扩展,允许用户在单击扩展按钮时向图形计算器 Desmos 添加新颜色。
两个基本文件:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript(tab.ib, {
file: "add_color.js"
});
});
和
(function() {
if (window.location.href === "https://www.desmos.com/calculator") {
var name = prompt("What would you like the name of the new color to be?");
var hex = prompt("What should the hex code of the new color be?");
window.Calc.colors[name] = hex;
};
})();
但是当我尝试运行它时,我得到了Uncaught TypeError: Cannot read property 'colors' of undefined。如果我使用 DevTools 控制台运行它,它会完美运行。有人能解释一下原因吗?
【问题讨论】:
-
插件中的窗口与页面上的窗口不同。看看:stackoverflow.com/questions/10485992/…
标签: javascript typeerror