如何以编程方式打开Chrome扩展程序页面 [英] How to open chrome extension page programmatically

查看:300
本文介绍了如何以编程方式打开Chrome扩展程序页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是该扩展程序的新成员.我想以编程方式打开Chrome扩展程序页面.例如:

I am a new one for the extension. I want to open a chrome extension page programmatically. Eg:

chrome-extension://njlkegdphefeellhaongiopcfgcinikh/options.html

当我单击网页按钮或链接时,我想打开特定的扩展名标签.

When I click the web page button or link I want to open the particular extension tab.

我尝试了很多方法.直接使用javascript等多种方式进行调用.但是我找不到合适的解决方案.任何人都有任何想法.

I tried many ways. Directly call using javascript and so many ways. But I couldn't find the proper solution. Anybody have any idea.

(我的目标是使用JavaScript打开扩展标签)

(My target is using JavaScript to open the extension tab)

推荐答案

以下是解决方法之一:

  1. 确保您的内容脚本在您单击按钮的页面上运行.
  2. 当您单击网页上的按钮时,请从内容脚本中添加事件侦听器,然后在事件列表器中将消息传递给后台.

  1. Make sure your content script runs on the page on which you are clicking on the button.
  2. When you click on button on web page, add event listener from content script and in the event listerner pass a messgae to background.

chrome.runtime.sendMessage({message: 'buttonClicked'}, 
  function() { 
    /* callback */ 
  });

  • 在您的后台脚本中,收听来自内容脚本的消息.

  • In your background script, listen to the message from content script.

    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
      if (request.message == 'buttonClicked') {
      // Create a new tab with options page
     }
    });
    

  • 要使用options.html页面创建新标签页,您可以这样做

  • To create a new tab with options.html page, you can do this

    chrome.tabs.create({
      active: true,
      url:  'options.html'
    }, null);
    

  • 这篇关于如何以编程方式打开Chrome扩展程序页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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