Chrome Extension: Despite adding it to web_accessible_resources, the resource remains unavailable

I'm currently developing a Chrome Extension with React/Redux and utilizing Webpack. As part of the optimization process, I have started migrating resources to a separate file using the WebPack DLLReference plugin.

After generating the dll/dll.vendor.js file, I encountered an issue where it was successfully loaded in the popup window but not in the injected content.

Despite adding it to the manifest:

"web_accessible_resources": [
    "dll/dll.vendor.js"
],

The file is accessible through the path: chrome-extension:///dll/dll.vendor.js, but unfortunately, it's not being recognized in the injected content as seen in Developer Tools -> Sources, leading to missing object errors.

Everything functioned properly before implementing the DLLReferencePlugin.

You can find my DLL webpack config file here: https://pastebin.com/z9RjRUqm

And my Content webpack config file here: https://pastebin.com/0Niw2Fqm

If you check the error image here, it highlights the line causing issues:

module.exports = vendor;

Interestingly, this problem only occurs in the content environment, whereas the popup works fine with the same code snippet. It seems that the extension toolbar injection into the page might be the root cause.

This leads me to suspect that the code relying on dll.vendor is executed prior to the actual injection on the page. I am actively exploring ways to prevent this from occurring. Any insights or suggestions on debugging this issue?

UPDATE

Upon further investigation, I discovered that the discrepancy arises because the code dependent on dll.vendor executes before the page's injection takes place. I am seeking solutions to mitigate this timing conflict.

Answer №1

When it comes to content scripts, one crucial aspect to understand is the Isolated World paradigm:

Content scripts operate within an isolated world, where they can interact with the DOM of the page they are injected into but cannot access JavaScript variables or functions created by that page. Each content script runs as if there is no other JavaScript code on the same page, and vice versa – the page's JavaScript cannot reach or manipulate the content script's scope.

So, why does this matter? If you're inserting a <script> tag into the page's DOM, the script will run in the page's context rather than the content script's context. This practice is commonly known as using "page level" or "injected" scripts, as explained in this post on Stack Overflow.

However, this might not align with your goal. You likely want the script to execute within the content script's environment so that you can leverage its libraries. In such cases, you have a few options:

  1. If you consistently require the same script configuration, specifying the scripts as an array in the manifest file would be the recommended approach. The order in which these scripts load is essential:

    "js" : [ "library.js", "actual_code.js" ]
    
  2. If you need to dynamically load code at runtime from an existing content script, you could prompt the background page to inject it programmatically for you. Ensure to sequence the calls correctly if you are already following this process.

  3. While not the most optimal solution from a technical standpoint, another workaround involves XHR-ing your internal resource and then employing eval() within the content script context.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Button will be disabled unless a value is selected in the dropdown menu

I am currently dealing with a code issue where the button is disabled on page load when the dropdown value is empty. However, even after selecting a value from the populated database dropdown, the button remains disabled. Jquery: <script> $(doc ...

Is it a graphics card malfunction or a coding complication? Delving into THREE.JS WebGL

Recently, I created a cool scene using three.js that was running perfectly until today. Strangely, without any changes to the code, I started encountering an error in every major browser - Chrome, Firefox, and Edge. The error message I am seeing is: THREE. ...

Creating a Duplicate of the Parent Element and its Child Elements using jQuery

Here is a code snippet I have that adds a new paragraph when a button is clicked. Currently, the script clones the "sub" div and appends it to the "main" div. However, the issue is that it only copies the content of the "inner" div within the "sub" div ins ...

Steps for triggering the material-ui menu to appear on hover of a button

I attempted to implement the following code without success. I was able to achieve it using plain CSS, but I need to utilize the makeStyles function provided by material-ui. My goal is to display a drop-down list of items when a user hovers over the butto ...

The video player will only start playing after the source has been changed if it was already in play

I am facing an issue with my video player that has thumbnails. The problem is that the video player does not start playing the selected video unless the video is already playing. This means I have to hit the play button first, and then select the thumbnail ...

Filtering an array dynamically by utilizing an array of conditions

Can jQuery-QueryBuilder be used to filter an array? This amazing plugin generates a unique array of conditions: condition:"AND" rules: { field:"name" id:"name" input:"select" operator:"equal" type:"string" value:"Albert" } I have ...

Utilize JQuery to identify and select every parent element, then retrieve the height of the first child element and adjust the height of the

Recently, I developed a PHP script that pulls news and case posts from a database. The HTML structure used for displaying these posts is as follows: <a href='/post/placeholder'> <div class='col nopadding col12-12 counter'> ...

Encountering problems with ajax technology

As a newcomer to Laravel and Ajax, I am facing an issue with displaying the state of the selected country in a dropdown list. Although the data is successfully fetched from Laravel and received through Ajax, I am struggling to dynamically add it to the H ...

When Controller Scope Goes Missing in ng-repeat

Upon glancing at my code, it should be evident that I am a newcomer to the world of Angular. I am currently developing an application that enables users to search for text, queries a database whenever the value in the text input changes, and displays a li ...

Saving integer data retrieved from DynamoDB in a React Native application

I need to store a user's progress by saving a value in a DynamoDB. While storing the value is not difficult, accessing this value for use in my application has proven to be quite challenging. When using dynamoDBWrapper.getItem(params), where the para ...

How can you modify Jquery show_hide so that the page automatically scrolls to the bottom of the concealed field when the button is clicked?

On my website, there is a button that reveals a map when clicked. The button is visible when the page loads, but once clicked, the map slides open and goes out of sight since the page loads at the top as usual. My goal is to have the page automatically scr ...

Encountered an issue while attempting to send a POST request using AngularJS $

I am facing an issue with accessing the POST method from my server. Whenever I log the response, it always returns status=0. Can anyone help me out or provide some advice? Note: I have tested the method in Postman and it works fine. Below is the code snip ...

Stryker score caused the Jenkins build to fail

Is there a way to configure the Jenkins pipeline so that it fails when the stryker score is below X? This is the stryker configuration: config.set({ mutator: "javascript", mutate: [...], testRunner: "jest", jest: { projectType: "n ...

Changing the Position of HTML Scripts in React

I am facing an issue where I need to relocate an external script within a specific section of my page. However, I am unable to access the CSS attributes associated with this item. It seems that it is displayed as an iFrame, making it difficult to modify th ...

A guide on utilizing the .getLastRow() function in Google Apps Script

I am relatively new to Google Script and I am currently working on a piece of code that is giving me some trouble. My goal is to have the program loop through a range of cells in a spreadsheet, printing them out until it reaches the last row. Despite try ...

"Enhance your data visualization with Highcharts Windbarb and effortless AJAX data

Alright. Let's rephrase a question that hasn't been answered yet. I've got a chart below with data loading dynamically via ajax. A simplified version (without ajax) of this graph works well as shown in this jsfiddle. The code below represe ...

Retrieving all selected checkboxes in AngularJS

I am a beginner in angular js and here is my template: <div class="inputField"> <h1>Categories</h1> <div> <label><input type="checkbox" id="all" ng-model="all" ng-change="checkAll();" ng-true-value="1">A ...

"Step-by-step guide on uploading multiple images to a Node server and storing them in

Hey everyone! I'm currently working on a project using React and MongoDB. Users are required to register and login before accessing the app. Once logged in, they can input their name, number, and images through a form. However, I've encountered a ...

Is there a way to conceal the parameters in the PHP GET method?

How to convert PHP GET method URL to a cleaner format? example.com/example.php?name=45 to example.com/example.php/45 Is it possible to achieve this using the .htaccess file? ...

Exploring the blur() function in JavaScript through cold calling

There is a specific line of code that I need help with. document.getElementById("firstName").addEventListener("blur", validateField); Additionally, there is this block of code: validateField = (event) => { const el = event.targe ...