在扩展名鼠标单击上打开本地.html [英] Open a local .html on extension mouse click

查看:76
本文介绍了在扩展名鼠标单击上打开本地.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我的第一个chrome扩展程序.单击扩展程序图标后,希望chrome打开一个新标签页,并在其中打开我创建的本地.html页面.

按照Google文档中的说明,我创建了以下内容:

manisfest.json

{
  "manifest_version": 2,

  "name": "Notes",
  "version": "1.0",

  "browser_action": {
    "default_icon": "ninjaicon.png",
    "default_popup": "notes.html"
},

  "background": {
    "scripts": ["background.js"]
},

  "permissions": [
    "tabs"
 ]
}

background.js

chrome.browserAction.onClicked.addListener(function(activeTab){
  chrome.tabs.create({'url': chrome.extension.getURL('notes.html')}, function(tab)
  });
});

当我按下扩展程序图标时,会弹出一个包含"notes.html"内容的弹出窗口,但是没有打开任何新标签.

我应该如何正确做?一直在这里寻找很多解决方案,但是没有用. 我只想创建一个扩展,单击该扩展会打开一个带有本地Web应用程序的新标签.

感谢您的回答.

解决方案

要使其正常工作,notes.html应该位于扩展程序的根目录中.

将清单文件中的default_popup删除为

单击浏览器操作图标时将触发onClicked事件,但是如果浏览器操作具有弹出窗口(因为它会覆盖此事件),则不会触发此事件.

在文档此处中提到,滚动至最后一页以读取相同内容. /p>

此外,您是否尝试过查看后台页面的控制台.我看到您编写的代码是错误的. chrome.tabs.create回调函数中没有左括号.如果要将代码添加到回调函数中,请对其进行修复;否则,您可以简单地编写如下代码:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.create({url: chrome.extension.getURL('notes.html')});
});

我希望这会有所帮助.

I'm creating my first chrome extension. When the extension icon is clicked, I wish that chrome will open a new tab, and open there a local .html page I have created.

Following the instructions on Google's documentation, I have created the following:

manisfest.json

{
  "manifest_version": 2,

  "name": "Notes",
  "version": "1.0",

  "browser_action": {
    "default_icon": "ninjaicon.png",
    "default_popup": "notes.html"
},

  "background": {
    "scripts": ["background.js"]
},

  "permissions": [
    "tabs"
 ]
}

background.js

chrome.browserAction.onClicked.addListener(function(activeTab){
  chrome.tabs.create({'url': chrome.extension.getURL('notes.html')}, function(tab)
  });
});

When I press my extensions icon, there is a popup with "notes.html" content, but no new tab is opened like I wished.

How am I suppose to do it correctly? been looking up on a lot of solutions here but non worked. I just wish to create an extension that when clicked opens a new tab with a local web application.

Thanks for the answers.

解决方案

For this to work, the notes.html should be in your extension's root directory.

Remove the default_popup from the manifest file as

The onClicked event is fired when a browser action icon is clicked but this event will not fire if the browser action has a popup as it overrides this event.

This is mentioned in the documentation here, scroll to the last to read the same.

Also, have you tried to see your background page's console. I see the code you have written is wrong. There is no opening brace in your chrome.tabs.create callback function. Fix it if you want to add code to your callback function, if not you can simply write like this:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.create({url: chrome.extension.getURL('notes.html')});
});

I hope this helps.

这篇关于在扩展名鼠标单击上打开本地.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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