【问题标题】:Chrome Extension Link not working... (might be the connected js)Chrome 扩展链接不起作用...(可能是连接的 js)
【发布时间】:2017-03-17 05:38:15
【问题描述】:

看起来应该没有问题,因为我将 4chan 添加到清单的权限药水,但链接仍然没有打开 4chan.org 的新标签。如果有人能说出我的代码有什么问题,我将不胜感激。

这是我当前的代码:

manifest.json:

{
"manifest_version": 2,

"name": "C-List",
"description": "Never Miss a New Thread Again",
"version": "3.0",

"browser_action": {
"default_icon": "icon.jpg",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"activeTab",
"https://www.4chan.org/",
"http://www.google.com/"
]
}

popup.html:

 <!doctype html>
<html>
<head>
<title>C-List</title>
<script src="popup.js"></script>
</head>
<body>
<div style="background-color:rgb(0,255,80)"></div>
<h3>A New Thread Has Been Posted</h3>
<!-- <a id = "link">4Chan</a>
<a href="https://4chan.org/" class="button">Go to Google</a>
<button id="checkPage">Check it out now!</button>
--> 
<button type="button"><a href="https://4chan.org/" class="button">New   Thread</a></button>
</body>
</html>

popup.js:

document.addEventListener('DOMContentLoaded', function() {
var checkPageButton = document.getElementById('checkPage');
checkPageButton.addEventListener('click', function() {

chrome.tabs.getSelected(null, function(tab) {
  d = document;

  var f = d.createElement('form');
  f.action = 'https://4chan.org/';
  f.method = 'post';
  var i = d.createElement('input');
  i.type = 'hidden';
  i.name = 'url';
  i.value = tab.url;
  f.appendChild(i);
  d.body.appendChild(f);
  //f.submit();
  });
  }, false);
  }, false);
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({'url': "https://4chan.org/"});
});
/*
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('checkPage');
// onClick's logic below:
link.addEventListener('click', function() {
    hellYeah('xxx');
});
});

【问题讨论】:

    标签: javascript html json google-chrome-extension


    【解决方案1】:

    chrome.browserAction.onClicked 事件不会在为您的browser_action 定义弹出窗口时触发。你得到任一弹出窗口打开chrome.browserAction.onClicked事件,而不是两者。您获得的完全取决于是否指定了弹出页面(在您的manifest.json 中或使用browserAction.setPopup() 设置)。

    因此,您将需要为您的按钮添加一个事件侦听器。比如:

    document.querySelector('button').addEventListener(function() {
        chrome.tabs.create({'url': "https://4chan.org/"});
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-14
      • 2011-03-13
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多