小编Cam*_*ton的帖子

chrome.browserAction.setIcon没有做任何事情

我正在制作chrome扩展,扩展有两种模式:always on(alwaysOn),或者只有当用户点击它时(onClick).我想根据用户的模式将图标更改为蓝色或红色,以便他们可以一目了然地看到它.但是,添加该chrome.browserAction.setIcon()行后,图标仍然不会在需要时更改.它只是保留在默认徽标上.

这是我的background.js:

// Get the behavior of the plugin; the default is set to "onClick", the other option is "alwaysOn"
chrome.storage.sync.get({
    extensionBehavior: 'onClick'
}, function(items) {
    if(items.extensionBehavior == 'onClick'){
      chrome.browserAction.setIcon({path: "blue-logo.png"});
      chrome.browserAction.onClicked.addListener(function() {
        // When the extension icon is clicked, send a message to the content script
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {"message": tabs[0].url}, function(response){});
        });
      });
    }
    else {
      chrome.browserAction.setIcon({path: "red-logo.png"});
      chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {          
        if (changeInfo.status == 'complete') …
Run Code Online (Sandbox Code Playgroud)

javascript browser google-chrome google-chrome-extension google-chrome-devtools

6
推荐指数
2
解决办法
2223
查看次数

如何使用getAllResponseHeaders方法创建JSON对象

我目前正在撰写Google Chrome扩展程序,我需要找到有关网站响应标头的信息.为了做到这一点,我使用了该getAllResponseHeaders方法,但我需要将它放在JSON对象中.不幸的是,我一直收到错误消息SyntaxError: Unexpected token D in JSON at position 0 at main.

这是我到目前为止用来执行此操作的代码:

xmlhttp.open("GET", url, false);
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        allResponseHeaders = xmlhttp.getAllResponseHeaders();
    }
};
xmlhttp.send();

var responseHeaders = JSON.parse(allResponseHeaders);
obj.headers = responseHeaders;
Run Code Online (Sandbox Code Playgroud)

当我在allResponseHeaders = xmlhttp.getAllResponseHeaders();通话结束后立即发出警报时,警报显示通话成功; 所有响应标头都已被检索.第一个响应头是Date,我认为这与Unexpected token D我的错误消息部分有关,但我不知道它为什么不能正确解析.我该如何解决?提前致谢.

编辑:我希望我的JSON对象看起来像这样:

{
  "headers": {
    "Date": "June 20, 2016",
    "Content-Type": "charset=UTF/8",
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript json google-chrome xmlhttprequest google-chrome-extension

1
推荐指数
2
解决办法
1252
查看次数

MockMultipartFile无法解析为某种类型

我正在为spring程序编写代码,我需要将File对象转换为MultipartFile对象.我试图通过使用MockMultipartFile类来做到这一点:

FileInputStream input = new FileInputStream(toUpload);
MultipartFile multipartFile = new MockMultipartFile("file",
                toUpload.getName(), "text/json", IOUtils.toByteArray(input));
Run Code Online (Sandbox Code Playgroud)

但是,我一直在收到错误MockMultipartFile cannot be resolved to a type.为什么是这样?如果它有帮助,我正在使用Maven,以防它可能是我不知道的依赖性错误.

我也尝试过这样做CommonsMultipartFile.那个代码在这里(toUploadFile我试图转换的对象):

DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, toUpload.getName(), (int) toUpload.length() , toUpload.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
Run Code Online (Sandbox Code Playgroud)

当我尝试这种方法时,我得到了错误The constructor CommonsMultipartFile(DiskFileItem) is undefined.这是为什么?

编辑:这是我们的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-uploading-files</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <dependencies> …
Run Code Online (Sandbox Code Playgroud)

java spring file maven spring-boot

0
推荐指数
1
解决办法
4677
查看次数