1- function addButton ( ) {
1+ function addButton ( ) {
22 const sidebar = document . querySelector ( ".window-sidebar" )
33 const button = document . createElement ( 'span' ) ;
44 button . classList . add ( 'button-link' ) ;
@@ -13,17 +13,17 @@ function addButton(){
1313 const buttonText = document . createElement ( 'span' ) ;
1414 buttonText . innerText = 'Toggl Task' ;
1515 button . append ( buttonText ) ;
16-
16+
1717 sidebar . querySelector ( ".mod-no-top-margin" ) . classList . remove ( "mod-no-top-margin" )
1818 sidebar . querySelector ( ".js-sidebar-add-heading" ) . classList . remove ( "mod-no-top-margin" )
1919}
2020
2121const mappings = {
22- "GG" : "gg" ,
23- "Audience Builder" : "ab" ,
24- "Audience Explorer" : "ae" ,
25- "Cta Calls" : "xcta" ,
26- "Camper" : "camper" ,
22+ "GG" : "gg" ,
23+ "Audience Builder" : "ab" ,
24+ "Audience Explorer" : "ae" ,
25+ "Cta Calls" : "xcta" ,
26+ "Camper" : "camper" ,
2727} ;
2828
2929function onClick ( ) {
@@ -32,29 +32,38 @@ function onClick() {
3232 let token = "***"
3333 console . log ( "Hallo" )
3434 const longId = window . location . pathname . split ( "/" ) [ 2 ] ;
35- var url = ' https://api.trello.com/1/cards/' + longId + ' ?key=' + apiKey + ' &token=' + token
35+ var url = ` https://api.trello.com/1/cards/${ longId } ?key=${ apiKey } &token=${ token } `
3636 fetch ( url ) . then ( response => response . json ( ) ) . then ( response => {
3737 shortId = response [ "idShort" ]
3838
3939 if ( ! response [ "name" ] . endsWith ( shortId ) ) {
4040 for ( var i in response [ "labels" ] ) {
41- var labelShort = mappings [ response [ "labels" ] [ i ] [ "name" ] ]
42- console . log ( labelShort )
41+ if ( labelShort == undefined ) {
42+ var labelShort = mappings [ response [ "labels" ] [ i ] [ "name" ] ]
43+ } else if ( mappings [ response [ "labels" ] [ i ] [ "name" ] ] != undefined ) {
44+ alert ( `Diese Karte hat sowohl "${ labelShort } " als auch "${ mappings [ response [ "labels" ] [ i ] [ "name" ] ] } ".` )
45+ }
4346 }
4447
4548 if ( labelShort != undefined ) {
46- const fixedEncodeURIComponent = ( str ) => {
47- return encodeURIComponent ( str ) . replace ( / [ ! ' ( ) * ] / g, ( c ) => {
48- return '%' + c . charCodeAt ( 0 ) . toString ( 16 ) ;
49- } ) ;
50- }
51- var url = 'https://api.trello.com/1/cards/' + longId + '?name=' + fixedEncodeURIComponent ( response [ "name" ] ) + fixedEncodeURIComponent ( " #" ) + labelShort + "_" + shortId + '&key=' + apiKey + '&token=' + token
49+
50+
51+ const url = new URL ( `https://api.trello.com/1/cards/${ longId } ` )
5252 console . log ( url )
53- fetch ( url , {
54- method : 'PUT'
55- } ) . then ( response => { //response.json()).then(response => {
53+
54+ const data = {
55+ "name" : `${ response [ "name" ] } #${ labelShort } _${ shortId } `
56+ } ;
57+ const jsonBody = JSON . stringify ( data ) ;
58+ fetch ( url . toString ( ) , {
59+ method : 'PUT' ,
60+ headers : {
61+ 'Authorization' : `OAuth oauth_consumer_key="${ apiKey } ", oauth_token="${ token } "` ,
62+ 'Content-Type' : 'application/json'
63+ } ,
64+ body : jsonBody
65+ } ) . then ( response => {
5666 console . log ( response )
57- console . log ( "HURRA!" )
5867 } )
5968 }
6069 }
0 commit comments