新标签上的动态网址的Chrome扩展程序安全策略

时间:2013-07-15 21:42:47

标签: javascript html google-chrome-extension

我只是试图从输入中获取文本,然后从那里打开一个带有新标签/窗口(或者)的URL,其中包含此值。

实施例

 <html>
 <head>
<title>Getting Started Extension's Popup</title>
<style>
  body {
    min-width: 357px;
    overflow-x: hidden;
  }
  img {
    margin: 5px;
    border: 2px solid black;
    vertical-align: middle;
    width: 75px;
    height: 75px;
  }
</style>
 </head>
  <body>
   <input type="text" name="enter" class="enter" value="9999" id="lolz"/>
   <input type="button" value="click" />  
  </body>
</html>

popup.js (Unsure about)


chrome.browserAction.onClicked.addListener(function(activeTab){
document.getElementById('lolz').value
var newURL = "https://stackoverflow.com/" + value;
chrome.tabs.create({ url: newURL });
});

所以最终结果是一个带有网址的新窗口&#34; https://stackoverflow.com/9999&#34;

目前我遇到以下错误。

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

这是popup.html:30

1 个答案:

答案 0 :(得分:0)

您不能在html中使用内联脚本或函数调用(例如,输入按钮的onclick)

将带有src的脚本添加到头部

<head>
  ...
  <script src="popup.js"></script>
</head>

为您的按钮添加ID。

popup.js应如下所示:

document.addEventListener('DOMContentLoaded', function () {
    document.querySelector('input#yourButtonID').addEventListener('click', popup);
});
function popup()
{
    var newURL = "http://stackoverflow.com/" + document.getElementById("lolz").value;
    chrome.tabs.create({ url: newURL });
}

使用实际ID更改yourButtonID。