From ecc3f7fa48f1ba9a53e5554790219a860f2a0759 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 2 Aug 2026 14:07:53 -0700 Subject: [PATCH 1/3] Look for links that may be feeds in page content. Fixes #1348. --- .../SafariExtensionHandler.swift | 6 +- .../netnewswire-subscribe-to-feed.js | 184 +++++++++++++----- 2 files changed, 145 insertions(+), 45 deletions(-) diff --git a/Mac/SafariExtension/SafariExtensionHandler.swift b/Mac/SafariExtension/SafariExtensionHandler.swift index 95d079e7f..65f97a7ec 100644 --- a/Mac/SafariExtension/SafariExtensionHandler.swift +++ b/Mac/SafariExtension/SafariExtensionHandler.swift @@ -54,7 +54,11 @@ final class SafariExtensionHandler: SFSafariExtensionHandler { } if openInDefaultBrowser == false { - feedURLString = feedURLString.replacingOccurrences(of: "feed:", with: "x-netnewswire-feed:") + if feedURLString.hasPrefix("feeds:") { + feedURLString = "x-netnewswire-feed:" + feedURLString.dropFirst("feeds:".count) + } else if feedURLString.hasPrefix("feed:") { + feedURLString = "x-netnewswire-feed:" + feedURLString.dropFirst("feed:".count) + } } if let feedURL = URL(string: feedURLString) { diff --git a/Mac/SafariExtension/netnewswire-subscribe-to-feed.js b/Mac/SafariExtension/netnewswire-subscribe-to-feed.js index 267674c97..2687eff31 100644 --- a/Mac/SafariExtension/netnewswire-subscribe-to-feed.js +++ b/Mac/SafariExtension/netnewswire-subscribe-to-feed.js @@ -1,67 +1,159 @@ -var thisPageLinkObjects = null; +var thisPageFeedCandidates = null; -// 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(); +// The detection rules here mirror the app's FeedFinder module (HTMLMetadata.resolveFeedLinks, +// HTMLFeedFinder, FeedSpecifier.calculatedScore) as closely as page-context JavaScript can. +// The extension can't make network requests, so it only suspects feeds — validation happens +// in the app, which runs everything we send through FeedFinder. - linkObject.href = theLink.href; - linkObject.type = theLink.type; - linkObject.title = theLink.title; +var feedURLWordsToMatch = ["feed", "xml", "rss", "atom", "json"]; - return linkObject; +// Matches by MIME type suffix, like the app. +function isFeedType(type) { + return type.endsWith("/rss+xml") || type.endsWith("/atom+xml") || type.endsWith("/json"); +} + +function isSupportedScheme(href) { + return href.startsWith("http://") || href.startsWith("https://") || href.startsWith("feed:") || href.startsWith("feeds:"); } -// 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) + if (!theLink.href || !isSupportedScheme(theLink.href)) { - 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) - { - isValid = (theLink.href != null); - } + return false; } - return isValid; + // An alternate with media or hreflang is another version of the page (mobile, translation), not a feed. + if (theLink.getAttribute("media") || theLink.getAttribute("hreflang")) + { + return false; + } + + // An explicit non-feed type is rejected, but a missing type is fine — some sites leave it off. + var type = theLink.getAttribute("type"); + if (type) + { + return isFeedType(type.toLowerCase()); + } + return true; +} + +function urlStringMightBeFeed(urlString) { + // Mask "buzzfeed" so it doesn't match on "feed". + var massagedURLString = urlString.toLowerCase().split("buzzfeed").join("_"); + + for (var i = 0; i < feedURLWordsToMatch.length; i++) + { + if (massagedURLString.includes(feedURLWordsToMatch[i])) + { + return true; + } + } + return false; } 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 = [] + thisPageFeedCandidates = []; - thisPageLinks = document.querySelectorAll("link[href][rel~='alternate'][type]"); + var orderFound = 0; - for (thisLinkIndex = 0; thisLinkIndex < thisPageLinks.length; thisLinkIndex++) + // First tier: declared feed links in the head. + var headLinks = document.querySelectorAll("link[href][rel~='alternate']"); + for (var i = 0; i < headLinks.length; i++) { - var thisLink = thisPageLinks[thisLinkIndex]; - if (isValidFeedLink(thisLink)) + var headLink = headLinks[i]; + if (isValidFeedLink(headLink)) { - thisPageLinkObjects.push(objectFromLink(thisLink)); + orderFound += 1; + thisPageFeedCandidates.push({ "href": headLink.href, "title": headLink.title, "isHeadLink": true, "orderFound": orderFound }); } } + + // Second tier: anchors anywhere in the page whose URLs look feed-like. + var seenHrefs = {}; + var bodyLinks = document.links; + for (var i = 0; i < bodyLinks.length; i++) + { + var href = bodyLinks[i].href; + if (!href || seenHrefs[href] || !isSupportedScheme(href) || !urlStringMightBeFeed(href)) + { + continue; + } + seenHrefs[href] = true; + orderFound += 1; + thisPageFeedCandidates.push({ "href": href, "title": bodyLinks[i].textContent, "isHeadLink": false, "orderFound": orderFound }); + } } -function subscribeToFeed(theFeed) { +function scoreForCandidate(candidate) { + var score = 0; + var url = candidate.href.toLowerCase(); + + if (candidate.isHeadLink) + { + score += 50; + } + score -= (candidate.orderFound - 1) * 5; + + if (url.includes("comments")) + { + score -= 10; + } + if (url.includes("podcast")) + { + score -= 10; + } + if (url.includes("rss")) + { + score += 5; + } + if (url.endsWith("/index.xml")) + { + score += 5; + } + if (url.endsWith("/feed/")) + { + score += 5; + } + if (url.endsWith("/feed")) + { + score += 4; + } + if (url.includes("json")) + { + score += 3; + } + if (candidate.title && candidate.title.toLowerCase().includes("comments")) + { + score -= 10; + } + + return score; +} + +function bestFeedCandidate() { + var bestCandidate = null; + var bestScore = -Infinity; + + for (var i = 0; i < thisPageFeedCandidates.length; i++) + { + var score = scoreForCandidate(thisPageFeedCandidates[i]); + if (score > bestScore) + { + bestScore = score; + bestCandidate = thisPageFeedCandidates[i]; + } + } + return bestCandidate; +} + +function subscribeToFeed(urlString) { // 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; - if (!feedURL.startsWith('feed:')) + // as "text/xml". + var feedURL = urlString; + if (!feedURL.startsWith('feed:') && !feedURL.startsWith('feeds:')) { feedURL = 'feed:' + feedURL; } @@ -77,10 +169,14 @@ function messageHandler(event) { // 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)) + if ((document.location != null) && (thisPageFeedCandidates.length > 0)) { - feedToOpen = thisPageLinkObjects[0]; - subscribeToFeed(feedToOpen); + var candidate = bestFeedCandidate(); + + // A head link is a declared feed URL — send it directly. A body link is only + // a guess, so send the page URL and let the app find and validate the real feed. + var urlToSend = candidate.isHeadLink ? candidate.href : document.location.href; + subscribeToFeed(urlToSend); } } else if (event.name === "ping") @@ -93,7 +189,7 @@ function messageHandler(event) { // succeed even if its been disabled and the page reloaded. Checking for the existence of // 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); + var shouldValidate = (document.location != null) && (thisPageFeedCandidates.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 }); From f3b3703d551fb99e2363f18d6b80933db6a9ae1f Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 2 Aug 2026 14:09:32 -0700 Subject: [PATCH 2/3] Update version. --- xcconfig/common/NetNewsWire_mac_target_common.xcconfig | 2 +- xcconfig/common/NetNewsWire_version.xcconfig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xcconfig/common/NetNewsWire_mac_target_common.xcconfig b/xcconfig/common/NetNewsWire_mac_target_common.xcconfig index 066402806..f3d6ee4cb 100644 --- a/xcconfig/common/NetNewsWire_mac_target_common.xcconfig +++ b/xcconfig/common/NetNewsWire_mac_target_common.xcconfig @@ -1,7 +1,7 @@ #include "./NetNewsWire_version.xcconfig" // High Level Settings common to both the Mac application and any extensions we bundle with it -MARKETING_VERSION = 7.1.2b2 +MARKETING_VERSION = 7.1.3b1 APP_GROUP_ID = group.$(ORGANIZATION_IDENTIFIER).NetNewsWire-Evergreen$(APP_GROUP_SUFFIX) diff --git a/xcconfig/common/NetNewsWire_version.xcconfig b/xcconfig/common/NetNewsWire_version.xcconfig index c69e0daee..bda8af627 100644 --- a/xcconfig/common/NetNewsWire_version.xcconfig +++ b/xcconfig/common/NetNewsWire_version.xcconfig @@ -1 +1 @@ -CURRENT_PROJECT_VERSION = 7112 +CURRENT_PROJECT_VERSION = 7200 From 73981dd2bc13a3ad32b779ad6ba4da91d7423e91 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 2 Aug 2026 15:15:56 -0700 Subject: [PATCH 3/3] Simplify the Safari extension to always send the page URL and let NetNewsWire's FeedFinder do all feed discovery. Fixes #1348. --- .../netnewswire-subscribe-to-feed.js | 177 +----------------- 1 file changed, 8 insertions(+), 169 deletions(-) diff --git a/Mac/SafariExtension/netnewswire-subscribe-to-feed.js b/Mac/SafariExtension/netnewswire-subscribe-to-feed.js index 2687eff31..e615c3207 100644 --- a/Mac/SafariExtension/netnewswire-subscribe-to-feed.js +++ b/Mac/SafariExtension/netnewswire-subscribe-to-feed.js @@ -1,164 +1,9 @@ -var thisPageFeedCandidates = null; +// The button is enabled on every web page. Clicking sends the page URL to the app, +// whose FeedFinder module does all feed discovery — head links, page scanning, +// URL guessing, special cases — so the logic lives in exactly one place. -// The detection rules here mirror the app's FeedFinder module (HTMLMetadata.resolveFeedLinks, -// HTMLFeedFinder, FeedSpecifier.calculatedScore) as closely as page-context JavaScript can. -// The extension can't make network requests, so it only suspects feeds — validation happens -// in the app, which runs everything we send through FeedFinder. - -var feedURLWordsToMatch = ["feed", "xml", "rss", "atom", "json"]; - -// Matches by MIME type suffix, like the app. -function isFeedType(type) { - return type.endsWith("/rss+xml") || type.endsWith("/atom+xml") || type.endsWith("/json"); -} - -function isSupportedScheme(href) { - return href.startsWith("http://") || href.startsWith("https://") || href.startsWith("feed:") || href.startsWith("feeds:"); -} - -function isValidFeedLink(theLink) { - if (!theLink.href || !isSupportedScheme(theLink.href)) - { - return false; - } - - // An alternate with media or hreflang is another version of the page (mobile, translation), not a feed. - if (theLink.getAttribute("media") || theLink.getAttribute("hreflang")) - { - return false; - } - - // An explicit non-feed type is rejected, but a missing type is fine — some sites leave it off. - var type = theLink.getAttribute("type"); - if (type) - { - return isFeedType(type.toLowerCase()); - } - return true; -} - -function urlStringMightBeFeed(urlString) { - // Mask "buzzfeed" so it doesn't match on "feed". - var massagedURLString = urlString.toLowerCase().split("buzzfeed").join("_"); - - for (var i = 0; i < feedURLWordsToMatch.length; i++) - { - if (massagedURLString.includes(feedURLWordsToMatch[i])) - { - return true; - } - } - return false; -} - -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. - thisPageFeedCandidates = []; - - var orderFound = 0; - - // First tier: declared feed links in the head. - var headLinks = document.querySelectorAll("link[href][rel~='alternate']"); - for (var i = 0; i < headLinks.length; i++) - { - var headLink = headLinks[i]; - if (isValidFeedLink(headLink)) - { - orderFound += 1; - thisPageFeedCandidates.push({ "href": headLink.href, "title": headLink.title, "isHeadLink": true, "orderFound": orderFound }); - } - } - - // Second tier: anchors anywhere in the page whose URLs look feed-like. - var seenHrefs = {}; - var bodyLinks = document.links; - for (var i = 0; i < bodyLinks.length; i++) - { - var href = bodyLinks[i].href; - if (!href || seenHrefs[href] || !isSupportedScheme(href) || !urlStringMightBeFeed(href)) - { - continue; - } - seenHrefs[href] = true; - orderFound += 1; - thisPageFeedCandidates.push({ "href": href, "title": bodyLinks[i].textContent, "isHeadLink": false, "orderFound": orderFound }); - } -} - -function scoreForCandidate(candidate) { - var score = 0; - var url = candidate.href.toLowerCase(); - - if (candidate.isHeadLink) - { - score += 50; - } - score -= (candidate.orderFound - 1) * 5; - - if (url.includes("comments")) - { - score -= 10; - } - if (url.includes("podcast")) - { - score -= 10; - } - if (url.includes("rss")) - { - score += 5; - } - if (url.endsWith("/index.xml")) - { - score += 5; - } - if (url.endsWith("/feed/")) - { - score += 5; - } - if (url.endsWith("/feed")) - { - score += 4; - } - if (url.includes("json")) - { - score += 3; - } - if (candidate.title && candidate.title.toLowerCase().includes("comments")) - { - score -= 10; - } - - return score; -} - -function bestFeedCandidate() { - var bestCandidate = null; - var bestScore = -Infinity; - - for (var i = 0; i < thisPageFeedCandidates.length; i++) - { - var score = scoreForCandidate(thisPageFeedCandidates[i]); - if (score > bestScore) - { - bestScore = score; - bestCandidate = thisPageFeedCandidates[i]; - } - } - return bestCandidate; -} - -function subscribeToFeed(urlString) { - // Convert the URL to a feed:// scheme because Safari - // will refuse to load e.g. a feed that is listed merely - // as "text/xml". - var feedURL = urlString; - if (!feedURL.startsWith('feed:') && !feedURL.startsWith('feeds:')) - { - feedURL = 'feed:' + feedURL; - } - - safari.extension.dispatchMessage("subscribeToFeed", { "url": feedURL }); +function subscribeToPage() { + safari.extension.dispatchMessage("subscribeToFeed", { "url": "feed:" + document.location.href }); } function messageHandler(event) { @@ -169,14 +14,9 @@ function messageHandler(event) { // 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) && (thisPageFeedCandidates.length > 0)) + if (document.location != null) { - var candidate = bestFeedCandidate(); - - // A head link is a declared feed URL — send it directly. A body link is only - // a guess, so send the page URL and let the app find and validate the real feed. - var urlToSend = candidate.isHeadLink ? candidate.href : document.location.href; - subscribeToFeed(urlToSend); + subscribeToPage(); } } else if (event.name === "ping") @@ -189,7 +29,7 @@ function messageHandler(event) { // succeed even if its been disabled and the page reloaded. Checking for the existence of // 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) && (thisPageFeedCandidates.length > 0); + var shouldValidate = (document.location != null); // 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 }); @@ -201,6 +41,5 @@ document.addEventListener("DOMContentLoaded", function(event) { if ((window.top === window) && (typeof safari != 'undefined') && (document.location != null)) { safari.self.addEventListener("message", messageHandler, false) - scanForSyndicationFeeds(); } });