How to open chrome extension page programmatically

微笑、不失礼 提交于 2019-12-22 18:13:53

问题


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.

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

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


回答1:


Here is one of the solutions:

  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 */ 
      });
    
  3. 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
     }
    });
    
  4. To create a new tab with options.html page, you can do this

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


来源:https://stackoverflow.com/questions/22761819/how-to-open-chrome-extension-page-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!