0

I have been developing a small project in Microsoft Edge Extension, I have a script that I got from a reddit which executes the script to locate answer of MSN Shopping Game. I wanted to add that to my Extension and I am currently unsuccessful to do so.

Here is my function of background.js:


function msnGameScript() {
  
  const code = `
  (function()
{
    if(!document.location.href.startsWith("https://www.msn.com/en-us/shopping")){
        alert("Invalid page!");
        return;
    }
    
    var shoppingPageChildren = null;
    try
    {
        shoppingPageChildren = document.getElementsByTagName("shopping-page-base")[0].shadowRoot.children[0]
            .getElementsByTagName("shopping-homepage")[0].shadowRoot.children[0]
            .getElementsByTagName("msft-feed-layout")[0].shadowRoot.children;
    }
    catch(e)
    {
        alert("Script error...\nMake sure the page is fully loaded.");
        return;
    }
    
    var msnShoppingGamePane = null;
    for(i = 0; i <= shoppingPageChildren.length - 1; i++){
        if(shoppingPageChildren[i].getAttribute("gamestate") == "active")
            msnShoppingGamePane = shoppingPageChildren[i];
    }
    var answerSelectorInterval = (msnShoppingGamePane == null ? 0 : setInterval(() => 
    {
        if(msnShoppingGamePane.gameState == "active" && msnShoppingGamePane.selectedCardIndex != msnShoppingGamePane._correctAnswerIndex)
            msnShoppingGamePane.selectedCardIndex = msnShoppingGamePane._correctAnswerIndex;
        
        if(msnShoppingGamePane._dailyLimitReached){
            clearInterval(answerSelectorInterval);
        }
    }, 500));
    alert(answerSelectorInterval == 0 ? "Unable to locate shopping game...\nTry scrolling down to it." : "Shopping game located!\nEnjoy :)")
})();
`;

chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
  if (tabs.length > 0) {
    const tabId = tabs[0].id;
    chrome.tabs.executeScript(tabId, { code });
  } else {
    console.error("No active tab found.");
  }
});

When I press a button the function is getting executed as Listener and Events are set perfectly, the code works when put in console of Developer Option but not working from extension. Help me to solve this and implement this.

I tried using scripts and different methods but am unsuccessful as I'm beginner in Javascript and Extension development.

Github Project Here is my github link for the extension, but the new button and script is not yet implemented on github project. on popup.html I have added this code:<button type="button" id="game-button">Game</button> on popup.js I have added this code:

var gameButton = document.getElementById("game-button");
gameButton.addEventListener("click",function (event){
    chrome.runtime.sendMessage({ type: "game-script" });
  });

on background.js I have this code:


chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
  
  if (message.type === "start-searches") {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      if (tabs.length > 0) {
        activeTabId = tabs[0].id; // Store the tab ID of the active tab
        // performSearches(message.numSearches, message.searchType, message.searchGen);
      } else {
        console.error("No active tab found.");
      }
    });
    performSearches(message.numSearches, message.searchType, message.searchGen);
  } else if (message.type === "stop-searches") {
    clearInterval(intervalId);
    result="";
    console.log("Stopped performing searches.");
  }else if (message.type === "game-script") {
    msnGameScript();
  }
});

function msnGameScript(); is provided above, Hope this information is enough.

RoN
  • 13
  • 2

0 Answers0