Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js json string to object #49

Open
xgqfrms opened this issue Sep 28, 2018 · 32 comments
Open

js json string to object #49

xgqfrms opened this issue Sep 28, 2018 · 32 comments
Labels
js json string to object js json string to object

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Sep 28, 2018

js json string to object

https://stackoverflow.com/questions/9036429/convert-object-string-to-json

image

const json = {
    "status": 200,
    "success": true,
    "message": null,
    "data": "{\n  version: '1.0',\n  defaultSchema: 'root',\n  schemas: [\n    {\n      name: 'root',\n      cache:'false',\n      type: 'custom',\n      factory: 'org.gil.sydb.server.schema.BaseSchemaFactory',\n      operand: {\n           tables:['*']\n      }\n    },\n    {\n      name: 'news',\n      type: 'custom',\n      factory: 'org.gil.sydb.server.schema.BaseSchemaFactory',\n      operand: {\n       tables:['MockTable']\n      }\n    }\n  ]\n}\n"
};


let str = `${json.data}`;

JSON.parse(JSON.stringify(eval(`(${json.data})`)));

image

https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object

@xgqfrms xgqfrms added the js json string to object js json string to object label Sep 28, 2018
Repository owner locked and limited conversation to collaborators Sep 28, 2018
@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 28, 2018

JSON5

https://json5.org/
https://github.com/json5/json5

https://github.com/json5/json5#json5stringify

<!DOCTYPE html>
<html lang="zh-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="author" content="xgqfrms">
    <meta name="generator" content="VS code">
    <title>JSON5</title>
</head>

<body>
    <section>
        <h1>
            <a href="https://feiqa.xgqfrms.xyz/index.html"></a>
        </h1>
    </section>
    <!-- libs -->
    <!-- <script src="https://unpkg.com/json5@^2.0.0/dist/index.min.js"></script> -->
    <script src="./libs/json5.min.js"></script>
    <script type="module">
        // const JSON5 = require('json5'); 
        import * as JSON5 from "./libs/json5.min.js";
        // JSON5.stringify(value[, replacer[, space]]);
        // JSON5.stringify(value[, options]);
    </script>
</body>

</html>

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 28, 2018

shit json string

image

OK

JSON5

image

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 28, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 28, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

Q: How to disable the Ace editor edit?

A: editor.setReadOnly(true);

https://stackoverflow.com/questions/24757506/how-to-disable-in-ace-editor-the-selection

ajaxorg/ace#406 (comment)

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

Ace & web code editor

https://cdn.xgqfrms.xyz/web-ide/ace.js

bug

relative path & reference bug

image

importScripts('https://cdn.xgqfrms.xyz/web-ide/worker-javascript.js');

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

solution 1

new instance & open Modal

solution 2

API

https://ace.c9.io/#api=editor&nav=api

https://ace.c9.io/api/editor.html

editor.setValue(initial_value); & editor.getValue(); & editor.setReadOnly(true);

    let editor = ace.edit("editor");
    // editor.setReadOnly(true);
    editor.setTheme("ace/theme/monokai");
    editor.session.setMode("ace/mode/javascript");
    const ace_content = document.querySelector(`.ace_content`);
    const ace_box = document.querySelector(`#editor`);
    let initial_html = ``;
    let initial_text = ``;
    let initial_value = ``;
    const showText = () => {
        let text = ace_content.innerText;
        console.log(`text =\n`, text);
    };
    let edit_btn = document.querySelector(`[data-btn="edit"]`),
        save_btn = document.querySelector(`[data-btn="save"]`),
        cancel_btn = document.querySelector(`[data-btn="cancel"]`);
    edit_btn.addEventListener(`click`, () => {
        editor.setReadOnly(false);
        initial_html = ace_box.innerHTML;
        // initial_html = ace_content.innerHTML;
        initial_text = ace_content.innerText;
        initial_value = editor.getValue();
        // console.log(`initial_html =\n`, initial_html);
        console.log(`initial_text =\n`, initial_text);
        console.log(`initial_value =\n`, initial_value);
    });
    save_btn.addEventListener(`click`, () => {
        // update data
        initial_html = ace_box.innerHTML;
        // initial_html = ace_content.innerHTML;
        initial_text = ace_content.innerText;
        initial_value = editor.getValue();
        showText();
        editor.setReadOnly(true);
    });
    cancel_btn.addEventListener(`click`, () => {
        // ace_box.innerHTML = initial_html;
        // ace_content.innerHTML = initial_html;
        // ace_box.innerText = initial_text;
        editor.setValue(initial_value);
        // editor.setValue(initial_text);
        // editor.undo();
        editor.setReadOnly(true);
        // editor.destroy();
    });

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

<meta name="renderer" content="webkit">

<!DOCTYPE html>
<html lang="zh-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="author" content="xgqfrms">
    <meta name="generator" content="VS code">
    <title>ACE in Action</title>
    <style type="text/css" media="screen">
        #editor {
            /* position: absolute; */
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
        }
        
        #editor {
            width: 500px;
            height: 300px;
        }
    </style>
</head>


<body>
    <section>
        <h1>
            <a href="https://feiqa.xgqfrms.xyz/index.html"></a>
        </h1>
    </section>
</body>

</html>

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

ACE in Action

https://cdn.xgqfrms.xyz/web-ide/index.html

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

Chrome Headless

使用 Chrome Headless 模式将 HTML 转 PDF

https://blog.csdn.net/hochenchong/article/details/80357504

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

https://www.cnblogs.com/xgqfrms/p/9724866.html

let url = `http://localhost:9000/api/render`,
    obj = {
        url: "https://developer.mozilla.org/en-US/docs/Web/CSS/calc",
        output: "pdf",
        // scrollPage: true,
        // pdf: {
        //     landscape: true,
        // },
        // output: "screenshot",
        ignoreHttpsErrors: true,
    };
fetch(url,
    {
        method: "POST",
        mode: "cors",
        headers: {
            "Content-Type": "application/json; charset=utf-8",
        },
        body: JSON.stringify(obj),
    })
    .then(res => {
        let headers = res.headers;
        headers.forEach(
            (value, key) => {
                console.log(`response header =`, `${key} = ${value}`);
                // console.log(`response header =`, key, value);
            }
        );
        // console.log(`response headers =`, headers);
        return res.blob();
    })
    .then(blob => {
        // base64
        let objectURL = URL.createObjectURL(blob);
        let a = document.createElement(`a`);
        a.href = objectURL;
        console.log(`a =`, a);
        // let title = document.querySelector(`title`).innerText;
        let title = `test`;
        // png
        // a.setAttribute(`download`, `${title}.png`);
        // pdf
        a.setAttribute(`download`, `${title}.pdf`);
        a.click();
    })
    .catch((err) => {
        console.log(`There has been a problem with your fetch operation: `, err);
    });

url-to-pdf-ap

https://github.com/alvarcarto/url-to-pdf-api#2-local-development

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

css & no margin

no header & no footer

https://stackoverflow.com/questions/46077392/additional-options-in-chrome-headless-print-to-pdf

@page {
    margin: 0;
}

@page {
    margin-top: 0;
}

@page {
    margin-bottom: 0;
}

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

no CORS

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir=D:\chrome

image

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 29, 2018

media & print.css

image

url = `http://localhost:9000/api/render`,
    obj = {
        url: "http://10.1.5.202/stock/f9/fastview/index.html",
        output: "pdf",
        scrollPage: true,
        pdf: {
            landscape: true,
        },
        emulateScreenMedia: true,
        // output: "screenshot",
        ignoreHttpsErrors: true,
    };

fetch(url,
    {
        method: "POST",
        mode: "cors",
        headers: {
            "Content-Type": "application/json; charset=utf-8",
        },
        body: JSON.stringify(obj),
    })
    .then(res => res.blob())
    .then(blob => {
        // base64
        let objectURL = URL.createObjectURL(blob);
        let a = document.createElement(`a`);
        a.href = objectURL;
        let title = `test`;
        // png
        // a.setAttribute(`download`, `${title}.png`);
        // pdf
        a.setAttribute(`download`, `${title}.pdf`);
        a.click();
    })
    .catch((err) => {
        console.log(`There has been a problem with your fetch operation: `, err);
    });

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 30, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 30, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 30, 2018

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 30, 2018

image

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 8, 2018

image

highstock crosshairs

highstock 十字准星线

http://stepday.com/topic/?633

    tooltip: {
        crosshairs:[
            {
                enabled:true,//是否显示X轴标线
                width:3,//标线宽度
                color:'red' //标线颜色值
            },
            {
                enabled:true,//是否显示Y轴标线
                width:3,//标线宽度
                color:'green' //标线颜色值
            }
        ],
        style:{
            display:'none' //通过样式表控制不显示tooltip数据提示框
        }
    },

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 8, 2018

window.parent.location.href;
window.parent.location.pathname;

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 8, 2018

js & throw new Error

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

throw 'Error2'; // generates an exception with a string value
throw 42;       // generates an exception with the value 42
throw true;     // generates an exception with the value true
throw new Error('Required');  // generates an error object with the message of Required

https://stackoverflow.com/questions/9156176/what-is-the-difference-between-throw-new-error-and-throw-someobject

http://www.javascriptkit.com/javatutors/trycatch2.shtml
https://humanwhocodes.com/blog/2009/03/10/the-art-of-throwing-javascript-errors-part-2/

https://www.w3schools.com/js/js_errors.asp

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
js json string to object js json string to object
Projects
None yet
Development

No branches or pull requests

1 participant