相关疑难解决方法(0)

Chrome扩展程序消息传递:未发送响应

我试图在内容脚本和扩展名之间传递消息

这是我在content-script中的内容

chrome.runtime.sendMessage({type: "getUrls"}, function(response) {
  console.log(response)
});
Run Code Online (Sandbox Code Playgroud)

在我的后台脚本中

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type == "getUrls"){
      getUrls(request, sender, sendResponse)
    }
});

function getUrls(request, sender, sendResponse){
  var resp = sendResponse;
  $.ajax({
    url: "http://localhost:3000/urls",
    method: 'GET',
    success: function(d){
      resp({urls: d})
    }
  });

}
Run Code Online (Sandbox Code Playgroud)

现在,如果我在getUrls函数中的ajax调用之前发送响应,则响应成功发送,但是当我发送响应时,在ajax调用的success方法中它不发送它,当我进入调试时我可以看到sendResponse函数代码中的端口为null .

javascript google-chrome google-chrome-extension google-chrome-app

146
推荐指数
2
解决办法
5万
查看次数

Chrome扩展程序获取所选文字

我正在寻找一种方法将所选文本添加到我的Chrome扩展程序中.

我想要前.在Facebook Feed中选择一个文本,当我点击我的图标时,它将获取它并在我的扩展程序中显示所选文本.

我到目前为止得到了这个:

chrome.tabs.executeScript(null, {
  code: "alert(window.getSelection().toString());"
})
Run Code Online (Sandbox Code Playgroud)

它会获取所选文本并通过Chrome中的消息提醒它.但是我想在我的html弹出窗口中显示它.我想写出来像这样:

document.getElementById("output").value = "Selected text here(but how)"
Run Code Online (Sandbox Code Playgroud)

需要帮忙!而且我知道还有其他问题,但他们没有给我我想要的东西..

html javascript google-chrome google-chrome-extension

25
推荐指数
2
解决办法
3万
查看次数

Chrome扩展程序内容安全策略指令错误

我正在尝试制作无线电流镀铬扩展,但存在问题.当我在浏览器中运行我的脚本时,就像普通的JS + HTML + CSS一样,但是当我尝试像Chrome扩展程序一样运行它时,我收到此错误:

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:"script-src'self'chrome-extension-resource:".要么是'unsafe-inline'关键字,哈希('sha256 -...'),要么是nonce('nonce -...')来启用内联执行.

之后我将其添加到我的清单中:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
Run Code Online (Sandbox Code Playgroud)

但之后我收到错误消息(清单行中的错误与上面的代码)


这是我的表现:

{
   "background": {
      "scripts": [ "jquery.js", "jquery-ui.js", "plate.js" ]
        },

   "browser_action": {
      "default_icon": "Images/action-normal.png",
      "default_popup": "player.html",
      "default_title": ""
   },
   "description": "Chrome Player",
   "manifest_version": 2,
   "name": "Radio Chrome Player",
   "permissions": [ "http://www.radio-station.com/" ],
   "version": "1.0"

   "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

}
Run Code Online (Sandbox Code Playgroud)

这是主要的html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
<script src="main.js"></script> …
Run Code Online (Sandbox Code Playgroud)

html javascript google-chrome google-chrome-extension content-security-policy

20
推荐指数
1
解决办法
4万
查看次数