2018-08-19 23:54:26 +02:00
|
|
|
var thisPageLinkObjects = null;
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
// I convert the native "link" node into an object that I can pass out to the global page
|
|
|
|
function objectFromLink(theLink) {
|
|
|
|
var linkObject = new Object();
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
linkObject.href = theLink.href;
|
|
|
|
linkObject.type = theLink.type;
|
|
|
|
linkObject.title = theLink.title;
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
return linkObject;
|
|
|
|
}
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
// Some sites will list feeds with inappropriate or at least less-than-ideal information
|
|
|
|
// in the MIME type attribute. We cover some edge cases here that allow to be passed through,
|
|
|
|
// where they will successfully open as "feed://" URLs in the browser.
|
|
|
|
function isValidFeedLink(theLink) {
|
|
|
|
var isValid = false;
|
|
|
|
|
|
|
|
switch (theLink.type)
|
|
|
|
{
|
|
|
|
case "application/atom+xml":
|
|
|
|
case "application/x.atom+xml":
|
|
|
|
case "application/rss+xml":
|
|
|
|
// These types do not require other criteria.
|
|
|
|
isValid = (theLink.href != null);
|
|
|
|
|
|
|
|
case "text/xml":
|
|
|
|
case "application/rdf+xml":
|
|
|
|
// These types require a title that has "RSS" in it.
|
|
|
|
if (theLink.title && theLink.title.search(/RSS/i) != -1)
|
2018-06-19 19:18:36 +02:00
|
|
|
{
|
2018-08-19 23:54:26 +02:00
|
|
|
isValid = (theLink.href != null);
|
2018-06-19 19:18:36 +02:00
|
|
|
}
|
2018-08-19 23:54:26 +02:00
|
|
|
}
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
return isValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
function scanForSyndicationFeeds() {
|
|
|
|
// In case we don't find any, we establish that we have at least tried by setting the
|
|
|
|
// variables to empty instead of null.
|
|
|
|
thisPageLinkObjects = []
|
|
|
|
|
2018-09-04 12:54:10 +02:00
|
|
|
thisPageLinks = document.querySelectorAll("link[href][rel~='alternate'][type]");
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
for (thisLinkIndex = 0; thisLinkIndex < thisPageLinks.length; thisLinkIndex++)
|
|
|
|
{
|
|
|
|
var thisLink = thisPageLinks[thisLinkIndex];
|
2018-09-04 12:54:10 +02:00
|
|
|
if (isValidFeedLink(thisLink))
|
2018-08-19 23:54:26 +02:00
|
|
|
{
|
2018-09-04 12:54:10 +02:00
|
|
|
thisPageLinkObjects.push(objectFromLink(thisLink));
|
2018-08-19 23:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 19:18:36 +02:00
|
|
|
|
2018-08-19 23:54:26 +02:00
|
|
|
function subscribeToFeed(theFeed) {
|
|
|
|
// Convert the URL to a feed:// scheme because Safari
|
|
|
|
// will refuse to load e.g. a feed that is listed merely
|
|
|
|
// as "text/xml". We do some preflighting of the link rel
|
|
|
|
// in the PageLoadEnd.js so we can be more confident it's a
|
|
|
|
// good feed: URL.
|
|
|
|
var feedURL = theFeed.href;
|
2018-09-04 12:45:29 +02:00
|
|
|
if (!feedURL.startsWith('feed:'))
|
2018-08-19 23:54:26 +02:00
|
|
|
{
|
2018-09-04 12:45:29 +02:00
|
|
|
feedURL = 'feed:' + feedURL;
|
2018-08-19 23:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
safari.extension.dispatchMessage("subscribeToFeed", { "url": feedURL });
|
|
|
|
}
|
|
|
|
|
|
|
|
function messageHandler(event) {
|
|
|
|
if (event.name === "toolbarButtonClicked")
|
|
|
|
{
|
|
|
|
// Workaround Radar #31182842, in which residual copies of our
|
|
|
|
// app extension may remain loaded in context of pages in Safari,
|
|
|
|
// causing multiple responses to broadcast message about toolbar
|
|
|
|
// button being clicked. In the case of the "extra" injections,
|
|
|
|
// the document location is null, so we can avoid doing on anything.
|
|
|
|
if ((document.location != null) && (thisPageLinkObjects.length > 0))
|
|
|
|
{
|
|
|
|
feedToOpen = thisPageLinkObjects[0];
|
|
|
|
subscribeToFeed(feedToOpen);
|
2018-06-19 19:18:36 +02:00
|
|
|
}
|
2018-08-19 23:54:26 +02:00
|
|
|
}
|
|
|
|
else if (event.name === "ping")
|
|
|
|
{
|
|
|
|
// Just a hack to get the toolbar icon validation to work as expected.
|
|
|
|
// If we don't pong back, the extension knows we are not loaded in a page.
|
|
|
|
|
|
|
|
// There is a bug in Safari where the messageHandler is apparently held on to by Safari
|
|
|
|
// even after an extension is disabled. So an effort to "ping" an extension's scripts will
|
2022-01-04 23:25:20 +01:00
|
|
|
// succeed even if its been disabled and the page reloaded. Checking for the existence of
|
2018-08-19 23:54:26 +02:00
|
|
|
// document.location seems to ensure we have enough of a handle still on the document that
|
|
|
|
// we can do something useful with it.
|
|
|
|
var shouldValidate = (document.location != null) && (thisPageLinkObjects.length > 0);
|
|
|
|
|
|
|
|
// Pass back the same validationID we were handed so they can look up the correlated validationHandler
|
|
|
|
safari.extension.dispatchMessage("pong", { "validationID": event.message.validationID, "shouldValidate": shouldValidate });
|
|
|
|
}
|
2018-06-19 19:18:36 +02:00
|
|
|
}
|
2018-08-19 23:54:26 +02:00
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function(event) {
|
|
|
|
// Prevent injecting the JavaScript in IFRAMES, and from acting before Safari is ready...
|
|
|
|
if ((window.top === window) && (typeof safari != 'undefined') && (document.location != null))
|
|
|
|
{
|
|
|
|
safari.self.addEventListener("message", messageHandler, false)
|
|
|
|
scanForSyndicationFeeds();
|
|
|
|
}
|
|
|
|
});
|