Skip to content

Instantly share code, notes, and snippets.

@tilfin
Last active April 7, 2022 03:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilfin/4992651 to your computer and use it in GitHub Desktop.
Save tilfin/4992651 to your computer and use it in GitHub Desktop.
It is to easily pass data to notification html at createHTMLNotification for Chrome Extension. The way is to store data as JSON string in location.hash.
var data = { image: "image.jpg", text: "text..." };
var popup = webkitNotifications.createHTMLNotification(
'notification.html#' + encodeURIComponent(JSON.stringify(data)) );
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<script src="notification.js"></script>
</head>
<body>
<div id="content"></div>
</body>
</html>
document.addEventListener('DOMContentLoaded', function(){
var data = window.location.hash.substr(1);
data = JSON.parse(decodeURIComponent(data));
var html = '<img src="' + data.image + '">';
html += '<p class="text">' + data.text + '</p></div>';
document.getElementById('content').innerHTML = html;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment