【问题标题】:Chrome extensions: How can I pass form variables to a url in a newly created tab?Chrome 扩展:如何将表单变量传递给新创建的选项卡中的 url?
【发布时间】:2011-03-24 00:18:27
【问题描述】:
我正在开发我的第一个 chrome 扩展程序,并使用基于图像的上下文菜单项来捕获给定图像的 URL,然后希望在新选项卡中的特定 URL 处显示该图像。因此,我需要将单击的图像的 URL(使用 srcUrl)传递给特定的脚本,然后该脚本可以在该页面上呈现它。是否可以在 chrome.tabs.create() 调用中执行 HMLHttpRequest,还是必须以其他方式完成?
感谢您的帮助。
【问题讨论】:
标签:
xmlhttprequest
google-chrome-extension
【解决方案1】:
您需要创建一个包含该脚本的 HTML 页面并将其放入您的扩展文件夹中。然后您可以将图像 url 作为 GET 参数传递给它:
chrome.tabs.create({url: "local.html?img_url=...");
如果 url 参数不够,你也可以使用chrome.tabs.sendRequest() 与该页面进行通信:
chrome.tabs.create({url: "local.html", function(tab){
chrome.tabs.sendRequest(tab.id, {img_url: "local.html?img_url=...");
));
使用该页面中的请求侦听器:
chrome.extension.onRequest.addListener(function(request) {
console.log(request.img_url);
});