Chrome扩展程序:获取URL时扩展程序上下文无效 [英] Chrome Extension: Extension context invalidated when getting URL

查看:316
本文介绍了Chrome扩展程序:获取URL时扩展程序上下文无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的chrome扩展程序文件夹中的一些图像检索到网站DOM,每次重新加载扩展程序时,都会出现扩展上下文无效"错误.当我执行"chrome.storage.local.set"时,也会发生同样的事情.

I'm retrieving some images from one of my chrome extension folders to the website DOM and everytime I reload the extension I'm getting a 'Extension Context Invalidated' error. Same thing happens when I do a 'chrome.storage.local.set'.

做一些研究后,我意识到这个错误与在这个答案上很好地解释了的事实有关,但是自从我我的内容脚本和background.js之间没有消息传递,我想知道为什么会发生这种情况.

Doing some research I have realized that this error has to do with the facts well explained on this answer but since I'm not messaging between my content script and the background.js I wonder why this happens.

这是我脚本的一部分(通过popup.js中的chrome.tabs.executeScript注入),在那里我得到了错误,我基本上是将图像从我的扩展文件夹之一注入到网站DOM中:

This is the part of my script (injected via chrome.tabs.executeScript in the popup.js) where I'm getting the error, I'm basically injecting images from one of my extension folders to the website DOM:

  for (let k = 0; k < incomingChatTags.length; k++) {
    let normalHolderTag = $(incomingChatTags[k]).text().toLowerCase();
    switch (normalHolderTag) {
      case "vip":
        $(incomingChatTags[k]).addClass("ce-vip");
        priorityVal += 300;
        break;
      case "rg":
        $(incomingChatTags[k]).addClass("ce-rg");
        priorityVal += 240;
        break;
      case "accountclosure":
        $(incomingChatTags[k]).addClass("ce-accountclosure");
        priorityVal += 200;
        break;
      case "21com":
        let logo21 = chrome.extension.getURL("/images/21_thumb.png");
        $(incomingChatTags[k]).html('<img src="' + logo21 + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "caxino":
        //the console shows the error here and not before....¿why? 
        let logoCaxino = chrome.extension.getURL(
          "/images/caxino_thumb.png"
        );
        $(incomingChatTags[k]).html('<img src="' + logoCaxino + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "justspin":
        let logoJustSpin = chrome.extension.getURL(
          "/images/wildz_thumb.png"
        );
        $(incomingChatTags[k]).html('<img src="' + logoJustSpin + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "neonvegas":
        let logoNeonVegas = chrome.extension.getURL(
          "/images/neonVegas_thumb.jpg"
        );
        $(incomingChatTags[k]).html('<img src="' + logoNeonVegas + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "nitrocasino":
        let logoNitroCasino = chrome.extension.getURL(
          "/images/nitroCasino_thumb.jpg"
        );
        $(incomingChatTags[k]).html(
          '<img src="' + logoNitroCasino + '" />'
        );
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "snabbis":
        let logoSnabbis = chrome.extension.getURL(
          "/images/snabbis_thumb.png"
        );
        $(incomingChatTags[k]).html('<img src="' + logoSnabbis + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "sb.bet":
        let logoSB = chrome.extension.getURL("/images/sb_thumb.png");
        $(incomingChatTags[k]).html('<img src="' + logoSB + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "wildz":
        let logoWildz = chrome.extension.getURL("/images/wildz_thumb.png");
        $(incomingChatTags[k]).html('<img src="' + logoWildz + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
      case "wishmaker":
        let logoWishMaker = chrome.extension.getURL(
          "/images/wishmaker_thumb.png"
        );
        $(incomingChatTags[k]).html('<img src="' + logoWishMaker + '" />');
        $(incomingChatTags[k]).addClass("ce-tag-logo");
        break;
    }
    $(incomingChat).attr("data-priority", priorityVal);
    $(incomingChat).find(".numbers_cell").text(priorityVal);
  }

推荐答案

我想解释一下我为保持这一点所做的事情:

I want to explain what I did in order to keep this:

基本上,我将所有导致此错误的内容包装为:

Basically I wrap everything that was creating this error with :

typeof chrome.app.isInstalled !== "undefined""

说实话,我仍在努力弄清楚为什么这样做.我了解每次更新扩展程序时,内容脚本仍会注入网站(例如,获取dom元素,将数据存储到chrome本地存储等),但我不明白为什么该声明避免了这种情况的发生,因为该扩展程序仍安装在我的浏览器中,我只是在更新文件.

I still trying to figure out why this is working, to be honest. I understand that every time I update the extension the content scripts are still injected on the website (in my case, grabbing dom elements, storing data to chrome local storage, etc) but I don't get why that statement avoid this to happen since the extension is still installed on my Browser, I'm just updating the files.

如果有人可以更深入地说明为什么这对我有用,请在此处亮点.

If someone can give a more in-depth explanation of why this is working for me please shine a light here.

这篇关于Chrome扩展程序:获取URL时扩展程序上下文无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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