将WebExtension从Chrome移植到Firefox? [英] Porting WebExtension from Chrome to Firefox?

查看:272
本文介绍了将WebExtension从Chrome移植到Firefox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个未打包的Chrome扩展程序,只是我电脑上的一个目录。我发现我应该能够将它移植到Firefox, a>。



我跟着移植谷歌浏览器扩展程序MDN指南,发现我的清单文件是完美的。

然后我按照如何执行 Firefox中的临时安装

然而,当我点击目录中的任何文件时, 。扩展名不加载。有什么建议?我知道扩展在Chrome中工作正常,加载没有错误。
$ b

manifest.json

  {
manifest_version:2,
name:ER,
description:P,
version: 1.0,

browser_action:{
default_icon:icon.png,
default_popup:popup.html
},

background:{
scripts:[background.js]
},

content_scripts:[
{
matches:[SiteIwant],
js:[ChromeFormFill.js],
run_at:document_idle



$ b $ permissions
$ cookie
$ active



$ b $ ,
tabs,
https://ajax.googleapis.com/
],
externally_connectable:{
matches:[ Chrome浏览器.File.js文件中的SiteIwant]
}
}

em>:
$ b

  // JavaScript source c 
console.log(内部内容);
console.log(chrome.runtime.id);
document.getElementById(ID)。value = chrome.runtime.id.toString();

Background.js

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request.data ===info){
console.log(Recieving Info);
return true;
}
});
$ b chrome.tabs.create(
{
url:'myUrl'
active:true
},function(tab){
chrome.tabs.executeScript(tab.id,{file:'Run.js',runAt:document_idle});
});

Run.js alert('嗨')



当我尝试在Firefox上加载它时,什么都不会发生。

解决方案

问题:


$ b

manifest.json :



你有两行(一个尾随,一个没有;在语法上正确):

 matches:[SiteIwant] 

SiteIwant必须是有效的匹配模式。我假设这是从有效的东西改变了混淆您正在使用的网站。我用:

 matches:[*://*.mozilla.org/*] 



Background.js 中:



#1

  url:'myUrl' 
active:true

需要:

  url:'myUrl',
active:true

[请注意'myUrl'之后的]另外,myUrl需要是有效的网址。我使用:

  url:'http://www.mozilla.org/',











$

  chrome.tabs.executeScript(tab.id,{file:'Run.js',runAt:document_idle}); 

在Firefox 48中,该行在选项卡可用之前执行。这是一个错误。它已在 Firefox开发人员版每晚。您将需要其中的一个来测试/继续开发。



ChromeFormFill.js中的问题:



#3另一个Firefox 48错误:chrome.runtime.id is undefined。这个问题已经在Developer Edition和Nightly中得到了解决。

我假设你的HTML有一个ID = 'ID' code>。如果不是,你的 document.getElementById(ID)将是 null`。您不检查是否返回的值是有效的。

一旦所有这些错误得到解决,并在Firefox Nightly或开发版下运行,它工作正常。

I made a working Chrome extension that is not packaged and is just a directory on my computer. I found out that I should be able to port this to Firefox rather easily.

I followed the "Porting a Google Chrome extension" guide on MDN and found that my manifest file is perfect.

I then followed the instructions on how to perform "Temporary Installation in Firefox" of the extension.

However, when I click on any file inside the directory, nothing happens. The extension doesn't load. Any advice? I know the extension works in Chrome fine and loads without error.

manifest.json:

{
  "manifest_version": 2,
  "name": "ER",
  "description": "P",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

  "background": {
    "scripts": [ "background.js" ]
  },

  "content_scripts": [
    {
      "matches": [ "SiteIwant" ],
      "js": [ "ChromeFormFill.js" ],
      "run_at":  "document_idle" 

    }
  ],

  "permissions": [
    "*://*/*",
    "cookies",
    "activeTab",
    "tabs",
    "https://ajax.googleapis.com/"
  ],
  "externally_connectable": {
    "matches": ["SiteIwant"]
  }
}

ChromeFormFill.js:

// JavaScript source c
console.log("inside content");
console.log(chrome.runtime.id);
document.getElementById("ID").value = chrome.runtime.id.toString();

Background.js

chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
      if (request.data === "info") {
          console.log("Recieving Info");
          return true;
      }
 });

chrome.tabs.create(
{
    url: 'myUrl' 
    active: true
}, function (tab) {
    chrome.tabs.executeScript(tab.id, { file: 'Run.js', runAt: "document_idle" });
});

Run.js will just alert('hi').

It just won't do anything when I try to load it on Firefox; nothing will happen.

解决方案

Issues:

In manifest.json:

You have two lines (one with a trailing ,, one without; both syntactically correct):

"matches": ["SiteIwant"]

"SiteIwant" needs to be a valid match pattern. I'm assuming that this was changed away from something valid to obfuscate the site that you are working with. I used:

"matches": [ "*://*.mozilla.org/*" ]

In Background.js:

#1 The lines:

    url: 'myUrl' 
    active: true

need to be:

    url: 'myUrl',
    active: true

[Note the , after 'myUrl'.] In addition, myUrl needs to be a valid URL. I used:

    url: 'http://www.mozilla.org/',

#2 A Firefox 48 bug: Your line:

chrome.tabs.executeScript(tab.id, { file: 'Run.js', runAt: "document_idle" });

In Firefox 48 this line is executed prior to the tab being available. This is a bug. It is fixed in Firefox Developer Edition and Nightly. You will need one of those to test/continue development.

Issues in ChromeFormFill.js:

#3 another Firefox 48 bug: "chrome.runtime.id is undefined". This is fixed in Developer Edition and Nightly.

I'm going to assume your HTML has an element with an ID = 'ID'. If not, your document.getElementById("ID") will benull`. You don't check to see if the returned value is valid.

Once all those errors were resolved, and running under Firefox Nightly, or Developer Edition, it works fine.

这篇关于将WebExtension从Chrome移植到Firefox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆