简体   繁体   中英

How to email a link to the current page using a bookmarklet without overwriting the content of the tab?

I'm trying to create a Chrome JavaScript bookmarklet that will open my email handler to compose an email with the body containing a link to the current page and the subject being the title of the page. I found this guidance , which suggested using the following code in a bookmarklet:

javascript:document.location="mailto:?subject="+document.title+"&body="+escape(document.location);

This works, but has an annoying behavior of also replacing the content of the current tab with the literal mailto link location:

Chrome窗口显示文字mailto链接

How can I modify the JavaScript so that the bookmarklet mails the link, but preserves the original page content in the browser?

I tried:

  1. Adding return false at the end of the JavaScript. That prevented anything from happening (even generating the email).

  2. Adding location.reload() after the semicolon as in:

     javascript:document.location="mailto:?subject="+document.title+"&body="+escape(document.location); location.reload(); 

    That reloaded the page, but did not generate the email.

  3. Adding location.reload() before the semicolon as in

     javascript:document.location="mailto:?subject="+document.title+"&body="+escape(document.location) location.reload(); 

    That did nothing (didn't reload the page or generate the email).

I'd rather not use an extension for this because:

  1. I find it easier to show / hide bookmarks than dealing with showing / hiding extension icons when I need them / don't need them.

  2. I don't want to deal with activating, allowing in incognito, and browser version compatibility issues with extensions.

You can use window.open()

The obvious solution to not overwrite the current page location is to use window.open() .

javascript:void(window.open("mailto:?subject=" + encodeURIComponent(document.title) + "&body=" + encodeURIComponent(document.location)));

However, that creates a blank tab that will need to be closed, and will often encounter problems with popup blocking. Under some conditions, this tab can be closed programmatically, but that's going to depend on your browser, some browser settings, popup blocker extensions, etc.

Cleaner: use an <iframe>

For this use, it's just as effective, and less prone to problems, to use an <iframe> . The following bookmarklet adds an <iframe> to the end of the document.body using insertAdjacentHTML() . Adding the <iframe> to the document will cause the mailto URL to be evaluated by your browser. The browser will then tell your email program to open a send-email window for you to send the email.

javascript:(function(){var now=Date.now(); document.body.insertAdjacentHTML('beforeend', '<iframe id="iframe-to-delete-' + now + '" src="mailto:?subject=' + encodeURIComponent(document.title.trim()) + '&body=' + encodeURIComponent(document.location) + '" style="display: none !important"></iframe>'); setTimeout(() => document.getElementById('iframe-to-delete-'+now).remove(), 3000);})()

Once the your email program opens the send-email dialog, the bookmarklet can remove the <iframe> , which should leave the page undisturbed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM