简体   繁体   中英

How can I edit a manifest.json file from a JS file?

I'm making a Google Chrome extension that parses a Twitter feed's source code when the user inputs the handle. I'm aware that you cannot extract an external site's source code unless you give it permission in the manifest.json file. However, the URL in my case will vary based on the Twitter handle that the user inputs, so how can I edit the manifest file directly from my popup.js script?

For example, I want something like this:

{
    "permissions": [
        "http://www.twitter.com/" + handle + "/with_replies"
    ]
}

in my manifest.json file and

var handle = "CNN";
$(document).ready(function() {
    $.get("http://www.twitter.com/" + handle + "/with_replies", function(data) {
        $("#body").text(data);
    });
});

to return the content of the Twitter feed for CNN.

************************************************ UPDATE ************************************************

So to test to see if my code works, I changed my manifest.json file to include http://www.twitter.com/* as a permission, and I changed my jQuery code to:

$.get("https://twitter.com/", function(data) {
    $("h5").text(data);
    });
});

This is inside an event listener that listens for a button click, so this should change the text of the h5 element to the source of the webpage, but it doesn't. I've tried replacing https://www.twitter.com/* with https://www.twitter.com/ and it still doesn't work. But if I replace the jQuery code with something simple like $("h5").text("Hello world!"); , it works fine. Is there something wrong with my jQuery code to extract the page's content?

This is what the permissions section in my manifest file looks like:

"permissions": [
 "activeTab",
 "storage",
 "https://twitter.com/*"
]

I doubt you could edit the file for security reasons. However, it you could have multiple content_scripts executing on different occasions. So if your condition is meet on that website, it will execute.

"content_scripts": [
  {
    "matches": ["https://www.google.com/*"],
    "js": ["jquery.js", "myGoogleScript.js"]
  },
  {
    "matches": ["http://www.example.com/*"],
    "js": ["jquery.js", "myExampleScript.js"]
  }
]

For more go to: Possible to have multiple content scripts for different functions?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM