I want to read the json file in the extension with the extension

Asked 1 years ago, Updated 1 years ago, 305 views

The whole thing you want to do

Extensions detect notifications implemented on the website and block notifications when url in the block list is transition destination url
I would like to read the json file (block list) in the extension function by the extension function and compare the url registered in the json file with the transition destination url obtained.
The method can be fetch or xmlhttprequest.

What I want to do this time

Want to read json files in extensions within extensions
content.js if(url==="blacklist url"){} in content.js I want to get the contents of the json file above this process

What I don't know

  • Can the content.js of manifest content_scripts handle asynchronous processing?
  • content.js blocking.json in relative path but not read

If blocked.json cannot be obtained in the first place, the console will issue an error.
And "I can't get it"." The url with GET https://demosite/blocked.json404 appears to be getting an error trying to browse to the site's json file instead of the json file in the extension.

I look forward to hearing from you.

Currently, the directory structure is as follows:

extension
 ├-- js
 │   -- --blocked.json
 │   -- --content.js
 │   --inject.js
 └-- manifest.json

manifest

{
  "name": "Blocking fishing sites",
  "description": "Control method for web push notification by browser extension",
  "version": "1.0",
  "manifest_version"—2,
  "web_accessible_resources":[
    "js/content.js",
    "js/blocked.json"
  ],
  "content_scripts":[
    {
      "matches": ["<all_urls>",
      "run_at": "document_start",
      "js": ["js/inject.js" ]
    }
  ],
  "permissions": [
    "*://*/*",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "webNavigation"
  ]
}

blocked.json

{
  "keyword": [
    "test",
    "test_1",
    "test_2",
    "test_3"
  ],

  "url": [
    "https://example.com/",
    "https://example_1.com/",
    "https://example_2.com/",
    "https://example_3.com/"
  ]
}

inject.js

console.log("--inject script--");
const script = document.createElement("script");
script.src=chrome.runtime.getURL("js/content.js");
( document.head || document.documentElement).insertAdjacentElement("afterbegin", script);


console.log("--load json--");
const blockedPath="js/blocked.json";
consts= document.createElement("script");
s.jsonUrl = chrome.runtime.getURL (blockedPath);

console.log("blockedPath:", s.jsonUrl);

fetch(s.jsonUrl)
    .then(response=>response.json())
    .then(data=>{
      console.log(data);
      console.log(data.url[0]);
    });

content.js

...omitted...

window.open=function(open){
  return function(url, name, features) {
    console.log("url:", url);

    // Compare the contents of json with url
    if(url==="blacklist url"){
      console.log("url blocked!");
      return null;
    } else{
      return open.call(window, url, name, features);
    }

  };
}(window.open);

javascript json chrome-extension

2022-11-17 03:58

1 Answers

Relative URLs cannot be used for this purpose because they are relative to the URL of the page you are viewing.

Specify blocked.json as web_accessible_resources and .fetch().


2022-11-17 08:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.