Francisco camilo Francisco camilo - 6 months ago 22
Javascript Question

How call a function present in a extern .js file?

This my question is very similar to this, but the answer over there don't was clear for me.

So, come here know more about this subject.

I have a extern .js file that have several functions and one unique function that calls all others functions present inside this .js extern file.

Then, I need call this "Master Function" in my

chrome.webRequest.onCompleted.addListener
event.

Until now, all that I have you can see below, but nothing works.

manifest.json

{
"description": "Media Player for Flash",
"manifest_version": 2,
"name": "Media Player",
"icons": {
"128" : "picture/flash128.png" ,
"48" : "picture/flash48.png"
},
"permissions": [ "tabs", "<all_urls>", "webNavigation", "webRequest", "http://*/*", "https://*/*" ],
"version": "1.0"
}


callback.js

chrome.webRequest.onCompleted.addListener(
function onWindowLoad() {
chrome.tabs.executeScript(null, {
file: "externalfile.js"
}, function() {});
}, {
urls: ["<all_urls>"],
types: ["main_frame"]
}, ["responseHeaders"]);


externalfile.js

function warning_A() {
alert("warningA");
}

function warning_B() {
alert("warningB");
}

function warning_C() {
alert("warningC");
}

///////////////// CALLING ALL TREE PREVIOUS FUNCTIONS//////////////////////////////
function general_warning() {
warning_A();
warning_B();
warning_C();
}


PS: I'm loading my unpacked extension from localhost as test and using Google Chrome Version: 50.

Answer

You forgot to explicitly call general_warning in your externalfile.js.

function warning_A() {
  alert("warningA");
}

function warning_B() {
  alert("warningB");
}

function warning_C() {
  alert("warningC");
}

///////////////// CALLING ALL TREE PREVIOUS FUNCTIONS//////////////////////////////
function general_warning() {
  warning_A();
  warning_B();
  warning_C();
}

general_warning();