Communicate between isolated worlds (extension js and webpage js) on chrome extension

对着背影说爱祢 提交于 2019-12-18 18:39:23

问题


As you may know, js files on chrome extensions and pages cannot directly access each other and they run on isolated worlds.

However, I want to access some of the functions on a page and call those functions from the plugin.

And I do not want to make my own version of those functions.

I'm wondering if this is possible... Would appreciate all the answers.

EDIT: The functions are on the background page. Its a browser action extension.

more info: Basically, I have a context menu which creates a tab to submit url to a page. however, re-opening the page makes many tabs and opening so many pages takes time. So I am willing to call the javascript function on the page directly from the extension if one instance of the page is open already. And I have access to the page permission-wise.

TL;DR: Need a way to call a javascript function on a page (without copying it locally) from an extension.


回答1:


Assuming you know the tab ID of the page whose function you want to call, you can use chrome.tabs.update(tabId, {url: 'javascript:functionNameHere()'}); from your extension page. This works because javascript: URLs are mapped to script execution in the main (i.e. page, not isolated) world.



来源:https://stackoverflow.com/questions/8422223/communicate-between-isolated-worlds-extension-js-and-webpage-js-on-chrome-exte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!