如何让侧边栏执行主文档中的 javascript 代码?

发布于 2024-10-05 11:17:54 字数 160 浏览 0 评论 0原文

我正在开发一个在主文档中加载网页的 Firefox 侧边栏。 现在我想从主文档调用一个 javascript 函数来响应侧边栏上的按钮。 侧边栏如何使主文档执行其 JavaScript 函数之一? 到目前为止,我有一个包含 javascript 代码的变量,但是如何执行它呢? (在加载的网站的上下文中?)

I'm working on a firefox sidebar that loads a webpage in the main document.
Now i'd like to call a javascript function from the main document in response of a pushed button on the sidebar.
How can the sidebar cause the main document to execute one of its javascript functions?
So far, I'm having a variable containing the javascript code, but how can it be executed? (in the context of the loaded website?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-10-12 11:17:54

Molle 博士的回答是正确的,但我总是喜欢工作示例。

console.log("Opening new page at http://example.com");
page = window.open('http://example.com');
console.log(page);
console.log("calling alert on opened page");
page.alert("I opened and the previous windows JS called me!!!!");
console.log("called alert");

示例页面 => [已删除。请参阅下面的更新]

结果:

从其他页面启动的警报
控制台日志

注意:
我必须允许弹出窗口才能使演示代码正常工作,但这是另一个问题的另一个问题。 ; )

firefox 弹出窗口被阻止通知

更新:

所以这样做

page.alert()

有效,因为它不必等待页面完成加载以使其正常工作。为了能够调用已加载页面上的脚本函数,您必须确保脚本已完成加载。为了说明这一点,我改变了我的例子。

示例页面 => http://mikegrace. s3.amazonaws.com/forums/stack-overflow/example-call-function-in-opened-page.html

JS 看起来像这样

page = window.open('http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-the-other-page.html');

var initOtherPage = function() {
  try {
    page.showMeTheMoney();
    console.log("It worked!");
  } catch (e) {
    console.log("failed to call function on other page because: "+e);
    setTimeout(function() {
      console.log("trying again")
      initOtherPage();
    }, 1000);
  }
}
initOtherPage();

控制台输出

alt text

打开的页面

alt text

Dr.Molle is correct with his answer but I always love working examples.

console.log("Opening new page at http://example.com");
page = window.open('http://example.com');
console.log(page);
console.log("calling alert on opened page");
page.alert("I opened and the previous windows JS called me!!!!");
console.log("called alert");

Example page => [Removed. See update below]

Results:

alert initiated from other page
console logs

Note:
I had to allow popups for the demo code to work but that is another issue for another SO question. ; )

firefox popup blocked notice

UPDATE:

So doing

page.alert()

worked because it didn't have to wait for the page to finish loading for it to work. In order to be able to call a function is a script on the loaded page you have to ensure that the script has finished loading. To do illustrate this I changed my example.

Example page => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-call-function-in-opened-page.html

The JS looks like this

page = window.open('http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-the-other-page.html');

var initOtherPage = function() {
  try {
    page.showMeTheMoney();
    console.log("It worked!");
  } catch (e) {
    console.log("failed to call function on other page because: "+e);
    setTimeout(function() {
      console.log("trying again")
      initOtherPage();
    }, 1000);
  }
}
initOtherPage();

Console output

alt text

Opened page

alt text

逆蝶 2024-10-12 11:17:54

使用 window.open() 加载页面并将其保存到变量中:

page=window.open('some.htm')

然后您可以使用该变量访问窗口并调用其中的函数:

page.someFunction()

Load the page using window.open() and save it into a variable:

page=window.open('some.htm')

then you can access the window using this variable and call functions inside:

page.someFunction()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文