Stopping content script in chrome extension

google-chrome, google-chrome-devtools, google-chrome-extension, javascript, jquery

Solution

`window.close()` is for closing a window, so no wonder it does not work.

"Unloading" a content-script is not possible, but if you want to remove an element (e.g. your `div`) from the DOM, just do:

elem.parentNode.removeChild(elem);

(Whether you bind this behaviour to a link/button in your `<div>` or have the browser-action, trigger an event in the background-page that sends a message to the corresponding content-script which in turn removes the element is up to you. (But clearly the former is much more straight-forward and efficient.)

If you, also, want your script to stop performing some other operation (e.g. handling `click` events) you could (among other things) set a flag variable to `false` (when removing the `<div>`) and then check that flag before proceeding with the operation (e.g. handling the event):

var enabled = true;
document.addEventListener('click', function (evt) {
    if (!enabled) {
        /* Do nothing */
        return;
    }

    /* I am 'enabled' - I'll handle this one */
    evt.preventDefault();
    ...

/* In order to "disable" the content-script: */
div.parentNode.removeChild(div);
enabled = false;

Notes:

If you plan on re-enabling the content-script upon browser-action button click, it is advisable to implement a little mechanism, where the background-page sends a message to the content-script asking it to re-enable itself. If the content-script is indeed injected but disabled, it should respond back (to confirm it got the message) and re-enable itself. If there is no response (meaning this is the first time the user clicks the button on this page, the background-page injects the content script.

If it is likely for the content script to be enabled-disabled multiple times in a web-pages life-cycle, it would be more efficient to "hide" the `<div>` instead of removing it (e.g.: `div.style.display = 'none';`).

If you only need to disable event handler, instead of using the `enabled` flag, it is probably more efficient to keep a reference to the listener you want to disable and call `removeEventListener()`.

E.g.:

function clickListener(evt) {
    evt.preventDefault();
    ...
}
document.addEventListener('click', clickListener);

/* In order to "disable" the content-script: */
div.parentNode.removeChild(div);
document.removeEventListener('click', clickListener);

Problem

I have developed chrome extension , I have added , ``` chrome.browserAction.onClicked.addListener ``` which will start the script once clicked on , the script in turn will add a div at the bottom of the web page on the tab the browser action is clicked , All I have to do is , I need to add a close link which will stop the content script and close the div at the bottom , I have tried windows.close() , self.close() but nothing seems to work ,I would atleast want it to work in a way that 2nd time some one clicks on browser action, the script should stop. Here is my code, background.js ``` chrome.browserAction.onClicked.addListener( function() { chrome.tabs.executeScript( { file: 'myscript.js' } ); }); ``` myscript.js ``` document.body.appendChild(div); document.addEventListener("click", function (e) { e.preventDefault(); var check = e.target.getAttribute("id"); var check_class = e.target.getAttribute("class"); if(check=="ospy_" || check=="ospy_id" || check=="ospy_text" || check=="ospy_el" || check=="ospy_class" || check=="ospy_name" || check=="ospy_href" || check=="ospy_src"|| check=="ospy_wrapper"|| check=="ospy_style"|| check=="ospy_rx"|| check=="ospy_con"|| check_class=="ospy_td"|| check=="ospy_main_tab"|| check_class=="ospy_tab" || check_class=="ospy_ip"|| check_class=="ospy_lab") { } else{ document.getElementById('ospy_id').value = ""; document.getElementById('ospy_class').value = ""; document.getElementById('ospy_el').value = ""; document.getElementById('ospy_name').value = ""; document.getElementById('ospy_style').value = ""; document.getElementById('ospy_href').value = ""; document.getElementById('ospy_text').value = ""; document.getElementById('ospy_src').value = ""; document.getElementById('ospy_con').value = ""; document.getElementById('ospy_').value = ""; document.getElementById('ospy_rx').value = ""; var dom_id=e.target.getAttribute("id"); // var dom_id = e.target.id.toString(); var dom_name = e.target.name.toString(); var dom_class = e.target.className.toString(); // var dom_class = this.class; var dom_html = e.target.innerHTML; var dom_href = e.target.getAttribute("href"); var dom_text = e.target.text; var dom_el= e.target.tagName; var dom_src= e.target.src; //var XPATH = e.target.innerHTML; var rel_xpath = ""; var field =""; var field_value = ""; field="id"; field_value = dom_id; rel_xpath = dom_el+"[@"+field+"='"+field_value+"']"; if(dom_id == null){ field="href"; field_value= dom_href; //var rel_xpath = dom_el+"[contains(text(), '"+dom_text+"')]"; rel_xpath = dom_el+"[@"+field+"='"+field_value+"']"; if(dom_href==null || dom_href=="#") { field="src"; field_value= dom_src; rel_xpath = dom_el+"[@"+field+"='"+field_value+"']"; //rel_xpath = "nope nothing"; if(dom_src==null) { var rel_xpath = dom_el+"[contains(text(), '"+dom_text+"')]"; if(dom_text=="") { field="class"; field_value= dom_class; rel_xpath = dom_el+"[@"+field+"='"+field_value+"']"; } } } } var con_xpath = ""; var con_xpath = dom_el+"[contains(text(), '"+dom_text+"')]"; if(dom_text==null) { con_xpath = "NA"; } var css ="color: "; css += getComputedStyle(e.target).color; css +="\nWidth: "; css += getComputedStyle(e.target).width; css +="\nHeight: "; css += getComputedStyle(e.target).height; css +="\nbg: "; css += getComputedStyle(e.target).background; css +="\nfont: "; css += getComputedStyle(e.target).font; css +="\nvertical-align: "; css += getComputedStyle(e.target).verticalalign; css +="\nmargin: "; css += getComputedStyle(e.target).margin; var node = getXPath(e.target.parentNode); document.getElementById('ospy_id').value = dom_id; document.getElementById('ospy_class').value = dom_class; document.getElementById('ospy_el').value = dom_el; document.getElementById('ospy_name').value = dom_name; document.getElementById('ospy_style').value = css; document.getElementById('ospy_href').value = dom_href; document.getElementById('ospy_text').value = dom_text; document.getElementById('ospy_src').value = dom_src; document.getElementById('ospy_').value = node; document.getElementById('ospy_rx').value =rel_xpath; document.getElementById('ospy_con').value =con_xpath; }}, false); ```

Original source