@@ -159,12 +159,17 @@ function bookmarksModel(options) {
159159
160160 self . getCurrentBookmarkURL = function ( ) {
161161 if ( self . sharingBookmark ( ) ) {
162- self . shrinkBookmarkURL ( true ) ;
163- return self . useShortBookmarkURL ( ) ;
164- // return self.sharingBookmark().getBookmarkUrl();
162+ if ( self . shrinkBookmarkURL ( ) ) {
163+ // If "Short URL" is checked, get the shortened URL
164+ self . useShortBookmarkURL ( ) ; // This will update currentBookmarkURL with the shortened URL
165+ } else {
166+ // If "Short URL" is unchecked, use the long URL
167+ self . currentBookmarkURL ( self . sharingBookmark ( ) . getBookmarkUrl ( ) ) ;
168+ }
165169 } else {
166- return '' ;
170+ self . currentBookmarkURL ( '' ) ;
167171 }
172+ return self . currentBookmarkURL ( ) ;
168173 } ;
169174
170175 self . shrinkBookmarkURL = ko . observable ( true ) ;
@@ -195,33 +200,56 @@ function bookmarksModel(options) {
195200 } ;
196201
197202 self . useShortBookmarkURL = function ( ) {
198- var bitly_access_token = '227d50a9d70140483b003a70b1f449e00514c053' ;
199- long_url = self . sharingBookmark ( ) . getBookmarkUrl ( ) ;
200- params = {
201- "group_guid" : "Bec5n80dm93" ,
202- "domain" : "bit.ly" ,
203- "long_url" : long_url
204- } ;
203+ // var bitly_access_token = '227d50a9d70140483b003a70b1f449e00514c053';
204+ // long_url = self.sharingBookmark().getBookmarkUrl();
205+ // params = {
206+ // "group_guid": "Bec5n80dm93",
207+ // "domain": "bit.ly",
208+ // "long_url": long_url
209+ // };
210+
211+ // $.ajax({
212+ // type: "POST",
213+ // url: "https://api-ssl.bitly.com/v4/shorten",
214+ // data: JSON.stringify(params),
215+ // success: function(response){
216+ // if (response.link != undefined) {
217+ // $('.in #short-url')[0].value = response.link;
218+ // } else {
219+ // $('.in #short-url')[0].value = long_url;
220+ // }
221+ // },
222+ // dataType: 'json',
223+ // contentType: "application/json",
224+ // beforeSend: function(xhr){
225+ // xhr.setRequestHeader("Authorization", "Bearer " + bitly_access_token);
226+ // }
227+ // });
228+ var long_url = self . sharingBookmark ( ) . getBookmarkUrl ( ) ;
229+
230+ var params = {
231+ "url" : long_url ,
232+ "csrfmiddlewaretoken" : $ ( 'input[name="csrfmiddlewaretoken"]' ) . val ( ) // Including CSRF token for Django's CSRF protection
233+ }
205234
206235 $ . ajax ( {
207- type : "POST" ,
208- url : "https://api-ssl.bitly.com/v4/shorten" ,
209- data : JSON . stringify ( params ) ,
210- success : function ( response ) {
211- if ( response . link != undefined ) {
212- $ ( '.in #short-url' ) [ 0 ] . value = response . link ;
213- } else {
214- $ ( '.in #short-url' ) [ 0 ] . value = long_url ;
215- }
216- } ,
217- dataType : 'json' ,
218- contentType : "application/json" ,
219- beforeSend : function ( xhr ) {
220- xhr . setRequestHeader ( "Authorization" , "Bearer " + bitly_access_token ) ;
221- }
236+ type : "POST" ,
237+ url : "/url_shortener/" ,
238+ data : params ,
239+ success : function ( response ) {
240+ console . log ( response )
241+ if ( response . shortened_url != undefined ) {
242+ self . currentBookmarkURL ( response . shortened_url ) ; // Set the value directly
243+ }
244+ } ,
245+ error : function ( xhr , status , error ) {
246+ console . log ( "Error shortening URL:" , error ) ;
247+ } ,
248+ dataType : 'json' ,
249+ contentType : "application/x-www-form-urlencoded" , // Use URL encoding for the data
222250 } ) ;
223251 } ;
224-
252+ self . currentBookmarkURL = ko . observable ( '' ) ;
225253 self . setBookmarkIFrameHTML = function ( ) {
226254 var bookmarkState = self . sharingBookmark ( ) . getBookmarkHash ( ) ;
227255 $ ( '.in #bookmark-iframe-html' ) [ 0 ] . value = app . viewModel . mapLinks . getIFrameHTML ( bookmarkState ) ;
0 commit comments