Accessing an array in another JS file - Chrome extension

Hi, I am doing a chrome extension for some practice, which is a simple site blocker. I have two different JS files that need to share the same array containing the URLs to block.

  • background.js handles webRequests and blocks websites.
  • options.html - the options page where users can edit the URLs that need to be blocked, calls storeURL.js so that it can store all the items in the list on the html page in an js array. There are no problems so far.

However, I cannot access the array inside storeURL.js from background.js. They seem to be called in two separate cases. If I try to include both storeURL.js and background.js inside my background.html, this will launch two completely separate storeURL.js scripts.

How should I do it? Should I store the contents of the array from storeURL.js to a file?

Some examples:

background.js

chrome.webRequest.onBeforeRequest.addListener(
    blocking,["blocking"]);
    { urls: blockedURLS, types: [] }, ["blocking"]);

storeURL.js populates an array based on a list in options.html

var blockedURLS = [];
        $('li').each(function(i, elem) {
            blockedURLS.push($(elem).text());
        });

blocked by urls in my background.js is displayed as undefined. I also tried pointing the options.html file to background.js and just including all js in one file with the following:

<script type="text/javascript" src="background.js"></script>

However, when I do this, it seems that the second background.js is being called, and not just indicating an already running one.

, , , , . JS/chrome.

, , !

EDIT: background.js. .

var blockedURLS = ["hi"] ;

    var list = document.getElementById('list');
    $("#saveList").click(function(e) {
        e.preventDefault();
        localStorage.setItem('todoList', list.innerHTML);
        alertify.success("You have saved your list.");

        //Pushes each element in the list from options.html into the urls array
        blockedURLS = [];
        $('li').each(function(i, elem) {
            blockedURLS.push($(elem).text());
        });
        alert("Currently blocked:\n" + blockedURLS);
    });
    $("#reset").click(function(e) {
        e.preventDefault();
        localStorage.clear();
        location.reload();
    });

    loadToDo();

    function loadToDo() {
      if (localStorage.getItem('todoList')){
        list.innerHTML = localStorage.getItem('todoList'); 
    }
    }

, , , options.html , "" URL-, . . blockURL .

+4
2

. ... , Storage api API Chrome. , ,

myValue = chrome.storage.local.get("MyValue")

, , , myValue = storage.myValue storage.myValue = "something"

github

CoffeeScript, javascript bin. javascript /, , CoffeeScript, .

, , . , .

, , , , - :-), .

EDIT: , / Script, , .

... ; -)

0

:

var bg = chrome.extension.getBackgroundPage();

blockURS:

alert(bg.blockedURLS);

chrome.extension.getBackgroundPage().blockedURLS = [];

.

, , , .

0

All Articles