最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension: Extension context invalidated when getting URL - Stack Overflow

programmeradmin1浏览0评论

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'.

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.

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 < iningChatTags.length; k++) {
    let normalHolderTag = $(iningChatTags[k]).text().toLowerCase();
    switch (normalHolderTag) {
      case "vip":
        $(iningChatTags[k]).addClass("ce-vip");
        priorityVal += 300;
        break;
      case "rg":
        $(iningChatTags[k]).addClass("ce-rg");
        priorityVal += 240;
        break;
      case "accountclosure":
        $(iningChatTags[k]).addClass("ce-accountclosure");
        priorityVal += 200;
        break;
      case "21":
        let logo21 = chrome.extension.getURL("/images/21_thumb.png");
        $(iningChatTags[k]).html('<img src="' + logo21 + '" />');
        $(iningChatTags[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"
        );
        $(iningChatTags[k]).html('<img src="' + logoCaxino + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "justspin":
        let logoJustSpin = chrome.extension.getURL(
          "/images/wildz_thumb.png"
        );
        $(iningChatTags[k]).html('<img src="' + logoJustSpin + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "neonvegas":
        let logoNeonVegas = chrome.extension.getURL(
          "/images/neonVegas_thumb.jpg"
        );
        $(iningChatTags[k]).html('<img src="' + logoNeonVegas + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "nitrocasino":
        let logoNitroCasino = chrome.extension.getURL(
          "/images/nitroCasino_thumb.jpg"
        );
        $(iningChatTags[k]).html(
          '<img src="' + logoNitroCasino + '" />'
        );
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "snabbis":
        let logoSnabbis = chrome.extension.getURL(
          "/images/snabbis_thumb.png"
        );
        $(iningChatTags[k]).html('<img src="' + logoSnabbis + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "sb.bet":
        let logoSB = chrome.extension.getURL("/images/sb_thumb.png");
        $(iningChatTags[k]).html('<img src="' + logoSB + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "wildz":
        let logoWildz = chrome.extension.getURL("/images/wildz_thumb.png");
        $(iningChatTags[k]).html('<img src="' + logoWildz + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "wishmaker":
        let logoWishMaker = chrome.extension.getURL(
          "/images/wishmaker_thumb.png"
        );
        $(iningChatTags[k]).html('<img src="' + logoWishMaker + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
    }
    $(iningChat).attr("data-priority", priorityVal);
    $(iningChat).find(".numbers_cell").text(priorityVal);
  }

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'.

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.

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 < iningChatTags.length; k++) {
    let normalHolderTag = $(iningChatTags[k]).text().toLowerCase();
    switch (normalHolderTag) {
      case "vip":
        $(iningChatTags[k]).addClass("ce-vip");
        priorityVal += 300;
        break;
      case "rg":
        $(iningChatTags[k]).addClass("ce-rg");
        priorityVal += 240;
        break;
      case "accountclosure":
        $(iningChatTags[k]).addClass("ce-accountclosure");
        priorityVal += 200;
        break;
      case "21":
        let logo21 = chrome.extension.getURL("/images/21_thumb.png");
        $(iningChatTags[k]).html('<img src="' + logo21 + '" />');
        $(iningChatTags[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"
        );
        $(iningChatTags[k]).html('<img src="' + logoCaxino + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "justspin":
        let logoJustSpin = chrome.extension.getURL(
          "/images/wildz_thumb.png"
        );
        $(iningChatTags[k]).html('<img src="' + logoJustSpin + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "neonvegas":
        let logoNeonVegas = chrome.extension.getURL(
          "/images/neonVegas_thumb.jpg"
        );
        $(iningChatTags[k]).html('<img src="' + logoNeonVegas + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "nitrocasino":
        let logoNitroCasino = chrome.extension.getURL(
          "/images/nitroCasino_thumb.jpg"
        );
        $(iningChatTags[k]).html(
          '<img src="' + logoNitroCasino + '" />'
        );
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "snabbis":
        let logoSnabbis = chrome.extension.getURL(
          "/images/snabbis_thumb.png"
        );
        $(iningChatTags[k]).html('<img src="' + logoSnabbis + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "sb.bet":
        let logoSB = chrome.extension.getURL("/images/sb_thumb.png");
        $(iningChatTags[k]).html('<img src="' + logoSB + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "wildz":
        let logoWildz = chrome.extension.getURL("/images/wildz_thumb.png");
        $(iningChatTags[k]).html('<img src="' + logoWildz + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
      case "wishmaker":
        let logoWishMaker = chrome.extension.getURL(
          "/images/wishmaker_thumb.png"
        );
        $(iningChatTags[k]).html('<img src="' + logoWishMaker + '" />');
        $(iningChatTags[k]).addClass("ce-tag-logo");
        break;
    }
    $(iningChat).attr("data-priority", priorityVal);
    $(iningChat).find(".numbers_cell").text(priorityVal);
  }
Share Improve this question asked Aug 21, 2020 at 10:46 LowtruxLowtrux 2083 gold badges15 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Upon reloading extension, Your old content scripts will continue to reside in opened pages until you refresh them. However their context with respect to Chrome extension would be lost.

Just one liner to avoid the situation:

if(chrome.runtime.id == undefined) return;

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"”

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.

发布评论

评论列表(0)

  1. 暂无评论