Skip to content

Instantly share code, notes, and snippets.

@franzenzenhofer
Last active March 5, 2024 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franzenzenhofer/078e1f64113a7b1ed5b58df13b06d212 to your computer and use it in GitHub Desktop.
Save franzenzenhofer/078e1f64113a7b1ed5b58df13b06d212 to your computer and use it in GitHub Desktop.
javascript:(function() {
try {
// Check if window.getSelection() is available
var selection = window.getSelection ? window.getSelection().toString().trim() : '';
// Double check to ensure we have access to the current URL
var currentUrl = window.location.href ? window.location.href : 'URL unavailable';
// Construct the message based on the selection or lack thereof
var message = selection || `Fetch content from ${currentUrl} for context. Please wait for further instructions.`;
var baseMessage = `I copied this content:\n\n>>>\n"${message}"\n>>>\n\nFrom this URL: ${currentUrl}\nFetch this URL for more context.\n\nPlease wait for further instructions.`;
var encodedMessage = encodeURIComponent(baseMessage);
// Construct the target URL with the encoded message as a query parameter
var targetUrl = `https://chat.openai.com/g/g-soMXGp85o-franz-enzenhofers-copy-and-fetch/?q=${encodedMessage}`;
// Attempt to open a new tab with the target URL
var newTab = window.open(targetUrl, '_blank');
// Check if the new tab was successfully opened
if (!newTab) {
throw new Error('Failed to open a new tab. Please check if pop-up blockers are enabled and try again.');
}
} catch (error) {
// Fallback or error handling logic
console.error('Bookmarklet Error:', error.message);
alert('An error occurred while executing the bookmarklet. Please check the console for more details.');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment