# Change max chars in Onebox - enlarge your Onebox?

**URL:** https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205
**Category:** Support
**Created:** [February 22, 2018, 5:34am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205 "2018-02-22T05:34:33Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [February 22, 2018, 5:34am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/1 "2018-02-22T05:34:33Z")

</div>

After Maja did add a more advance OneBox for Amazon links I would like to know if it is possible to “hack” the OneBox to strip more chars so the Onebox gets bigger.

> [@Import data from e.g Amazon like ISBN etc (A extended onebox solution for Amazon)](https://meta.discourse.org/t/import-data-from-e-g-amazon-like-isbn-etc-a-extended-onebox-solution-for-amazon/79565):
>
> I would like to add a book review section at my site. One box does some of the job like: But is it possible to “hack” to a form like this? Image Title: Author: ISBN: Book description: Star rating from Amazon

Eg I see the max chars for description is 200 chars. Where can I change that? Then I could have a larger OneBox - or is it not possible to set how large you want your onebox?

I dont see any limit in the [onebox/lib/onebox/engine/amazon\_onebox.rb at main · discourse/onebox · GitHub](https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb) so I thought it might be set more globally somewhere…

Best regards,

---

<div class="post-metadata">

### Author: ![maja](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maja/32/129533_2.png) [@maja](https://meta.discourse.org/u/maja)
#### Post date: [February 23, 2018, 3:40pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/2 "2018-02-23T15:40:18Z")

</div>

It’s not possible to change the length of the onebox description unfortunately (the only adjustable is internal post onebox).  
Amazon onebox uses whatever is displayed as a description excerpt on mobile

 ![27](https://global.discourse-cdn.com/meta/original/3X/6/2/6263303157cd2d7f0fe4652e88b826fc374197ef.png)

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [February 23, 2018, 4:39pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/3 "2018-02-23T16:39:12Z")

</div>

Okay, now you had made such a good date ripper for amazon - my hope was to be able to get even more data lenght then the onebox normally gives you.

I can see: [onebox/lib/onebox/engine/google\_play\_app\_onebox.rb at main · discourse/onebox · GitHub](https://github.com/discourse/onebox/blob/master/lib/onebox/engine/google_play_app_onebox.rb) use this code

```
MAX_DESCRIPTION_CHARS: 500

```

But I dont know if I could use something like that in your script… 🙂

---

<div class="post-metadata">

### Author: ![maja](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maja/32/129533_2.png) [@maja](https://meta.discourse.org/u/maja)
#### Post date: [February 23, 2018, 7:42pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/4 "2018-02-23T19:42:36Z")

</div>

It certainly can be done, we’d just have to decide on sensible description length which will be fixed then, so the onebox doesn’t get too big.

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [February 28, 2018, 10:09am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/5 "2018-02-28T10:09:11Z")

</div>

Not to maja specific - she did a great job adding the extension to the amazon script, but more generally.

how do I enlarge the onebox for amazon to make a book review section at my site?

Do I need to code a plugin or?

I see this script:

> <https://github.com/discourse/onebox/blob/main/lib/onebox/engine/github_blob_onebox.rb>

Use these settings:

```
    DEFAULTS = {
    EXPAND_ONE_LINER: EXPAND_AFTER | EXPAND_BEFORE, #set how to expand a one liner. user EXPAND_NONE to disable expand
    LINES_BEFORE: 10,
    LINES_AFTER: 10,
    SHOW_LINE_NUMBER: true,
    MAX_LINES: 20,
    MAX_CHARS: 5000
  }

```

Best regards,

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 2, 2018, 8:45am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/6 "2018-03-02T08:45:35Z")

</div>

I was now able to set up a dev box via this topic: [How to edit the discourse files? A development box? - #11 by mikechristopher](https://meta.discourse.org/t/how-to-edit-the-discourse-files-a-development-box/81388/11)

Can anybody tell me where to find the file responseble for the amazon data ripper.

It seems like is is not the file I thougt it was:

> ```
> https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb
> 
> ```

As there are no such file in my system. Even then the data output is like this on my dev box, with stars info and ISBN etc

> **[Pathology for the Physical Therapist Assistant: 9780323395496: Medicine & Health...](https://www.amazon.com/Pathology-Physical-Therapist-Assistant-Catherine/dp/032339549X)**
>
> Pathology for the Physical Therapist Assistant: 9780323395496: Medicine & Health Science Books @ Amazon.com

Thank you.

---

<div class="post-metadata">

### Author: ![maja](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maja/32/129533_2.png) [@maja](https://meta.discourse.org/u/maja)
#### Post date: [March 2, 2018, 10:55am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/7 "2018-03-02T10:55:30Z")

</div>

[onebox/lib/onebox/engine/amazon\_onebox.rb at main · discourse/onebox · GitHub](https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb) is correct file to modify.

The best would be to clone the onebox library [GitHub - discourse/onebox: (DEPRECATED) A gem for turning URLs into website previews · GitHub](https://github.com/discourse/onebox) (run `bundle install` inside your onebox directory after cloning).

After that you can change the code and run `bundle exec rake server` to preview changes in the browser on `localhost:9000`

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 2, 2018, 11:14am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/8 "2018-03-02T11:14:20Z")

</div>

This way?

`git clone https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb ~/discourse/lib/onebox`

Or should I create a new home folder a side discourse?

`git clone https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb ~/onebox`

---

<div class="post-metadata">

### Author: ![maja](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maja/32/129533_2.png) [@maja](https://meta.discourse.org/u/maja)
#### Post date: [March 2, 2018, 11:26am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/9 "2018-03-02T11:26:16Z")

</div>

> [@frold](#):
>
> `git clone https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb ~/onebox`

Use this one.

If you want Discourse app to use your cloned onebox, you can replace `gem 'onebox', '1.8.42'` with `gem 'onebox', path: 'path-to-your-cloned-onebox'` (run `bundle` after changing Gemfile)

> <https://github.com/discourse/discourse/blob/main/Gemfile#L38>

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 2, 2018, 3:19pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/10 "2018-03-02T15:19:47Z")

</div>

Nice I was able to use

```
gem 'onebox', path: '/Users/username/onebox/'

```

Then I can edit a file and run

```
bundle update

```

before

```
bundle exec rails server

```

Or is there a easier way to do it?

---

<div class="post-metadata">

### Author: ![maja](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maja/32/129533_2.png) [@maja](https://meta.discourse.org/u/maja)
#### Post date: [March 2, 2018, 3:58pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/11 "2018-03-02T15:58:17Z")

</div>

You only have to run `bundle` once after changing Gemfile.  
After that you can run `bundle exec rails server` inside `/discourse` and it should work.

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 4, 2018, 9:54am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/12 "2018-03-04T09:54:04Z")

</div>

UIt seems like Im not able to over rule the settings set somewhere else?

I did add

```
      include HTML

  # Set the max number of chars
  DEFAULTS = {
    MAX_DESCRIPTION_CHARS: 500
  }

```

And then around line 109

I did change line

```
            description: raw.at("#productDescription")&.inner_text,

```

to  
` description: raw.at("#productDescription")&.inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]],`

But Im not able to have more then 250 chars - I can reduce it to eg. 50 chars but not go over 250 - how to?

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 6, 2018, 5:29pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/13 "2018-03-06T17:29:49Z")

</div>

Im a little stucked here ☹

I even tried this: But Im not able to get more then 250 chars - any one who can push my in the right direction?

> **Code snippet**
>
> ```
> require 'json'
> 
> module Onebox
> module Engine
> class AmazonOnebox
> include Engine
> include LayoutSupport
> include HTML
> 
> # Set the max number of chars
> DEFAULTS = {
> MAX_DESCRIPTION_CHARS: 500
> }
> 
> always_https
> matches_regexp(/^https?:\/\/(?:www\.)?(?:smile\.)?(amazon|amzn)\.(?<tld>com|ca|de|it|es|fr|co\.jp|co\.uk|cn|in|com\.br)\//)
> 
> def url
> if match && match[:id]
> return "https://www.amazon.#{tld}/gp/aw/d/#{URI::encode(match[:id])}"
> end
> 
> @url
> end
> 
> def tld
> @tld || @@matcher.match(@url)["tld"]
> end
> 
> def http_params
> {
> 'User-Agent' =>
> 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3'
> }
> end
> 
> private
> 
> def match
> @match ||= @url.match(/(?:d|g)p\/(?:product\/)?(?<id>[^\/]+)(?:\/|$)/mi)
> end
> 
> def image
> if (main_image = raw.css("#main-image")) && main_image.any?
> attributes = main_image.first.attributes
> 
> return attributes["data-a-hires"].to_s if attributes["data-a-hires"]
> 
> if attributes["data-a-dynamic-image"]
> return ::JSON.parse(attributes["data-a-dynamic-image"].value).keys.first
> end
> end
> 
> if (landing_image = raw.css("#landingImage")) && landing_image.any?
> landing_image.first["src"].to_s
> end
> 
> if (ebook_image = raw.css("#ebooksImgBlkFront")) && ebook_image.any?
> ::JSON.parse(ebook_image.first.attributes["data-a-dynamic-image"].value).keys.first
> end
> end
> 
> def price
> # get item price (Amazon markup is inconsistent, deal with it)
> if raw.css("#priceblock_ourprice .restOfPrice")[0] && raw.css("#priceblock_ourprice .restOfPrice")[0].inner_text
> "#{raw.css("#priceblock_ourprice .restOfPrice")[0].inner_text}#{raw.css("#priceblock_ourprice .buyingPrice")[0].inner_text}.#{raw.css("#priceblock_ourprice .restOfPrice")[1].inner_text}"
> elsif raw.css("#priceblock_dealprice") && (dealprice = raw.css("#priceblock_dealprice span")[0])
> dealprice.inner_text
> elsif !raw.css("#priceblock_ourprice").inner_text.empty?
> raw.css("#priceblock_ourprice").inner_text
> else
> raw.css(".mediaMatrixListItem.a-active .a-color-price").inner_text
> end
> end
> 
> def multiple_authors(authors_xpath)
> author_list = raw.xpath(authors_xpath)
> authors = []
> author_list.each { |a| authors << a.inner_text.strip }
> authors.join(", ")
> end
> 
> def data
> og = ::Onebox::Helpers.extract_opengraph(raw)
> 
> if raw.at_css('#dp.book_mobile') #printed books
> title = raw.at("h1#title")&.inner_text
> authors = raw.at_css('#byline_secondary_view_div') ? multiple_authors("//div[@id='byline_secondary_view_div']//span[@class='a-text-bold']") : raw.at("#byline")&.inner_text
> rating = raw.at("#averageCustomerReviews_feature_div .a-icon")&.inner_text || raw.at("#cmrsArcLink .a-icon")&.inner_text
> 
> table_xpath = "//div[@id='productDetails_secondary_view_div']//table[@id='productDetails_techSpec_section_1']"
> isbn = raw.xpath("#{table_xpath}//tr[8]//td").inner_text.strip
> 
> # if ISBN is misplaced or absent it's hard to find out which data is
> # available and where to find it so just set it all to nil
> if /^\d(\-?\d){12}$/.match(isbn)
> publisher = raw.xpath("#{table_xpath}//tr[1]//td").inner_text.strip
> published = raw.xpath("#{table_xpath}//tr[2]//td").inner_text.strip
> book_length = raw.xpath("#{table_xpath}//tr[6]//td").inner_text.strip
> else
> isbn = publisher = published = book_length = nil
> end
> 
> result = {
> link: link,
> title: title,
> by_info: authors,
> image: og[:image] || image,
> description: raw.at("#productDescription")&.inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]],
> rating: "#{rating}#{', ' if rating && (!isbn&.empty? || !price&.empty?)}",
> price: price,
> isbn_asin_text: "ISBN",
> isbn_asin: isbn,
> publisher: publisher,
> published: "#{published}#{', ' if published && !price&.empty?}"
> }
> 
> elsif raw.at_css('#dp.ebooks_mobile') # ebooks
> title = raw.at("#ebooksTitle")&.inner_text
> authors = raw.at_css('#a-popover-mobile-udp-contributor-popover-id') ? multiple_authors("//div[@id='a-popover-mobile-udp-contributor-popover-id']//span[contains(@class,'a-text-bold')]") : (raw.at("#byline")&.inner_text&.strip || raw.at("#bylineInfo")&.inner_text&.strip)
> rating = raw.at("#averageCustomerReviews_feature_div .a-icon")&.inner_text || raw.at("#cmrsArcLink .a-icon")&.inner_text || raw.at("#acrCustomerReviewLink .a-icon")&.inner_text
> 
> table_xpath = "//div[@id='detailBullets_secondary_view_div']//ul"
> asin = raw.xpath("#{table_xpath}//li[4]/span/span[2]").inner_text
> 
> # if ASIN is misplaced or absent it's hard to find out which data is
> # available and where to find it so just set it all to nil
> if /^[0-9A-Z]{10}$/.match(asin)
> publisher = raw.xpath("#{table_xpath}//li[2]/span/span[2]").inner_text
> published = raw.xpath("#{table_xpath}//li[1]/span/span[2]").inner_text
> else
> asin = publisher = published = nil
> end
> 
> result = {
> link: link,
> title: title,
> by_info: authors,
> image: og[:image] || image,
> description: raw.at("#productDescription")&.inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]],
> rating: "#{rating}#{', ' if rating && (!asin&.empty? || !price&.empty?)}",
> price: price,
> isbn_asin_text: "ASIN",
> isbn_asin: asin,
> publisher: publisher,
> published: "#{published}#{', ' if published && !price&.empty?}"
> }
> 
> else
> title = og[:title] || CGI.unescapeHTML(raw.css("title").inner_text)
> result = {
> link: link,
> title: title,
> image: og[:image] || image,
> price: price
> }
> 
> result[:by_info] = raw.at("#by-line")
> result[:by_info] = Onebox::Helpers.clean(result[:by_info].inner_html) if result[:by_info]
> 
> summary = raw.at("#productDescription").inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]]
> result[:description].inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]] = og[:description].inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]] || (summary.DEFAULTS[:MAX_DESCRIPTION_CHARS] && summary.inner_text[0..DEFAULTS[:MAX_DESCRIPTION_CHARS]])
> end
> 
> result
> end
> end
> end
> end
> 
> ```

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 10, 2018, 10:36am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/14 "2018-03-10T10:36:42Z")

</div>

Seems like a dead end?

 ![21](https://global.discourse-cdn.com/meta/original/3X/0/7/0766e3a66d5df2a6432d084cb8abdb8ef3ab33f3.png)

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 12, 2018, 9:03am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/15 "2018-03-12T09:03:12Z")

</div>

Ahh, dooh.

It seems like the source that the script rip data from by standart only contains 250 chars

If you eg. go to

```
https://www.amazon.com/gp/aw/d/0323353177/

```

And view it as user agent:

> Mozilla/5.0 (iPhone; CPU iPhone OS 5\_0\_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3

The div with `productDescription` only contains 250 chars.

If you press the field you go to a new page with the path

```
https://www.amazon.com/gp/aw/d/0323353177/#productDescription_secondary_view_div_1520843164890

```

Here you have the full description inside the div `productDescription_fullView`

It should have a xpath

```
//*[@id="productDescription_fullView"] 

```

A dirty solution in: [onebox/lib/onebox/engine/amazon\_onebox.rb at main · discourse/onebox · GitHub](https://github.com/discourse/onebox/blob/master/lib/onebox/engine/amazon_onebox.rb)

Find the empty line 85 add

```
      description = raw.at_css('#productDescription_fullView')&.inner_text

```

Change line 104

```
        description: raw.at("#productDescription")&.inner_text,

```

to

```
        description: description,

```

To have something like that

 ![41](https://global.discourse-cdn.com/meta/original/3X/1/f/1fbffca42bd9d53d5589b2544f719a1665a8a29f.png)

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [March 17, 2018, 4:01pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/16 "2018-03-17T16:01:33Z")

</div>

I did add the changes to: [Get full details for a printed book by frold · Pull Request #383 · discourse/onebox · GitHub](https://github.com/discourse/onebox/pull/383)

How to use the changes in my live site?

---

<div class="post-metadata">

### Author: ![frold](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frold/32/82547_2.png) [@frold](https://meta.discourse.org/u/frold)
#### Post date: [May 8, 2018, 7:06am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/17 "2018-05-08T07:06:48Z")

</div>

Seems like a dead end?

I can not see that those changes have been adapted either reviewed. 🤔

---

<div class="post-metadata">

### Author: ![hoangviet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hoangviet/32/174417_2.png) [@hoangviet](https://meta.discourse.org/u/hoangviet)
#### Post date: [March 27, 2020, 1:28am UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/18 "2020-03-27T01:28:02Z")

</div>

Can you guys help me write more specific code on how to increase the number of words described in Onebox?  
I have tried several ways, but still cannot increase the number from this description.  
I want to increase the number of descriptive words to 500 words for 1 onebox

Thanks

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 8, 2024, 12:37pm UTC](https://meta.discourse.org/t/change-max-chars-in-onebox-enlarge-your-onebox/81205/19 "2024-06-08T12:37:06Z")

</div>

This topic was automatically closed after 2298 days. New replies are no longer allowed.
