javascript - Chrome 扩展 : saving data attribute in background from popup

标签 javascript jquery google-chrome-extension

背景.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/

相关文章:

javascript - JavaScript 中的条码扫描器?或插件/扩展

php - 喊话箱 : Best way to count users active

javascript - 如何从 float 中 chop 多余的零

javascript - 调用 child_added 监听器时返回自动生成的 ID

javascript - 单击特定行内的按钮时获取行值

javascript - 使用模块模式访问 jQuery 回调中的私有(private)变量

javascript - Node js 的谷歌端到端加密

events - jQuery event.stopPropagation 不适用于 <a>

css - 如何在弹出式 chrome 扩展中制作边框半径?

javascript - Chrome 扩展程序 - 请求新权限