简体   繁体   English

Chrome扩展程序-在视图和扩展程序之间共享数据

[英]Chrome extension - Share data between view and the extension

I'm building a chrome extension to debrid and stream a link on my raspberry pi (running Kodi). 我正在构建一个Chrome扩展程序,以在我的树莓派(运行Kodi)上整理和流式传输链接。

So far, it's working well. 到目前为止,它运行良好。 I can give a link : 我可以给一个链接:

在此处输入图片说明

and it's debrided + streamed on Kodi. 并在Kodi上进行了清创+流式传输。

I would like to make it easier to use. 我想使其更易于使用。

Instead of pasting the link into the app, I made a little script to search on a given type of page if a link can be debrided via Realdebrid and is so, the script add a button to directly stream it : 如果没有可以通过Realdebrid清除链接的链接,我制作了一个小脚本来搜索给定类型的页面,而不是将链接粘贴到应用程序中,因此,该脚本添加了一个按钮来直接对其进行流处理:

在此处输入图片说明

The problem here is : 这里的问题是:
When the user click on the button, how can I pass the link to my extension ? 当用户单击按钮时,如何将链接传递到我的扩展程序? I would like to have it in my extension because I can display the link into history. 我想在扩展程序中使用它,因为我可以显示历史链接。

I tried something like that : 我尝试过这样的事情:

  • from the view : 从视图: 在此处输入图片说明

  • from the extension : 从扩展名: 在此处输入图片说明

But it's not working. 但这不起作用。

I think I should use messages but I don't understand how. 我认为我应该使用消息,但我不知道如何使用。

It's an open source project that you can find on Github . 这是一个开源项目,您可以在Github上找到它。 As it's not working yet you won't find the code I shared in the 2 images before. 由于尚无法使用,因此您之前在两张图片中都找不到我共享的代码。 It's just on my computer, not pushed. 它只是在我的计算机上,没有被推送。

Any help would be really appreciated ! 任何帮助将非常感激 !

Thanks ! 谢谢 !

EDIT 1 : 编辑1:

When I click on the button injected in the page, I get this error : 当我单击页面中注入的按钮时,出现此错误:
在此处输入图片说明

Which leads to this line : 导致这一行: 在此处输入图片说明

EDIT 2 : 编辑2: 在此处输入图片说明

If I keep the app open while I click on the button it's working. 如果在单击按钮时保持打开状态,则该应用程序正常运行。 But I do not want to have to launch the app of course, the goal is to click on the button only. 但我当然不想启动该应用程序,目标是仅单击该按钮。

Popup script is loaded when you open the app by a user action ie clicking on the app icon. 通过用户操作(即单击应用程序图标)打开应用程序时,将加载Popup script Until then, it is not available to you. 在此之前,您将无法使用它。 As such you are getting this error because when you send a request the listener should be available and in your case its not there. 因此,您将收到此错误,因为在发送请求时,侦听器应该可用,而在您的情况下,侦听器不可用。

Now, the easiest way to achieve this would be to send the request from the content script to the background page which is available even when the app is not open. 现在,最简单的方法是将请求从内容脚本发送到后台页面,即使应用程序未打开,该页面仍然可用。

  1. Inject the button using the content script and send a message to the background page (called event pages) on click event of the button. 使用内容脚本注入按钮,并在按钮的单击事件上将消息发送到后台页面(称为事件页面)。

  2. Add an event listener in your background page which will listen to the request from the content script and store the link in the local storage. 在您的后台页面中添加一个事件侦听器,该侦听器将侦听来自内容脚本的请求并将链接存储在本地存储中。

  3. When your app opens just load the link from the local storage and display your links in the history. 当您的应用打开时,只需从本地存储中加载链接,然后在历史记录中显示您的链接。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM