在Chrome扩展中与孤立的世界(扩展js和网页js)进行沟通 [英] Communicate between isolated worlds (extension js and webpage js) on chrome extension

查看:130
本文介绍了在Chrome扩展中与孤立的世界(扩展js和网页js)进行沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然而,我想访问一些关于chrome扩展和页面的js文件,它们不能直接访问对方,并且它们运行在孤立的世界中。在页面上调用这些函数并从插件中调用这些函数。

我不想自己创建这些函数的版本。



我想知道这是否可能...会欣赏所有答案。



编辑:功能在背景页面上。它是一个浏览器动作扩展。

更多信息:基本上,我有一个上下文菜单,它创建一个选项卡来将网址提交到页面。但是,重新打开页面会使许多选项卡和打开很多页面需要时间。因此,如果页面的一个实例已经打开,我愿意直接从扩展中调用页面上的JavaScript函数。我可以访问页面的权限。



TL; DR :需要在页面上调用JavaScript函数的方法假设您知道要调用其功能的页面的选项卡ID,那么您可以使用以下命令来创建该选项卡: 可以在您的扩展页面使用 chrome.tabs.update(tabId,{url:'javascript:functionNameHere()'}); 。这是因为javascript:URLs 映射到主要(即页面,非隔离)世界中的脚本执行。


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.

解决方案

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.

这篇关于在Chrome扩展中与孤立的世界(扩展js和网页js)进行沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆