开发者

Chrome extension issues with contact scripts

开发者 https://www.devze.com 2023-01-14 02:05 出处:网络
I think I am having trouble visualizing what code goes where and what requests and responses go where.

I think I am having trouble visualizing what code goes where and what requests and responses go where.

Let's say I want to alter all <img> tags in the body of a document. I am sure I would adjust there styling in the inject.js file,...but would I still need to send a request to background.html? If so,...I am not sure what the response would be.

Thanks for your help!

(CONT)

Does this code make any sense? I am trying to grab <div> tags a开发者_StackOverflownd make them disappear. Then reload them one by one (fifo) after each press of the command and semi-colon keys. Here is the .js file I wanted to inject.

var hideShowElements = document.getElementsByTagName('div'); 

var queue = [];

var active = false; 

function hide(){

    for (var i = 0; i < hideShowElements.length; i++) {

    hideShowElements[i].style.visibility == "hidden";
    queue.push(hideShowElements[i]);

    }           
}

hide();

document.onkeydown = function(k){
    if(k.isCtrl || k.keyCode == 91) active = true;
    if(active && k.keyCode == 186){

        for (var i = 0; i < queue.length; i++){

             queue[i].style.visibility == "visible";

        }
    }

}

document.onkeyup = function(k){
   if(k.isCtrl || k.keyCode == 91) active = false;
}


Content scripts (injected) have some limitations, as stated in the api:

Content scripts have some limitations. They cannot:

  • Use chrome.* APIs (except for parts of chrome.extension)
  • Use variables or functions defined by their extension's pages
  • Use variables or functions defined by web pages or by other content scripts
  • Make cross-site XMLHttpRequests

So if you need any of those you would need to send a request to a background page and ask to perform this action for you, otherwise background page is not required.

In your case you don't need to sent anything to a background page.

0

精彩评论

暂无评论...
验证码 换一张
取 消