【问题标题】:Can I call variables or function from background page in Chrome Packaged App?我可以从 Chrome Packaged App 的后台页面调用变量或函数吗?
【发布时间】:2013-09-24 07:13:08
【问题描述】:

我的 Chrome 打包应用程序有 2 个 JavaScript 文件。

1. background.js 是 manifest.json 调用的主要 JavaScript 文件

var foo = 'sss';

function bar(a,b) {
    return a+b;
}

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('index.html', {
    bounds: {
      width: 500,
      height: 300
    }
  });
});

2。 app.js 是 index.html 的控制器

问题:我可以从 app.js 中的 background.js 调用变量“foo”或函数“bar”吗?

PS. 或者有任何解决方案在页面之间转移价值,例如 chrome.storage

【问题讨论】:

标签: javascript html google-chrome-app


【解决方案1】:

在您的 app.js 中,您可以像这样获得背景页面:

chrome.runtime.getBackgroundPage(function(page) {
    page.foo;
    page.bar(2, 2);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 2016-09-20
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2014-04-03
    • 2016-05-23
    相关资源
    最近更新 更多