Blogger's homepage URL obtained using JSON data

Please note: I don't have a background in programming.
I'm making an effort to learn as much as possible.

As I was reading (and later experimenting with) the solution to this query, it dawned on me that having the same information in JSON format could be quite helpful. Specifically, how does one fetch the "home" URL of a Blogger blog using JSON.

I attempted to work it out myself, especially considering that...

<script type="text/javascript>
function specialpost(json) {
 document.write('<div>');
// VARIABLE DECLARATION
 var i;
 var j;
// LOOP
 for (i = 0; i < json.feed.entry.length; i++)
 {
  for (j = 0; j < json.feed.entry[i].link.length; j++) {
   if (json.feed.entry[i].link[j].rel == 'alternate') {
    break;
   }
  }
var postUrl = "'" + json.feed.entry[i].link[j].href + "'";
var postTitle = json.feed.entry[i].title.$t;
var postContent = json.feed.entry[i].content.$t;
var item = '<h3><a href="' + postUrl + '" target="_blank">' + postTitle + '</a></h3><p>' + postContent + '</p>';
 document.write(item);
 }
 document.write('</div>');
 }
</script>
<script src="https://MYBLOG.blogspot.com/feeds/posts/default/-/special?max-results=NUMBER_OF_POSTS_DISPLAYED&alt=json-in-script&callback=specialpost"></script>

...the code will generate a div with titles (including links) and content of specified posts labeled "special". However, halfway through my attempt, things got a bit confusing.

I am using jsonviewer to analyze my blog's JSON data in a structured tree-like manner. I suspect that if each post's URL is within [] entry, then the blog's URL might be located within [] link.

Question: Is this assumption correct?

[] link has four entries: {}0,{}1,{}2, and {}3
each containing three sub-entries:

  • rel set to

    "http://schemas.google.com/g/2005#feed"
    , "self", "alternate", and "hub" respectively

  • type defined as "application/atom+xml" for first two, text/html for the third
    - undefined for {}3

  • href set to:

    "http://MYBLOG.blogspot.com/feeds/posts/default"


    (followed by ?alt=json leading back to the JSON tree view).

    "https://www.blogger.com/feed/1234567890123456789/posts/default?alt=json"


    (19-digit number assigned by Google)

    "http://MYBLOG.blogspot.com" and

    "http://pubsubhubbub.appspot.com"
    respectively.

I believe {}2 holds what I need.

Question: Am I correct again? If so, how do I assign the value of var myblogUrl to the href from {}2?

Lastly: What are the other three entries for? The href in {}0 seems to direct to a subscription page. Is this accurate?

Answer №1

As per the ATOM guidelines regarding the link element (Check out this link for more details)

link - Indicates a connected web page. The relationship type is determined by the rel attribute. Each entry can only have one alternate per type and hreflang. An alternate link must be included in an entry if there is no content element. Additional information available here.

This means that the URLs in the link entry will vary based on the request being made. You would need to utilize the JavaScript split function to extract the homepage value -

var myblogUrl = json.feed.link[0].href.split('feeds')[0];

Please Note: This method works when requesting feeds with URLs like myblog.blogspot.com. If you are requesting feeds from blogger.com (for example, this link), adjustments will be required.

Answer №2

const websiteUrl = data.feed.link.find(item => {
    return item.rel === "alternate";
}).href.split('/search/')[0];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for moving and filling data in a different component using NextJS

Currently, I am developing an application using Next.js and tailwindcss. The Issue In essence, I have a table consisting of 4 columns where each row contains data in 3 columns and the last column includes an "Update" button. The data in each row is genera ...

Is there a way to dynamically update the target of a form using JavaScript?

My goal is to change the target of a form in order to open a new window. However, all redirections of this form use the same window except for one specific button. This button calls a custom JavaScript function that needs to change the default "_self" targ ...

Alter the list of objects in MongoDB to a list of strings that hold their corresponding ObjectId values

I have a database filled with objects that contain information, as well as nested arrays of objects. My goal is to transform the inner arrays into arrays consisting only of ObjectId types with their corresponding ObjectIds. For later retrieval of this inf ...

Unlocking the AngularJS variable within the template script

My controller code: $scope.totals = totals; In the template, I am able to successfully inject HTML using this code: {{totals}} However, my goal is to access the totals variable in a script within the template. Here's what I attempted: <script& ...

Arranging a JSON Object Array in JavaScript by its alphanumeric key attribute order

I need assistance sorting this JSON array by unitId var array = [ { id: 10, unitId: 'unit17' }, { id: 11, unitId: 'unit19' }, { id: 13, unitId: 'unit27' }, { id: 12, unitId: 'unit2' }, { id: 13, unitId: 'unit ...

Change the background color of numbers in an array using DOM manipulation in JavaScript

Can someone assist me with the following task: 1. Generate an array containing 10 numbers ranging from 0 to 360. 2. Display these numbers on the DOM within a chosen element. 3. Adjust the background color of each number based on its HUE value in (hue, sat ...

Loop through unique IDs using AngularJS ng-repeat

I am just starting to delve into AngukarJS and ionic framework, embarking on the journey of creating an app with a structured tree layout as follows: Home Tab (list of books) (templates/allbooks.html) > unique book (book description and chapters) (tem ...

Check out the Pa11y JSON configuration file specifically designed for actions by visiting the following link: https://github.com/pa11y/pa11

Our automated accessibility testing is conducted using the Jenkins CI tool with the help of pa11y. Below is the Jenkinsfile used to execute these tests: node('mypod') { container('centos') { def NODEJS_HOME env.NODEJS_HOME = "${t ...

What could be causing the error "SyntaxError: Expected token ']' to appear" to pop up?

Recently, I have been working on implementing a 'night mode' feature for a website. However, every time I attempt to execute the script, an error message pops up: "SyntaxError: Expected token ']'" on line 9. The problematic line in ...

Tips for retrieving a local variable from inside a callback and using it outside the function

Recently, I started utilizing npm plaid and I'm looking for a way to access the variable trans outside of the plaidClient.getTransactions function. Any suggestions would be greatly appreciated. var trans; var startDate = moment().subtract(30, &apo ...

undefined event typescript this reactjs

I have come across the following TypeScript-written component. The type definitions are from definitelytyped.org. I have bound the onWheel event to a function, but every time it is triggered, this becomes undefined. So, how can I access the referenced el ...

Adding values to a knockout data table without refreshing the page

I am currently experiencing difficulties with refreshing my knockout.js DataTable. I have a data table that loads correctly on page load using the attached function method. I also have multiple buttons (Display All, Allowed, Not Allowed) that display the t ...

Provide the 'URL' of an image in JavaScript

Is it possible to retrieve the image address of an img tag using JavaScript without accessing the src attribute directly? Interestingly, Google Chrome provides a way to copy the image address by right-clicking on an img tag and selecting 'Copy image ...

The div on my webpage is not loading its content as expected when using JavaScript

I am having trouble continuously loading the content of a div from another page. Using alert worked fine, but the page data is not loading. JavaScript <script> $(document).ready(function(){ setInterval(function(){$("#loadAvailable").load("update.p ...

Maintain the previous droppable positioning after refreshing the page

I am encountering an issue with the .droppable event. I have set up two sections where I can move elements, but every time the page is refreshed, the positioning of the elements reverts to the initial position. How can I maintain the last positioning of th ...

Determine image size (pre-upload)

One of the requirements for a project I am working on is to verify the dimensions (width and height) of images before uploading them. I have outlined 3 key checkpoints: 1. If the dimensions are less than 600 X 600 pixels, the upload should be rejected. ...

Python's Choropleth Mapping

Here is the dataset I am working with: This portion shows the code I have written: An error has arisen during my code execution: For those who require the exact code snippet: canada_geo = r'canada_provinces.json' # create a simple world map c ...

Extracting information from a JSON file using the array name provided in the URL

Currently, I am facing an issue with printing data in JSON format from my 'data.json' file. In my PHP file (alldata.php), I have successfully managed to obtain all the data (arrays) and print them out in a pretty format. However, my challenge lie ...

Transmitting an Array Using an Ajax Call

Is there anyone knowledgeable in Ajax here? I'm struggling with sending a javascript array in an ajax request. Can someone provide me with an example of how the ajax request should be formatted? My goal is to gather all the javascript data, package it ...

Interfacing Highcharts with Angular for seamless data binding across series

I am fairly new to using highcharts and I am having difficulty binding my data into the series parameter. In my controller, I have an array of objects that I want to display (when I use console.log, I can see that they are all properly there) this.plotDa ...