简体   繁体   English

为什么我的扩展程序在 Mozilla Firefox 上运行而不是在 Google Chrome 上运行?

[英]Why is my extension working on Mozilla Firefox and not on Google Chrome?

So I developed this pretty small extension for one particular website.所以我为一个特定的网站开发了这个非常小的扩展。

The goal of the whole addon, was to change styling of it (it looks like it was from 1990s by default).整个插件的目标是改变它的样式(默认情况下它看起来像是从 1990 年代开始的)。 At first I wrote a Greasemonkey script, and it was working great, but later, when my friends wanted to use it as well I decided to make it into a browser addon, to make it flawless.一开始我写了一个 Greasemonkey 脚本,效果很好,但后来,当我的朋友也想使用它时,我决定把它做成一个浏览器插件,让它完美无瑕。

So the transition from GreaseMonkey script to Mozilla Firefox addon wasn't hard at all.所以从 GreaseMonkey 脚本到 Mozilla Firefox 插件的过渡一点也不难。 Everything works, just like a GreaseMonkey script.一切正常,就像 GreaseMonkey 脚本一样。 But when I tried to change it into Chrome extension - no results.但是当我尝试将其更改为 Chrome 扩展时 - 没有结果。 As if the extension wasn't there.好像扩展不存在一样。

I don't think I was using any code, that was "Firefox" friendly, as GreaseMonkey code worked both on Mozilla and Chrome.我认为我没有使用任何对“Firefox”友好的代码,因为 GreaseMonkey 代码可以在 Mozilla 和 Chrome 上运行。


Contents of my manifest.json :我的manifest.json的内容:

{
  "manifest_version": 2,
  "name": "XXXXXXXXXX",
  "short_name": "XXXXX",
  "author": "XXXXXXXXX",
  "version": "1.0.1",

  "description": "Changes stylistically Shinden.pl website.",

  "icons": {
    "48": "icons/48.png",
    "96": "icons/96.png"
  },

  "content_scripts": [
    {
      "matches": ["*://shinden.pl/*"],
      "exclude_matches": ["*://shinden.pl/animelist/*"],
      "run_at": "document_start",
      "css": ["resources/static.css"]
    },

    {
      "matches": ["*://shinden.pl/*"],
      "exclude_matches": ["*://shinden.pl/animelist/*"],
      "js": ["contentScripts/documentStart.js"],
      "run_at": "document_start"
    },

    {
      "matches": ["*://shinden.pl/*", "file:///C:/Users/Wojciech/Desktop/index.html"],
      "exclude_matches": ["*://shinden.pl/animelist/*"],
      "js": ["contentScripts/documentEnd.js"],
      "run_at": "document_end"
    },

    {
      "matches": ["*://shinden.pl/*"],
      "exclude_matches": ["*://shinden.pl/animelist/*"],
      "js": ["contentScripts/documentIdle.js"],
      "run_at": "document_idle"
    }
  ]


}

As you can see - addon firstly adds a .css file into the browser, to change static parts of it.如您所见 - 插件首先将.css文件添加到浏览器中,以更改其中的 static 部分。 Then I have 3 scripts (for now documentStart.js and documentIdle.js are empty), which are loaded at 3 different times.然后我有 3 个脚本(现在documentStart.jsdocumentIdle.js是空的),它们在 3 个不同的时间加载。

So here's my documentEnd.js : (sorry, but I've been using some native language variable names)所以这是我的documentEnd.js :(抱歉,我一直在使用一些本地语言变量名)

var bialy         = "#e7ecef";
var niebieski_100 = "#274c77";
var niebieski_50  = "#6096ba";
var niebieski_25  = "#a3cef1";
var szary_25      = "#4d5057";
var szary_100     = "#272727";

////////////////////////////////////////////////////////////////////////////////////////////////////

var dokument = document.documentElement;
dokument.style.setProperty('--bialy', bialy);
dokument.style.setProperty('--niebieski_100', niebieski_100);
dokument.style.setProperty('--niebieski_50', niebieski_50);
dokument.style.setProperty('--niebieski_25', niebieski_25);
dokument.style.setProperty('--szary_25', szary_25);
dokument.style.setProperty('--szary_100', szary_100);

function hexToRGBA(hex, alpha) {
    var r = parseInt(hex.slice(1, 3), 16),
        g = parseInt(hex.slice(3, 5), 16),
        b = parseInt(hex.slice(5, 7), 16);

    if (alpha) {
        return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
    }
    else {
        return "rgb(" + r + ", " + g + ", " + b + ", 1)";
    }
}

function fadeHex(hex, brightness){
    var r = parseInt(hex.slice(1, 3), 16),
        g = parseInt(hex.slice(3, 5), 16),
        b = parseInt(hex.slice(5, 7), 16);

    var biggest = r;
    if (g > biggest) biggest = g;
    if (b > biggest) biggest = b;
    var ratio = parseInt(brightness)/biggest;

    r = ((r * 10 * ratio) / 10) | 0;
    g = ((g * 10 * ratio) / 10) | 0;
    b = ((b * 10 * ratio) / 10) | 0;

    var newR = r.toString(16);  if (newR.length < 2) { newR = "0" + newR; }
    var newG = g.toString(16);  if (newG.length < 2) { newG = "0" + newG; }
    var newB = b.toString(16);  if (newB.length < 2) { newB = "0" + newB; }

    return '#' + newR + newG + newB;
}

dokument.style.setProperty('--niebieski_25_30', hexToRGBA(niebieski_25, 0.3));
dokument.style.setProperty('--niebieski_50_30', hexToRGBA(niebieski_50, 0.3));
dokument.style.setProperty('--niebieski_50_50', hexToRGBA(niebieski_50, 0.5));

//////////////////////////////////////////////

function changePowSerImagesSizes(){
    var parentsOfImages = document.getElementsByClassName("relation_t2t");
    for(var i=0; i<parentsOfImages.length; i++){
        var image = parentsOfImages[i].getElementsByTagName("img");
        var oldSource = image[0].src;
        var animeID = oldSource.slice(-10);

        if(oldSource.slice(-15) != "/res/other/placeholders/title/100x100.jpg".slice(-15)) image[0].src = "/res/images/225x350/" + animeID;
        else image[0].src = "/res/other/placeholders/title/225x350.jpg";

    }
}
changePowSerImagesSizes();

////////////////////////////////////////////////////////////////////////////////////////////////////

function changePostacieImagesSizes(){
    var parentOfCharacters = document.getElementsByClassName("person-list");
    if (parentOfCharacters == null) return;
    if (parentOfCharacters.length == 0) return;                                                                 //so we dont access nullpointer         // gdy nie ma takich obrazkow na ekranie
    var parentsOfImages = parentOfCharacters[0].getElementsByClassName("person-character-item");

    for(var i=0; i<parentsOfImages.length; i++){
        var image = parentsOfImages[i].getElementsByTagName("img")[0];
        var oldSource = image.src;
        var characterID = oldSource.slice(-10);

        if(oldSource.slice(-15) != "/res/other/placeholders/title/36x48.jpg".slice(-15)) image.src = image.src = "/res/images/100x100/" + characterID;
        else image.src = "/res/other/placeholders/title/100x100.jpg";
    }
}
changePostacieImagesSizes();
////////////////////////////////////////////////////////////////////////////////////////////////////

function changeRekomImagesSizes(){
    var biggerParent = document.getElementsByClassName("page-content page-anime-recommendations")[0];
    if (biggerParent == null) return;

    var parentsOfImages = biggerParent.getElementsByClassName("media media-item");
    for(var i=0; i<parentsOfImages.length; i++){
        var image = parentsOfImages[i].getElementsByTagName("img");
        var oldSource = image[0].src;
        var animeID = oldSource.slice(-10);

        if(oldSource.slice(-15) != "/res/other/placeholders/title/100x100.jpg".slice(-15)) image[0].src = "/res/images/225x350/" + animeID;
        else image[0].src = "/res/other/placeholders/title/225x350.jpg";

    }
}
changeRekomImagesSizes();

////////////////////////////////////////////////////////////////////////////////////////////////////

function changeAniListImagesSizes(){
    var biggerParent = document.getElementsByClassName("anime-list")[0];
    if (biggerParent == null) return;

    biggerParent = biggerParent.getElementsByTagName("article")[0];
    if (biggerParent == null) return;


    var parentsOfImages = biggerParent.getElementsByClassName("div-row");
    for(var i=0; i<parentsOfImages.length; i++){


        var aElement = parentsOfImages[i].getElementsByClassName("cover-col")[0].getElementsByTagName("a")[0];
        if (aElement == null) return;


        var aStyle = aElement.style.backgroundImage;
        var animeID = aStyle.slice(-12).slice(0,10);

        if(animeID != "00x100.jpg") aElement.style.backgroundImage = "url(/res/images/225x350/" + animeID + ")";
        else aElement.style.backgroundImage = "url(/res/other/placeholders/title/225x350.jpg)";

    }
}
changeAniListImagesSizes();

////////////////////////////////////////////////////////////////////////////////////////////////////

function changeGlowneImagesSizes(){
    var parentOfImages = document.getElementsByClassName("info-aside aside-title")[0];
    if (parentOfImages == null) return;

    var image = parentOfImages.getElementsByClassName("info-aside-img")[0];
    var oldSource = image.src;

    var animeID = oldSource.slice(-10);

    if(oldSource.slice(-15) != "/res/other/placeholders/title/225x350.jpg".slice(-15)) image.src = "/res/images/genuine/" + animeID;

    image.setAttribute("onerror", 'this.onerror=null; this.src="' + oldSource + '";');  // to prevent very few anime that doesnt have 'genuine' image from being broken
}

changeGlowneImagesSizes();

////////////////////////////////////////////////////////////////////////////////////////////////////


function changeVideoPlayerSize(){

    var parentOkienka = document.getElementById("player-block");
    if (parentOkienka == null) return;

    var videoOkienko = parentOkienka.getElementsByTagName("iframe")[0];

    videoOkienko.width = "100%";
    setTimeout(() => {
        var width = videoOkienko.clientWidth;
        var height = width * 9 / 16;
        videoOkienko.height = height.toString();
    }, 300);
}
window.addEventListener('resize', changeVideoPlayerSize);

And here's the thing - I don't know which part of the code creates the problem.事情就是这样——我不知道代码的哪一部分造成了问题。

Is it a problem with my manifest.json ?我的manifest.json有问题吗? But if so, then why did my extension get approved in Chrome Web Store?但如果是这样,那为什么我的扩展程序在 Chrome Web 商店中获得批准? link 关联

Also - if you want to compare with a working addon - here's Firefox version link另外-如果您想与工作插件进行比较-这里是 Firefox 版本链接

Thanks to @wOxxOm I found that both .js and .css files are actually injected into the website, but not working correctly.感谢@wOxxOm,我发现.js.css文件实际上都注入了网站,但无法正常工作。

After some research I found this post at Chromium forum: https://bugs.chromium.org/p/chromium/issues/detail?id=776891经过一番研究,我在 Chromium 论坛上找到了这篇文章: https://bugs.chromium.org/p/chromium/issues/detail?id=776891

Chromium "style sheets injected by extensions are now treated as user style sheets", thus making them less important than author created style sheets. Chromium “由扩展注入的样式表现在被视为用户样式表”,因此它们不如作者创建的样式表重要。


In my case I had to manually add !important near all the stylesheet rules and it repaired the problem, which is inconvenient, but there's no way around it.在我的情况下,我必须在所有样式表规则附近手动添加!important并且它修复了问题,这很不方便,但没有办法解决它。

暂无
暂无

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

相关问题 为什么Cut,Copy,Paste在Mozilla Firefox和Google Chrome浏览器中不起作用? - why Cut,Copy,Paste is not working in Mozilla Firefox and Google Chrome browser? Java代码可在Mozilla Firefox中运行,但不能在Google Chrome中运行 - Javascript code working in Mozilla Firefox but not in Google Chrome 网站滑块在Google Chrome和Mozilla firefox上不起作用 - website slider is not working on Google Chrome and Mozilla firefox table.rows.length在mozilla firefox中不起作用,但在Google Chrome中起作用 - table.rows.length not working in mozilla firefox but working in google chrome 代码块在 mozilla firefox 中工作,但在 Chrome 中不工作 - Code block working in mozilla firefox but not in Chrome Window.close(); 在Chrome和Mozilla Firefox中无法使用 - The Window.close(); is not working in Chrome and Mozilla Firefox 编写Google Chrome / Mozilla扩展程序所需的帮助 - Help needed on writing extension for Google Chrome/Mozilla 后退按钮在Internet Explorer中不起作用,在mozilla firefox和google chrome中工作良好 - back button not working in internet explorer, working good in mozilla firefox and google chrome 为什么Google Gauge Chart可以在Chrome浏览器中运行而不能在Firefox中运行? - Why is a Google Gauge Chart working in Chrome but not in Firefox? 为什么此功能= true在Google Chrome中运行但在Firefox中无效? - Why is this function = true working in Google Chrome but not Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM