执行chrome.extension.getBackgroundPage()时引发错误

编程入门 行业动态 更新时间:2024-10-28 08:17:09
本文介绍了执行chrome.extension.getBackgroundPage()时引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理我的第一个扩展,并试图创建一个简单的扩展以在页面上注入可拖动的div。这很好用,但我想在后台页面上保留div的位置(我也尝试了本地存储,但想明白为什么这不起作用)。

I am working on my first extension and am trying to create a simple extension to inject a draggable div on a page. That works nicely, but I want to preserve the location of the div on the background page (I'm also trying out local storage, but want to understand why this isn't working).

我不需要按钮,所以还没有创建popup.html文件,我相信这完全是可选的。它到目前为止工作到目前为止只是注入JavaScript文件。

I do not need a button so have not created a popup.html file, which, I believe, is entirely optional. It certainly has worked so far just injecting javascript files.

但是,现在执行chrome.extension.getBackgroundPage()时会出现以下错误:

However, I now get the following error thrown when executing chrome.extension.getBackgroundPage():

未捕获错误:chrome.extension.getBackgroundPage只能用于扩展进程。有关更多详细信息,请参阅内容脚本文档。

Uncaught Error: chrome.extension.getBackgroundPage can only be used in extension processes. See the content scripts documentation for more details.

内容脚本文档似乎没有发现我的方法有任何问题: code.google/chrome/extensions/content_scripts.html

The content scripts documentation did not seem to identify anything wrong with my approach: code.google/chrome/extensions/content_scripts.html

这里是我正在使用的一个redacted清单:

Here is a redacted manifest I am using:

{ "name": "My helper", "version": "1.0", "description": "Tastes great", "background_page": "background.html", "content_scripts": [ { "matches":["page.of.interest/*"], "run_at":"document_idle", "js":[ "jquery.js", "jquery-ui-1.8.17.custom.min.js", "my_content_script.js"], "css": [ "my_content_script.css" ] } ], "permissions": [ "background" ] }

所以我在my_content_script.js中运行这个声明。这是否被视为扩展过程的一部分?我只能在popup.html(或其他可能的?)文件上运行它吗?

So I am running this statement inside "my_content_script.js". Is this NOT considered part of the extension process? Can I only run this on a popup.html (or other possibly?) file?

如果是这种情况,那么使用localstorage比使用localstorage更容易尝试通过dom与扩展进程进行通信。

If this is the case, then maybe it is easier to just use localstorage rather than trying to communicate through the dom with the extension process.

希望我已经清楚,尽管我对这些概念一无所知。

Hope I've been clear despite my ignorance about some of these concepts.

推荐答案

从一个 content_script 但是因为 content_script 具有更多面向它正在运行的页面的权限是不允许的访问。然而,您可以使用文档提及一些方法。

I don't think the docs explicitly say you can't use chrome.extension.getBackgroundPage() from a content_script but because the content_script has permissions more oriented with the page it is being run on it isn't allowed access. The docs do mention a few methods you can use however.

与其他chrome。* API不同的是,部分chrome.extension可以被内容脚本使用:

Unlike the other chrome.* APIs, parts of chrome.extension can be used by content scripts:

您必须使用邮件传递 a>在 background_page 和 content_script 之间进行通信。

You will have to use message passing to communicate between the background_page and the content_script.

更多推荐

执行chrome.extension.getBackgroundPage()时引发错误

本文发布于:2023-10-24 09:17:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   chrome   extension   getBackgroundPage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!