我正在写一个chrome扩展,我无法存储数组.我读到我应该使用JSON stringify/parse来实现这一点,但是使用它时出错了.
chrome.storage.local.get(null, function(userKeyIds){
if(userKeyIds===null){
userKeyIds = [];
}
var userKeyIdsArray = JSON.parse(userKeyIds);
// Here I have an Uncaught SyntaxError: Unexpected token o
userKeyIdsArray.push({keyPairId: keyPairId,HasBeenUploadedYet: false});
chrome.storage.local.set(JSON.stringify(userKeyIdsArray),function(){
if(chrome.runtime.lastError){
console.log("An error occured : "+chrome.runtime.lastError);
}
else{
chrome.storage.local.get(null, function(userKeyIds){
console.log(userKeyIds)});
}
});
});
Run Code Online (Sandbox Code Playgroud)
我怎么能存储像{keyPairId:keyPairId,HasBeenUploadedYet:false}这样的对象数组?
javascript google-chrome local-storage google-chrome-extension
我正在尝试在扩展的选项页面中创建 XMLHttpRequest。在我的options.js文件中,我只有以下代码:
if (window.XMLHttpRequest){
var xhr = new getXMLHttpRequest();
}
Run Code Online (Sandbox Code Playgroud)
但我在控制台中出现此错误
未捕获的 ReferenceError:getXMLHttpRequest 未定义
我在这里看到getXMLHttpRequests 对于托管应用程序来说是一个问题,但在本例中,它是一个简单的扩展,所以我不明白。
我在比较两个字符串时遇到了一个奇怪的问题.这是我的代码:
console.log(x == y);
console.log("'" + x + "'=='" + y + "'");
console.log(typeof(x));
console.log(typeof(y));
Run Code Online (Sandbox Code Playgroud)
在控制台中,我有:
false
'1Ä4±'=='1Ä4±'
string
string
Run Code Online (Sandbox Code Playgroud)
我猜我的字符串包含奇怪的字符,所以我应该如何比较它们呢?我读比较Unicode字符时,JavaScript字符串比较失败,但在我的情况,x以及y来自同一来源,具有相同的编码.
要在Rust中实现迭代器,我们只需要实现该next方法,如文档中所述.然而,该Iterator特征有更多的方法.
据我所知,我们需要实现特征的所有方法.例如,这不编译(playground链接):
struct SomeStruct {}
trait SomeTrait {
fn foo(&self);
fn bar(&self);
}
impl SomeTrait for SomeStruct {
fn foo(&self) {
unimplemented!()
}
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
错误很明显:
error[E0046]: not all trait items implemented, missing: `bar`
--> src/main.rs:8:1
|
5 | fn bar(&self);
| -------------- `bar` from trait
...
8 | impl SomeTrait for SomeStruct {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `bar` in implementation
Run Code Online (Sandbox Code Playgroud) clj-http解释了如何使用 设置日志记录log4j2,但我的项目使用 logback,我无法从clj-http.
这是我正在做的事情的简约再现。
之后lein new test-logging,我编辑project.clj如下:
(defproject test-logging "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[ch.qos.logback/logback-classic "1.2.3"]
[org.clojure/tools.logging "0.4.1"]
[clj-http "3.10.0"]]
:resource-paths ["resources"]
:main ^:skip-aot test-logging.core
:repl-options {:init-ns test-logging.core})
Run Code Online (Sandbox Code Playgroud)
在src/test_logging/core.clj我只有:
(ns test-logging.core
(:require [clojure.tools.logging :as log]
[clj-http.client :as http]))
(defn -main []
(http/post "https://httpbin.org/post" {:body "this is a test"}))
Run Code Online (Sandbox Code Playgroud)
该logback配置文件下resources/logback.xml:
<?xml …Run Code Online (Sandbox Code Playgroud) 我正在迁移到futures0.3 和tokio0.2,并且有一种重复出现的模式我无法重复使用。我不确定这种模式是否已经过时,或者我是否做错了什么Pin。
通常我有一种类型,可以容纳一个套接字和一些通道接收器。此类结构的实现Future包括重复轮询流,直到它们返回Pending(NotReady在 0.1 生态系统中)。
然而,在 futures 0.3 中,用Future::polland代替,这种模式不再起作用:Stream::poll_nextself&mut self
use futures::{
stream::Stream,
task::{Context, Poll},
Future,
};
use std::pin::Pin;
use tokio::sync::mpsc::{Receiver, Sender};
/// Dummy structure that represent some state we update when we
/// receive data or events.
struct State;
impl State {
fn update(&mut self, _data: Vec<u8>) {
println!("updated state");
}
fn handle_event(&mut self, _event: u32) {
println!("handled event");
}
}
/// …Run Code Online (Sandbox Code Playgroud) 我试图实现一个基本上包装的IP地址类型u32:
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Address(u32);
Run Code Online (Sandbox Code Playgroud)
我执行std::ops的是有意义的一个IP地址(运营商&,|,+,-,等).唯一造成麻烦的是std::ops::Index:
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Address(u32);
enum Byte {
A,
B,
C,
D
}
impl ops::Index<Byte> for Address {
type Output = u8;
fn index<'a>(&'a self, byte: Byte) -> &'a Self::Output {
match byte {
Byte::A => ((self.0 & 0xFF000000) >> 24) as u8,
Byte::B => ((self.0 & 0x00FF0000) >> …Run Code Online (Sandbox Code Playgroud) 我有这样的事情:
fun1: function (){
var jqXHR = $.get(
"http://url.com",
"search=toto",
function(){});
jqXHR.fail(function (jqXHR, textStatus, errorThrown){
//do something
});
jqXHR.done(function (data,textStatus,jqXHR){
//do something
});
}
fun2: function (){
fun1();
//do something AFTER my AJAX requests are finished
}
Run Code Online (Sandbox Code Playgroud)
我想等待我的AJAX请求在继续之前完成.为实现这一点,我通常会在fun1()中使用回调函数.但我的代码现在包含几个级别的回调,它看起来很混乱.然后,我想使用该$.when方法,但我不知道如何做到这一点:因为fun1不是延迟对象,我不能只写:
$.when(fun1()).then(//do something);
Run Code Online (Sandbox Code Playgroud) 在我的一个类中,一个方法执行AJAX请求.在请求的回调函数中,我需要使用调用我的对象的另一个方法this.但是this在这种情况下并没有提到我的对象,所以我不知道该怎么做......它是否只有可能?
为澄清,请考虑以下代码:
function MyClass(arg) {
this.foo = arg;
}
MyClass.prototype = {
myMethod: function() {
console.log("I am myMethod");
},
myGet: function (){
$.get("http://example.iana.org/",function(data){
this.myMethod(); // does not work, because 'this' does not refer to my object
});
}
}
var obj = new MyClass("Javascript is complicated");
obj.myGet();
Run Code Online (Sandbox Code Playgroud) 在Tokio文档中,有以下代码段:
extern crate tokio;
extern crate futures;
use futures::future::lazy;
tokio::run(lazy(|| {
for i in 0..4 {
tokio::spawn(lazy(move || {
println!("Hello from task {}", i);
Ok(())
}));
}
Ok(())
}));
Run Code Online (Sandbox Code Playgroud)
对此的解释是:
该
lazy函数在第一次对将来进行轮询时运行关闭。在这里使用它来确保tokio::spawn从任务中调用。如果不使用lazy,tokio::spawn则会从任务的上下文外部调用它,从而导致错误。
尽管对Tokio有所了解,但我不确定我是否能准确理解。看来这两个lazy角色的作用略有不同,并且这种解释仅适用于第一个。难道不是在这里lazy(在for循环内)第二次调用将闭包转换为未来吗?
javascript ×5
rust ×5
asynchronous ×2
future ×2
rust-tokio ×2
traits ×2
ajax ×1
callback ×1
clojure ×1
iterator ×1
jquery ×1
lifetime ×1
logback ×1
logging ×1
raw-pointer ×1
string ×1
this ×1
unsafe ×1