Categories
discuss

How to save data to the browser with data: URL?

I have created a bookmarklet that tries to read and write with localStorage with the following data: url:

data:text/html;base64,PGRvY3R5cGUgaHRtbD4KPGh0bWw+Cjxib2R5Pgo8c2NyaXB0Pgpsb2NhbFN0b3JhZ2Uuc2V0SXRlbSgnY29udGVudCcsICdoZWxsbyB3b3JsZCcpOwpkb2N1bWVudC53cml0ZShsb2NhbFN0b3JhZ2UuZ2V0SXRlbSgnY29udGVudCcpKTsKPC9zY3JpcHQ+CjwvYm9keT4KPC9odG1sPg==

This translates to following code in the browser:

<doctype html>
<html>
<body>
<script>
localStorage.setItem('content', 'hello world');
document.write(localStorage.getItem('content'));
</script>
</body>
</html>

This code tries to write the string hello world to the browser’s localStorage, read the string and finally write it to the page.

However, this results in the following error:

Uncaught SecurityError: Failed to read the ‘localStorage’ property from ‘Window’: Storage is disabled inside ‘data:’ URLs.

As this approach doesn’t work, it brings me to the question: How to save data to the browser with data: URL? Is there some other API than localStorage that I could use to save data in data: URLs?

EDIT:

Cookies do not work. Trying to access cookies gives the following error:

Uncaught DOMException: Failed to read the ‘cookie’ property from ‘Document’: Cookies are disabled inside ‘data:’ URLs.

EDIT 2:

File system API does not work either. Fails with the error object:

file error

Answer

@charlietfl Creating a simple “notepad” where I could persist textual content in the browser even when offline or when the browser is restarted.

Working off the notepad use case, the following is a simple solution which works offline, when the browser restarts, persists across multiple devices (if you have your history shared across your different browsers) and you could argue comes with the added bonus of versioning built in…

One ‘storage’ mechanism you have available is the actual url so using it seems like a possible choice. As long as your happy for the url to change in this situation then you could build on top of the following.

<!doctype html>
<html>
<body>
<div id="editable" contenteditable="true">
    My notepad!
</div>
<script>
    document.getElementById('editable').addEventListener('blur', function (event) {
        window.location = 'data:text/html;base64,' + btoa(document.documentElement.outerHTML);
    });
</script>
</body>
</html>

Hope this help!

Categories
discuss

Could not find com.squareup.picasso:picasso:2.5.2

I adding picasso dependencies but seem it not worked. I tried changing the version. But still useless.

This my build.gradle (module)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "bhouse.travellist_starterproject"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:palette-v7:23.4.0'
    compile 'com.squareup.picasso:picasso:2.5.1'

}

and this is my build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

The error said:

Error:(27, 13) Failed to resolve: com.squareup.picasso:picasso:2.5.1 Show in File
Show in Project Structure dialog

Any help are welcome.

Answer

You need to add a section allprojects at the end of your main build.gradle that defines the repositories for the modules of your project:

allprojects {
    repositories {
        jcenter()
    }
}

This will result in the following build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
Categories
discuss

Implement tags bar in JavaFX

Demonstration of answer:(answered May 29 at 3:10 am)

**10/7/2016** you can find the code on GitHub

Sample Tags Image

Actual Question before answered:(asked May 22 at 19:53)

The title might be not too great but what I want to do is something like this in JavaFX:

Examples

YouTube:

YouTube tags example

StackOverFlow(which has and autocomplete):

StackOverflow tags example

Question: I don’t require to write me the code for that. Instead I want to know how I can achieve that using JavaFX and some ideas.

Answer

For the tags you can use a custom styled HBox containing a Text (the tag name) node an a Button (the deletion button (X)). By playing around with the background and the border you can achieve the desired look of the tags.

The onAction handler of the button should remove the tag from it’s parent…

For the whole tag bar you can use another HBox. Use the appropriate border for the correct look. In addition to the tags add a TextField with no background as last element and set the Hgrow property of that TextField to Priotity.ALWAYS to cover the rest of the available space.

The onAction handler of this TextField adds new tags and clears the content of the TextField.

You could e.g. use ControlsFX’s autocompletion features with the TextField or implement it on your own for a custom look…

public class TagBar extends HBox {

    private final ObservableList<String> tags;
    private final TextField inputTextField;

    public ObservableList<String> getTags() {
        return tags;
    }

    public TagBar() {
        getStyleClass().setAll("tag-bar");
        getStylesheets().add(getClass().getResource("style.css").toExternalForm());
        tags = FXCollections.observableArrayList();
        inputTextField = new TextField();
        inputTextField.setOnAction(evt -> {
            String text = inputTextField.getText();
            if (!text.isEmpty() && !tags.contains(text)) {
                tags.add(text);
                inputTextField.clear();
            }
        });

        inputTextField.prefHeightProperty().bind(this.heightProperty());
        HBox.setHgrow(inputTextField, Priority.ALWAYS);
        inputTextField.setBackground(null);

        tags.addListener((ListChangeListener.Change<? extends String> change) -> {
            while (change.next()) {
                if (change.wasPermutated()) {
                    ArrayList<Node> newSublist = new ArrayList<>(change.getTo() - change.getFrom());
                    for (int i = change.getFrom(), end = change.getTo(); i < end; i++) {
                        newSublist.add(null);
                    }
                    for (int i = change.getFrom(), end = change.getTo(); i < end; i++) {
                        newSublist.set(change.getPermutation(i), getChildren().get(i));
                    }
                    getChildren().subList(change.getFrom(), change.getTo()).clear();
                    getChildren().addAll(change.getFrom(), newSublist);
                } else {
                    if (change.wasRemoved()) {
                        getChildren().subList(change.getFrom(), change.getFrom() + change.getRemovedSize()).clear();
                    }
                    if (change.wasAdded()) {
                        getChildren().addAll(change.getFrom(), change.getAddedSubList().stream().map(Tag::new).collect(Collectors.toList()));
                    }
                }
            }
        });
        getChildren().add(inputTextField);
    }

    private class Tag extends HBox {

        public Tag(String tag) {
            getStyleClass().setAll("tag");
            Button removeButton = new Button("X");
            removeButton.setOnAction((evt) -> tags.remove(tag));
            Text text = new Text(tag);
            HBox.setMargin(text, new Insets(0, 0, 0, 5));
            getChildren().addAll(text, removeButton);
        }
    }

}

style.css

.tag-bar {
    -fx-border-color: blue;
    -fx-spacing: 3;
    -fx-padding: 3;
    -fx-max-height: 30;
}

.tag-bar .tag {
    -fx-background-color: lightblue;
    -fx-alignment: center;
}

.tag-bar .tag .button {
    -fx-background-color: transparent;
}
@Override
public void start(Stage primaryStage) {
    Button btn = new Button("Sort");

    StackPane.setAlignment(btn, Pos.BOTTOM_CENTER);

    TagBar tagBar = new TagBar();

    btn.setOnAction((ActionEvent event) -> {
        FXCollections.sort(tagBar.getTags());
    });

    Button btn2 = new Button("add "42"");
    btn2.setOnAction(evt -> {
        if (!tagBar.getTags().contains("42")) {
            tagBar.getTags().add("42");
        }
    });

    VBox root = new VBox();
    root.getChildren().addAll(tagBar, btn, btn2);
    root.setPrefSize(300, 400);

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}
Categories
discuss

Android Studio Assistant: No clients were able to be added to your Firebase project

When using the the new (Firebase) Assistant in Android Studio to add new services I get this error:

Firebase

No clients were able to be added to your Firebase project for the following reasons:
An app with this package name and SHA1 is already connected to a Google project. If you have used a Google API previously, please
select that project in the Connect to an existing Project list.

On the Firebase console page I had selected the option IMPORT GOOGLE PROJECTS and then I continued on integrating the Firebase Services. But when I try to use the Android Studio Firebase Assistant to add more services I get the error above.

And I can’t find this "in the Connect to an existing Project list" anywhere?

Answer

It looks like the Firebase assistant in Android Studio at the moment only works when creating new Firebase projects and not updating existing connected Google Projects.

When selecting the IMPORT GOOGLE PROJECTS on the Firebase console page, it did autogenerate a new OAuth 2.0 client ID for the project credentials on the Google Developer console page. The project had an existing credentials for this, so maybe some conflicts there?

Deleting this new autogenerate credentials and creating a new project instead fixed the issue with the assistant and the message in Android Studio is now:

Firebase

Firebase project created and connected locally to module: app.

Categories
discuss

dev server returned error code 403 react native

Followed the steps here to try react-native android on a windows box.

  1. On a separate prompt I executed react-native start which is running fine
  2. Started the AVD from AVD Manager
  3. Executed react-native run-android

BUILD SUCCESSFUL
Total time: 27.632 secs
Starting the app on emulator-5554 (D:softwareAndroidandroid-sdk/platform-tool
s/adb -s emulator-5554 shell am start -n com.awesomeproject/.MainActivity)…
Starting: Intent { cmp=com.awesomeproject/.MainActivity }

  1. Executed react-native run-android on a separate console from within same folder

But I see an error which I’m having hard time to resolve. Please suggest resolution.

enter image description here
enter image description here

—EDIT—

Possibly the issue is that 8081 port is in use by McAfee. So I updated the server.js to run dev port on 8088 and now when I browse http://localhost:8088/index.android.bundle?platform=android I can see JS loading.

But how do I make android use this address instead of default **10.0.2.2:8081?** I found a file AndroidInfoHelpers.java which contains that 10.0.2.2 path but not sure if that’s the way as being a JAVA file I possibly need to recompile whole program. There should be a simpler way to point android app to use a different port for dev server.

Also, when I execute react-native run-android the first line that gets output says “JS Server not recognized.. Continuing with the build” which likely is shouting the same thing.

Answer

A) Set the new port for dev server
User command
react-native start –port=8088
or
yourprojectnode_modulesreact-nativelocal-cliserverserver.js
to set the the available port say 8080

now use react-native start to start server on 8080

B) Now to establish communication between emulator and dev server, use below command
adb -s emulator -5554 reverse tcp:8088 tcp:8088
i) 5554 =>emulator port (adb devices command will tell you emulator port but if this command is not working then set the path C:/../android-sdk/platform-tools in Path environemnt variable )

ii) 8088 =>server port

C) run the command react-native run-android

D) Follow below steps updating Dev server path in emulator

i) Ctrl + M for windows to open your dev menu for the emulator.
ii) Click Dev Settings
III) Under Debugging select Debug server host & port for device
IV) Enter the url and port for your application as 10.0.2.2:8088

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..