新标签页上动态URL的Chrome扩展安全策略

Chrome extension security policy for dynamic URLs on new tabs

本文关键字:URL Chrome 扩展 安全策略 动态 标签 新标签      更新时间:2023-09-26

我只是想从输入中获取文本,然后用一个新选项卡/窗口(或)打开一个URL,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 });
});

因此,最终结果是一个带有url的新窗口"https://stackoverflow.com/9999"

目前我收到以下错误。

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

这是在popup.html:30

在html中不能有内联脚本或函数调用(例如,点击输入按钮)

将带有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更改您的ButtonID。