Downloading a selected text on browser - browser extention

I am trying to download a selected word or text on my browser (mozilla or chrome). When i select a word and right click on it, I can see the option "save this selected text to file" option. But I could not get the functionality working.It does not download the text. I am quite new to extention writing. I wanted to learn things by simple examples

manifest.json

    {
        "manifest_version":2,
        "version":"1.0",
        "name": "Selected Text Saver",
        "permissions": [
            "contextMenus",
            "activeTab"
        ],
        "background": {
        "scripts": ["main.js"]
        }
    }

main.js

    mouse1 = function(){
        chrome.tabs.executeScript({
            file: 'saver.js'
        });
    };

    chrome.contextMenus.create({
        title: "Save this selected text to file",
        contexts:["selection"],
        onclick: mouse1
    });

saver.js

    var text = window.getSelection().toString();
    var bb = new Blob([text], {type: 'text/plain'});
    var a = document.createElement('a');
    a.download = document.title.slice(0, 50).replace('/*[<>:/\\|?*]*/g', '') + '.txt';
    a.href = window.URL.createObjectURL(bb);
    a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');

Answers:

Answer

The JavaScript at Question does not perform any further tasks with the created <a> element following a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');.

Append the created <a> element to document.body and call a.click() for user to be prompted offer for download of file.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.