Call function in content script from web page

4,918 views
Skip to first unread message

Michal Drwal

unread,
Feb 19, 2018, 9:02:53 AM2/19/18
to Chromium-Extensions-Announce
Hi
How can i call function defined in content script from my web page?

in content script i have UruchomWybierzPlik()

function UruchomWybierzPlik(){
 //if (e.shiftKey && e.ctrlKey && e.keyCode == trigger_key)
 {
  alert("Contentscript is sending a message to background script: '" + contentScriptMessage  + "'");
 // chrome.extension.sendRequest({Function: "WybierzPlik"});
 chrome.runtime.sendMessage({Function: "WybierzPlik"}, function(response) {
  //console.log(response.farewell);
});
 
 }
}

in my wab page i have

function WybierzPlikChrome(callBackRez)
{
   
   UruchomWybierzPlik();
  
}

UruchomWybierzPlik() is not defined

how call UruchomWybierzPlik(); from WybierzPlikChrome()  ???

PhistucK

unread,
Feb 19, 2018, 10:02:02 AM2/19/18
to Michal Drwal, Chromium-Extensions-Announce
​You cannot call content scripts functions directly from web page scripts. They live in a different, separate world for increased security.

You have a few options -
- Add them as listeners to custom events (window.addEventListener('​UruchomWybierzPlik", UruchomWybierzPlik)) in the content script and dispatch the event from the web page script (window.dispatchEvent(new Event('UruchomWybierzPlik'))). If you want to return a value, you could use the same way, but the other way around (dispatch the event from the content script, listen to the event from the web page script), or put the value in the DOM somewhere (using element.textContent or using element.setAttribute).
- Inject a <script> to the page. That script will run in the context and world of the page and could interact with the other web page scripts normally.


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/ffc37a0d-dc86-4393-b468-fe1a47d42f1e%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Michal Drwal

unread,
Feb 20, 2018, 4:28:31 AM2/20/18
to Chromium-Extensions-Announce, drwal....@gmail.com
Hi

Now i try call function by events.

In my web page is:

function WybierzPlik()
{
        window.postMessage({ type: "ELDOK_EXT_MSG", params: "UruchomWybierzPlik()" }, "*");
}

in my content script :

window.addEventListener("message", function(e) {
       
        alert('aaaa');
       
        if (e.source != window) {
            return;
        }

        var data = JSON.parse(e.data);
        if (data.type && (data.type == "ELDOK_EXT_MSG")) {
              UruchomWybierzPlik();
       
        }
    });

My manifest :
{
  "name": "Plugin el-Dok",
  "description": "Plugin el-Dok",
  "version": "0.7",
 
  "background": { "scripts": ["background.js"], "persistent": false },
 
   "content_scripts":[{
       "matches": [ "*://*/*" ],
       "js": [ "jquery-2.1.4.min.js", "content.js" ],
      "run_at": "document_idle"
    }],
    "content_security_policy": "script-src 'self'; object-src 'self'",
     "description": "Komponenty dla el-Dok System ",

  "manifest_version": 2,
  "permissions": [ "nativeMessaging","tabs" ,"activeTab", "management" ],
  "externally_connectable": {
   "matches": ["*://*.eldok.pl/*","*://localhost/*"  ]
  
 
}
 
}

But addEventListener don't start. Where is the problem?



PhistucK

unread,
Feb 20, 2018, 4:42:08 AM2/20/18
to Michal Drwal, Chromium-Extensions-Announce
This simple test code works for me (using the debugger) -
Content script -
window.addEventListener('message', function () {console.log('0')});
Web page script -
window.postMessage({type: 'f', params: 'd()'}, "*");
0 is printed.

Make sure you operate in the same frame (or define all_frames: true in your manifest).


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
Reply all
Reply to author
Forward
0 new messages