User Tools

Site Tools


dokuwiki_bookmarklet

This is an old revision of the document!


Dokuwiki Bookmarklet

Goal: I want to be able to add bookmarks/links easily from my web browser to the Read it later page on this wiki.

Inspired by Shaarli's bookmarklet, I created this javascript laden anchor tag on a blank HTML page:

simplelink.php
<a title="Drag this link to your bookmarks toolbar or right-click it and Bookmark This Link, then click ✚read it later button in any page you want to add to your wiki" class="bookmarklet-link" href="javascript:(
 
function() {
   var%20url%20=%20location.href;
   var%20title%20=%20document.title%20||%20url;
   var%20desc=document.getSelection().toString();
   if(desc.length>4000){
      desc=desc.substr(0,4000)+'...';
         alert('The%20selected%20text%20is%20too%20long,%20it%20will%20be%20truncated.');
      }
      window.open(
         'https://neil.mckillop.org/readitlater.php?post='%20+%20encodeURIComponent(url)+
            '&amp;title='%20+%20encodeURIComponent(title)+
            '&amp;description='%20+%20encodeURIComponent(desc)+
            '&amp;auth_token=PUT_A_TOKEN_HERE'+
            '&amp;source=bookmarklet','_blank','menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1'
         );
      }
)();">✚ read it later</a>

I can drag this onto my bookmarks bar and use on any subsequent pages I visit.

It calls some simple PHP that shows a form that appends a link to the raw file in the dokuwiki data directory (this still keeps all the wiki history - it's marked as an 'external edit').

readitlater.php
<html>
<head>
<style type='text/css'>
h1,h2 {
        text-align: center;
}
input,textarea {
        width: 100%;
        padding: 0.5em;
}
input[type=submit] {
        width: 50%;
        padding: 1em;
}
textarea {
        height: 6em;
        margin-bottom: 1em;
}
div.buttonbar {
        text-align: center;
}
</style>
</head>
<body><?php
$auth_token = "PUT_THE_TOKEN_HERE"; // this should be the one you specify in the bookmarklet above
 
if(!empty($_REQUEST['save_bookmark'])) {
        if(!empty($_REQUEST['auth_token']) and $_REQUEST['auth_token'] == $auth_token) {
                $filename = "/var/lib/dokuwiki/farm/neil.mckillop.org/data/pages/read_it_later.txt";
                $string = "  * ";
                if($_REQUEST['title_to_save'] == $_REQUEST['url_to_save']) {
                        $string .= $_REQUEST['url_to_save'];
                } else {
                        $string .= $_REQUEST['url_to_save']." (".$_REQUEST['title_to_save'].")";
                }
                if(!empty($_REQUEST['desc_to_save']))
                        $string .= " - ".$_REQUEST['desc_to_save'];
                $string .= " [".date("Y-m-d H:i", strtotime('now'))."]";
                $string .= "\n";
                file_put_contents($filename,$string, FILE_APPEND);
        ?>
        <script type='text/javascript'>window.close();</script>
        <?php
        }
} else {
 
        if(empty($_REQUEST['auth_token']) or $_REQUEST['auth_token'] != $auth_token) {
                echo "Access denied.";
        } else {
                ?>
                <form method='post'>
                <input type='hidden' name='auth_token' value="<?= $auth_token; ?>" />";
                <h1>Read it later</h1>
                <h2>URL</h2>
                <input type='text' name='url_to_save' value="<?= $_REQUEST['post']; ?>" />
                <h2>Title</h2>
                <input type='text' name='title_to_save' value="<?= $_REQUEST['title']; ?>" />
                <h2>Description</h2>
                <textarea name='desc_to_save'><?= $_REQUEST['description']; ?></textarea>
                <div class='buttonbar'>
                <input type='submit' name='save_bookmark' value='Save &amp; Close' />
                </div>
                </form>
                <?php
        }
 
}
 
?></body>
</html>
dokuwiki_bookmarklet.1594691515.txt.gz · Last modified: 2020/07/14 01:51 by admin