背景.js
function getFrame() {
// popup.js can access background's frame with this function
return $('iframe')[0];
}
function updateStatus(status) {
// popup.js should call this function to update the frame's status
var frame = $('iframe')[0];
$(frame).data('status', status);
}
popup.js
..DOM READY
var bg = chrome.extension.getBackgroundPage();
('.hello').click(function () {
bg.updateStatus('ready')
}
// <ASSUME THE ABOVE EVENT HAS BEEN TRIGGERED HERE>
var frame = bg.getFrame();
console.log($(frame).data('status')) // undefined <- HOWEVER, THIS IS THE RESULT
我试图将状态存储在背景框架中,以便在弹出窗口重新打开时检索它。然而,上面是结果。有人可以解释为什么会发生这种情况吗?我是否缺少有关 data
属性的信息?
最佳答案
我找到了解决办法。我必须执行以下操作,而不是从 popup
中检索 frame
元素:
背景.js
function getStatus() {
// popup.js MUST retrieve the current status this way
// $(bg.getFrame()).data('status') IS BAD
var frame = $('iframe')[0];
return $(frame).data('status');
}
有人可以补充一下为什么会发生这种情况吗?
关于javascript - Chrome 扩展 : saving data attribute in background from popup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903140/