简体   繁体   English

mozilla firefox扩展:创建一个JavaScript库并在HTML页面中使用它

[英]mozilla firefox extension: creating a javascript library and using it in a HTML page

I created a library in a firefox extension : 我在firefox扩展中创建了一个库:

./components/main.js
./skin/icon.png
./install.rdf
./chrome.manifest

my main.js would use define a class calling a mozilla TCPScoket ( https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket ) 我的main.js将使用定义一个称为mozilla TCPScoket( https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket )的类

function MYLib() {}    
MYLib.prototype.doWork = function(arg1,arg2) { /* do something with TCPScoket */}

How should I use my library on the client side in the HTML ? 如何在HTML的客户端使用我的库?

I'm looking for something like (?): 我正在寻找类似(?)的东西:

<html><head><script>
var mylib = MozillaApiThing.createNewInstance("chrome://myextension/", "MYLib");
 ....
 </script>(...)

chrome or resource (not in restartless) URLs are available as direct URLs (ie you can enter them into address-bar and they show up). chromeresource (不是非重启)URL可以作为直接URL使用(即,您可以将它们输入到地址栏中,然后它们就会显示出来)。 Therefore you can use it as a simple script src 因此,您可以将其用作简单的脚本src

<script src="chrome://myextension/components/jsfile.js"></script>

Alternatively, you can read the JS file (ie with XMLHttpRequest or FileUtils.jsm or osfile.jsm) and then insert the content into the page with createElement('script') , textContent and appendChild() 或者,您可以读取JS文件(即使用XMLHttpRequest或FileUtils.jsm或osfile.jsm),然后使用createElement('script')textContentappendChild()将内容插入页面

Alternatively, you can try The message manager 或者,您可以尝试消息管理器

Note: Inserted local script into the page will have the content page scope (no access to browser scope API) 注意:在页面中插入本地脚本将具有内容页面范围(无法访问浏览器范围API)

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

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