digitarald / chromeless-external-links-snippet

Open external links in a new window when your web app runs in a chromeless runtime. Useful for Firefox OS and iOS web apps! Created and tested in Firefox.
MIT License
8 stars 6 forks source link

New version #2

Open Mte90 opened 10 years ago

Mte90 commented 10 years ago

The version of this repo on my devices and simulator of Firefox OS not work. So i have edit and this work without problems :-)

document.addEventListener("DOMContentLoaded", function() {
// Only enable for chromeless window
  if (locationbar.visible) return;

// Selector matches external links, but allows https/http switching
  var selector = "a[href^='http']:not([href*='://" + location.host + "']):not([target='_blank'])";

  Array.prototype.forEach.call(document.querySelectorAll(selector), function(el) {
    var target = el.getAttribute('target');
    if (!target || target.substr(0, 1) === '_') {
      el.setAttribute('target', '_blank');
    }
  });

});
mtbinkdotcom commented 10 years ago

Hi Mte90,

My app currently has the following comment after being reviewed by Firefox Marketplace:

Your app opens some external links in the same window. As the window is chromeless there is no back/forward navigation as well as no hardware back button on FirefoxOS devices. This dead end requires the user to restart the app. Common occurrences for external links are advertisements and sharing buttons. External links need be declared to open in a new window by adding the attribute target=”_blank”. If you can’t change all external links this JavaScript snippet can also automate this process during runtime: http://git.io/rZ8PwA

and that link points to this repository.

// Only enable for chromeless window
if (locationbar.visible) return;

is already enough?

Thanks, mtbink.com

Mte90 commented 10 years ago

The line check if is a webview so in iOS and Firefox For Android/Firefox OS the code it's executed.

Copy and paste in your project :-D

digitarald commented 10 years ago

Sorry for the late response. I'll check the changes and commit them.

For time a pull request would be great to simplify review and merging.

digitarald commented 10 years ago

@Mte90 can you clarify what stopped working? Did the event delegation not work for you?

Delegation is needed to make it work for dynamic content that is loaded after the snippet.

Mte90 commented 10 years ago

Your code in my site don't works but with this all the extern link on my site have the target. i think that add a code in the click event for all the link that check if have the target fix your question.

Mte90 commented 10 years ago

i have do it some test, my code is perfect to add the target at the start but it's not useful if in the page there are some link added in the fly. So i have written a mix of this snippet for a better code/optimization:

document.addEventListener("DOMContentLoaded", function() {
// Selector matches external links, but allows https/http switching
  var selector = "a[href^='http']:not([href*='://" + location.host + "']):not([target='_blank'])";
// Click event handler
  var handleClickEvent = function(evt) {
// Only external links allowed
// Add target when no named target given
    var target = evt.target.getAttribute('target');
    if (!target || target.substr(0, 1) === '_') {
      evt.target.setAttribute('target', '_blank');
    }
  };
// Delegate all clicks on document body
  var _link = document.querySelectorAll(selector);
  for (var _i = 0; _i < _link.length; _i++) {
    _link[_i].addEventListener('click', handleClickEvent, false);
  }
});

With this the event it's attached only on the links that correspond to the selector. I have removed the while that it's no needed anymore.

What do you think about this?

digitarald commented 9 years ago

@Mte90 do you have an example where the snippet doesn't work? I want to add tests to the library before fixing unreproducible issues.

Mte90 commented 9 years ago

has been a long time and do not remember the details but i use in my wp plugin https://github.com/Mte90/Firefox-OS-Bookmark-for-Wordpress/blob/master/public/assets/js/public.js As i have written in the comment my code is perfect to add the target at the start but it's not useful if in the page there are some link added in the fly.So i have written a mix of this snippet for a better code/optimization: