简体   繁体   English

Firefox XUL扩展在注入脚本中调用函数?

[英]Firefox XUL Extension Calls Function in Injected Script?

Two part question: 两部分问题:

1) How do you inject a script (for example 'library.js') to the HTML DOM when making an XUL extension? 1)在进行XUL扩展时,如何将脚本(例如'library.js')注入HTML DOM?

2) I currently have a toolbar button which I want to call a function in 'library.js' when the button is clicked. 2)我目前有一个工具栏按钮,单击该按钮时我想在“ library.js”中调用一个函数。

Here is my current XUL for the toolbar button 这是我当前用于工具栏按钮的XUL

<toolbarpalette id="BrowserToolbarPalette">
  <toolbarbutton id="myextension-button" 
    class="toolbarbutton-1"
    image="chrome://xulschoolhello/skin/favicon.png"
      oncommand="doStuff();"
    label="label" tooltiptext="tooltip" />
</toolbarpalette>

Here is library.js (which will be injected into the DOM). 这是library.js(将注入到DOM中)。 Library.js will have a bunch of functions modifying the DOM. Library.js将具有大量修改DOM的功能。 Here is one example: 这是一个例子:

function changeTheDOM(){
     document.body.innerHTML = 'I changed it';
}

I want doStuff() to somehow call the function changeTheDOM(), which is in the HTML DOM. 我希望doStuff()以某种方式调用HTML DOM中的function changeTheDOM()。 Let me know if that makes sense. 让我知道这是否有意义。

With all these different namespaces/context, its very difficult for me as a beginner to grasp how to interact between my application code and the DOM. 对于所有这些不同的名称空间/上下文,作为初学者的我来说很难理解如何在应用程序代码和DOM之间进行交互。

User reached out to me. 使用者与我联络。 They created an overlay extension. 他们创建了覆盖扩展程序。 So this solution is different from the bootstrap demo I showed in my other solution submited, as bootstrap addons don't support resource in chrome.manifest. 因此,此解决方案与我在提交的其他解决方案中展示的引导演示不同,因为引导插件不支持chrome.manifest中的资源。 ("resource" is needed because it is accessible by content, in bootstrap you have to set "contentaccessible=true" to the chrome package) (需要“资源”,因为它可以被内容访问,在引导程序中,您必须将chrome包设置为“ contentaccessible = true”)

The repository of the test addon created by blee908 is HERE@GitHub blee908创建的测试插件的存储库为HERE @ GitHub

I forked this addon and made the updates HERE@GitHub . 我分叉了这个插件,并进行了更新HERE @ GitHub So it now injects a script that alert's "hey" on the current website in the tab when this widget is clicked. 因此,当单击此小部件时,它现在在选项卡中的当前网站上注入一个警报的“嘿”脚本。 (If you don't see it when install addon you have to customize toolbars and drag from there to either addonbar or toolbar) (如果在安装插件时看不到它,则必须自定义工具栏,然后从那里拖动到插件栏或工具栏)

There were three steps to do this as seen in commit history of the updated addon 如更新的插件的提交历史所示,执行此操作需要三个步骤

  1. Create test.js 创建test.js

    Created a folder which I will expose to resource later on and created the script file with code I want to inject 创建了一个文件夹,稍后将向资源展示该文件夹,并使用我要注入的代码创建脚本文件

  2. Update chrome.manifest 更新chrome.manifest

    Create a resource out of the content/injectable/ folder, this way anything in that folder can be injected into sites without "Permission Security" exceptions. 从content / injectable /文件夹中创建资源,这样,该文件夹中的所有内容都可以注入到站点中,而无“ Permission Security”例外。

  3. Update browserOverlay.js 更新browserOverlay.js

    We defined in our chrome.manifest file that the location to the folder with our script is "resource://xulschoolhello-injections/" so lets tell it to inject the script file from. 我们在chrome.manifest文件中定义了包含脚本的文件夹的位置为“ resource:// xulschoolhello-injections /”,因此,让它告诉它从中注入脚本文件。 I also edited out the innerHTML being set to "rawr". 我还编辑了被设置为“原始”的innerHTML。

This template HERE - _ff-addon-template-BootstrapWatchHostEventListenerInjectFiles shows how to inject files by setting contentaccessible=true in the chrome.manifest file. 此处的模板-_ff-addon-template-BootstrapWatchHostEventListenerInjectFiles显示了如何通过在chrome.manifest文件中设置contentaccessible=true来注入文件。

Install that and then navigate to bing.com and scroll to the bottom you will see the inject iframe with blue background and injected image. 安装它,然后导航到bing.com并滚动到底部,您将看到带有蓝色背景和已注入图像的注入iframe。 You will also see the inject script run on load of bing, it will be an alert box. 您还将看到在bing负载下运行注入脚本,这将是一个警报框。

However there is an issue with the template above, I can't set the src of the iframe to the chrome path it throws this security error: 但是上述模板存在问题,我无法将iframe的src设置为会引发此安全错误的chrome路径:

Security Error: Content at http://www.bing.com/?FORM=Z9FD1 may not load or link to chrome://bootstrap-watch-host-event-listener-inject-files/content/_inject-website.htm. 安全错误: http ://www.bing.com/?FORM=Z9FD1上的内容可能无法加载或链接到chrome://bootstrap-watch-host-event-listener-inject-files/content/_inject-website.htm 。

This is weird and I don't understand it. 这很奇怪,我听不懂。 The whole point of contentaccessible=true in the chrome.manifest is so this security error doesn't happen. chrome.manifest中contentaccessible = true的重点是这样,因此不会发生此安全错误。 Other devs reading this please help. 其他开发人员在阅读本文时请提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM